Spring Security中successHandler无效问题及解决
目录
- Spring Security中successHandler无效
- 原先代码
- 以上代码运行之后
- 总结
Spring Security中successHandler无效
原先代码
@Override protected void configure(HttpSecurity http) throws Exception {www.devze.com http.authorizeRequejavascriptsts() .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().javascriptwriteValueAsString(principal)); out.flush(); out.close(); }) .permitAll() // 关闭 csrf .and() .csrf().disable(); }
以上代码运行之后
无论怎么测试,successHandler
都无效,只会返回原来的登录页面,
但是,加入自定义登录接口url
之后,successHandler
又生效:
@Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyReqjavascriptuest().authenticated() // 自定义登录页面 .and() .formLogin() .loginPage("/login.html") // 自定义登录接口 .loginProcessingUrl("/WAJAPnoDPidoLogin") .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(); }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论