Spring Security中successHandler无效问题及解决
更新时间:2024年08月01日 15:31:02 作者:一支万宝路
这篇文章主要介绍了Spring Security中successHandler无效问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
Spring Security中successHandler无效
原先代码
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
// 自定义登录页面
.and()
.formLogin()
.loginPage("/login.html")
.successHandler((req, resp, auth) -> {
Object principal = auth.getPrincipal();
resp.setContentType("application/json;charset=utf-8");
PrintWriter out = resp.getWriter();
out.write(new ObjectMapper().writeValueAsString(principal));
out.flush();
out.close();
})
.permitAll()
// 关闭 csrf
.and()
.csrf().disable();
}
以上代码运行之后
无论怎么测试,successHandler都无效,只会返回原来的登录页面,
但是,加入自定义登录接口url之后,successHandler又生效:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
// 自定义登录页面
.and()
.formLogin()
.loginPage("/login.html")
// 自定义登录接口
.loginProcessingUrl("/doLogin")
.successHandler((req, resp, auth) -> {
Object principal = auth.getPrincipal();
resp.setContentType("application/json;charset=utf-8");
PrintWriter out = resp.getWriter();
out.write(new ObjectMapper().writeValueAsString(principal));
out.flush();
out.close();
})
.permitAll()
// 关闭 csrf
.and()
.csrf().disable();
}

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
解决java启动时报线程占用报错:Exception in thread “Thread-14“ java.ne
这篇文章主要给大家介绍了关于解决java启动时报线程占用:Exception in thread “Thread-14“ java.net.BindException: Address already in use: bind的相关资料,文中将解决的办法介绍的非常详细,需要的朋友可以参考下2023-04-04


最新评论