使用SpringSecurity下,发生空转异常
环境信息:
Spring Boot 3.4.4 , jdk 17 , springSecurity 6.4.4
问题背景:
没有自定义controller ,改写了login 页面,并且进行了成功后的跳转处理,发现无法进入到login.html 页面,并且报错。但是如果不进行改写,login 则会自动被跳转到默认登陆页面,完成正常拦截。http://localhost:9999/bootscoder/login 比起我的改写后的少了一个 后缀
问题复现:
核心代码:
配置文件:
// 自定义表单登录配置
http.formLogin(form -> {
form.loginPage("/login.html") // 自定义登录页面
.usernameParameter("username")
.passwordParameter("password")
.loginProcessingUrl("/login")//登录路径,表单向该路径提交,提交后自动执行UserDetailsService的方法
.successForwardUrl("/main.html") // 使用重定向实现登录成功后的跳转
// .successHandler(new MyLoginSuccessHandler()) // 登录成功处理器
.failureForwardUrl("/fail.html");
});
// 设置放行的资源
http.authorizeHttpRequests(authz -> {
// 放行登录页、处理路径、失败页和静态资源
authz.requestMatchers("/login.html", "/fail.html", "/main.html").permitAll();
authz.requestMatchers("/css/*.css", "/js/*.js", "/img/**").permitAll();
// 其余请求要求认证
authz.anyRequest().authenticated();
});
// 关闭csrf防护(仅供测试使用,生产环境需谨慎关闭)
// http.csrf(csrf -> csrf.disable());
登陆页面:
<div class="container">
<h1>用户登录</h1>
<form action="/bootscoder/login" method="post">
<input type="hidden" th:value = "${_csrf.token}" name="_csrf" th:if="${_csrf}">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" id="username" name="username" placeholder="请输入用户名" value="bootscoder" required>
</div>
<div class="form-group">
<label for="password">密码</label>
<input type="password" id="password" name="password" placeholder="请输入密码" value="root" required>
</div>
<input type="checkbox" name="remember-me" value="true"/>记住我</br>
<button type="submit">登录</button>
</form>
</div>
yml前缀配置:
server:
port: 9999
servlet:
context-path: /bootscoder # 所有接口前缀为 /bootscoder
错误信息:
This page isn’t working
localhost redirected you too many times.
Try deleting your cookies
ERR_TOO_MANY_REDIRECTS
分析原因
我的资源放到的是templates中, 也就是说 security 默认的登陆页面在static中。
所以 当我 访问templates下的路径,由于没有写contoller 接口,会发生意外跳转,被拦截后,跳转到static 的默认登陆,然后由于我改写了登陆page,所以又会跳回来,这样导致了 重定向异常。
Spring Security 在配置登录表单时,如果未能找到合适的页面映射(比如你没有正确配置自定义登录页面的访问路径),就可能会触发默认的登录页面(默认在 static 目录下)与自定义页面之间的冲突,从而产生重定向循环。
解决方案
你的分析是正确的。如果你的登录页面放在了 templates 目录下,而没有额外通过 Controller 将其渲染,那么 Spring Boot 默认配置下实际上并不会访问该模板,而是走了静态资源(static 目录)中的默认登录页面,从而产生了重定向循环。
下面有两种解决方案:
方案一:将登录页面移动到 static 目录
如果你不需要使用模板引擎渲染(例如 Thymeleaf 动态处理数据),可以直接把 login.html
文件放到 src/main/resources/static
目录下,这样资源可以直接被以静态资源的形式访问而不会依赖 Controller。
注意:这种方式下,你在 HTML 中写死的路径也需要注意避免重复拼接 context path。例如:
<form th:action="@{/login}" method="post">
或
<form action="/login" method="post">
(注意不要写成
/bootscoder/login
,因为 Spring Boot 会自动处理 context path)
注意⚠️:
不能使用MVC模板引擎:静态资源文件夹中的HTML页面不会被模板引擎(如Thymeleaf)处理,所以不能使用th:action
等Thymeleaf标签。
页面跳转方式变化:
- 在模板引擎处理的页面中,可以通过控制器方法返回视图名称来实现页面跳转
- 在静态页面中,需要使用JavaScript进行页面重定向(
window.location.href
)或通过AJAX请求与后端交互
表单提交方式:
- 静态页面中的表单提交需要通过JavaScript拦截提交事件,使用
fetch
或XMLHttpRequest
等API发送请求到后端 - 不能依赖Spring MVC的表单处理机制直接提交到控制器方法
方案二:为模板页面添加 Controller(推荐)
如果你需要利用模板引擎(如 Thymeleaf)进行页面渲染,则需要提供一个 Controller 映射 GET 请求去返回该模板。例如,你可以这样做:
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class LoginController {
@GetMapping("/login.html")
public String login() {
// 返回 templates/login.html 渲染结果
return "login"; // 注意:返回值 "login" 对应 src/main/resources/templates/login.html
}
}
安全配置修改
同时,确保你的 Spring Security 配置中放行登录页面、登录处理 URL 以及失败页面,如下所示:
http.formLogin(form -> {
form.loginPage("/login.html") // 自定义登录页面,由 Controller 渲染
.usernameParameter("username")
.passwordParameter("password")
.loginProcessingUrl("/login") // 处理登录请求,实际为 /bootscoder/login
.defaultSuccessUrl("/main.html", true)
.failureUrl("/fail.html");
});
http.authorizeHttpRequests(authz -> {
// 放行登录页、登录处理 URL、失败页和静态资源
authz.requestMatchers("/login.html", "/login", "/fail.html", "/main.html").permitAll();
authz.requestMatchers("/css/*.css", "/js/*.js", "/img/**").permitAll();
authz.anyRequest().authenticated();
});
// 如果需要调试,可以先关闭 CSRF(仅用于测试环境)
// http.csrf(csrf -> csrf.disable());
同时,为静态页面(如 fail.html
和 main.html
)也建议确认它们能被正确访问,如果你没有为它们提供 Controller 渲染,那就把这类页面放入 static
目录中。
小结
- 放在
static
目录:如果不依赖模板渲染,可以直接将login.html
放至static
,避免因找不到自定义 Controller 而触发 Security 默认登录页。 - 使用 Controller 渲染模板:如果需要把页面放在
templates
下利用 Thymeleaf 渲染,则必须提供映射该 GET 请求的 Controller,否则 Spring Security 可能依然会使用默认静态的登录页面,造成重定向循环。