SpringSecurity自定义登录页面跳转时访问页面302
1 问题原因
登录时报302
2 SecurityConfig 配置 关闭 csrf 跨站请求伪造 还是报错
package com.mangoubiubiu.security.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; /** * SecurityConfig 配置类 */ @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //表单登录 http.formLogin() .loginProcessingUrl("/login") .loginPage("/login.html") .successForwardUrl("/main"); //所有请求都必须被认证(登录) http.authorizeRequests() //放行登录页面 .antMatchers("/login.html").permitAll() //所有请求都必须被认证(登录) .anyRequest().authenticated(); //关闭 csrf 跨站请求伪造 http.csrf().disable(); } @Bean public PasswordEncoder pw(){ return new BCryptPasswordEncoder(); } }
以上代码还是报错
3 究极踩坑点
看看form表单 用户名提交的key
SpringSecurity 默认的 key 是 username 和 password 可以在 UsernamePasswordAuthenticationFilter 这个类里面看
4 解决方案
只需要 将 form表单提交也 的 key 和 SpringSecurity 默认的key 保持一致即可
成功登录
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16060179.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步