Spring Boot开发中localhost页面加载异常及无法自定义问题求助
解决Spring Boot启动后出现陌生登录页的问题
检查是否意外引入Spring Security依赖
新手搭建Spring Boot项目时,很容易误选spring-boot-starter-security依赖。这个依赖会自动开启基础身份认证,默认生成登录页,不管你换哪个端口都会触发。- 打开
pom.xml(Maven)或build.gradle(Gradle),如果找到spring-boot-starter-security相关依赖,直接删除,然后重启项目。 - 如果你需要保留Security功能,就新建一个配置类放开访问权限:
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().permitAll() .and().csrf().disable(); } }
- 打开
确认自定义页面的存放位置
自定义首页要放在正确的目录下:- 静态HTML文件直接放到
src/main/resources/static目录(比如index.html) - 用Thymeleaf等模板引擎的话,放到
src/main/resources/templates目录
确保文件名和路径正确,处理完Security配置后再访问端口就能看到自己的页面。
- 静态HTML文件直接放到
强制刷新浏览器缓存
有时候浏览器会缓存旧页面,按Ctrl+Shift+R强制刷新,或者用无痕模式打开页面测试,避免缓存干扰。
内容的提问来源于stack exchange,提问作者Nivetika Rajasekar
相关产品推荐
相关产品推荐

