SpringSecurity 自定义登录成功处理器

  1. 编写类实现AuthenticationSuccessHandler重写onAuthenticationSuccess
    public class SuccessHandler implements AuthenticationSuccessHandler {
    
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                            Authentication authentication) throws IOException, ServletException {
    
            Map map = new HashMap();
            map.put("code",200);
            map.put("msg","登录成功");
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println(map);
            writer.close();
        }
    }
    
  2. SpringSecurity配置类中使用自定义的登录成功处理器
    @Configuration
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin()
    //                指定登录路径
                    .loginProcessingUrl("/login")
    //                自定义登录页
                    .loginPage("/login.html")
    //                自定义登录成功处理器
                    .successHandler(new SuccessHandler());
        }
    }
    
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页