使用security 自定义登录页面时碰到的一个错误,页面一直重定向。
网上查了好几天,没有搞明白,利用的是mysql动态查询数据库,在不用自定义登录页面,利用它本身的登录页面是可以进行登录的,用上自己的页面后就出错。
首先说说我的排除过程:
1.过滤登录页面
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/login.html", "/", "/index.html", "/swagger-ui.html#/", "/login_p");
}
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/login.html", "/", "/index.html", "/swagger-ui.html#/", "/login_p").
permitAll()
.anyRequest().authenticated()
//后面的闭合我没写,截取了重要的一部分
用了两种方法过滤,但是还有错;
2.还是过滤的代码,网上说代码的先后顺序也有影响,但是我调整了顺序,依旧没用
3.查看登录的提交地址
<!DOCTYPE html >
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="/login" method="post">
<!-- <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}">-->
<tr>用户名:<input type="text" name="username"></tr>
<tr> 密码:<input type="text" name="password"></tr>
<tr><input type="submit" value="登录"></tr>
<tr>记住我:<input type="checkbox" name="remember-me"></tr>
</table>
</form>
</body>
</html>
formLogin().
loginPage("/login.html").loginProcessingUrl("/login").permitAll()
两处的提交地址一定要相同,loginpage()填写的是自定义的登录的HTML文件或者controller,一开始我没写loginProcessingUrl(),觉得默认应该和loginPage一样的吧,不写的话就是"/login",但是不写就一直出错,后来加上就好了,loginProcessingUrl()填写的是html文件form表单中的提交地址,注意检查路径前面的"/",丢失“/”也会一直报错的,
说完解决方法后说一下他的工作流程,不涉及源码,输入任意一个地址后,跳转到loginPage()的页面,点击登录后进行提交验证,如果提交地址的url写错后,相当于login?error,验证不成功,所以还会重定向到登录页面,就出现了死循环重定向。