认证成功处理器
实际上在UsernamePasswordAuthenticationFilter进行登录认证的时候,如果登录成功了是会调用
AuthenticationSuccessHandler的方法进行认证成功后的处理的。AuthenticationSuccessHandler就是登录成功处理器。
我们也可以自己去自定义成功处理器进行成功后的相应处理。
1@Component 2public class SuccessHandler implements AuthenticationSuccessHandler { 3 @Override 4 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 5 System.out.println("认证成功了"); 6 } 7} 8
认证失败处理器
实际上在UsernamePasswordAuthenticationFilter进行登录认证的时候,如果认证失败了是会调用
AuthenticationFailureHandler的方法进行认证失败后的处理的。AuthenticationFailureHandler就是
登录失败处理器。
我们也可以自己去自定义失败处理器进行失败后的相应处理。
1@Component 2public class FailureHandler implements AuthenticationFailureHandler { 3 @Override 4 public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { 5 System.out.println("认证失败了"); 6 } 7} 8
登出成功处理器
默认是post请求,默认访问路径是/logout。
1@Component 2public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler { 3 @Override 4 public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { 5 //在这里清除redis缓存中的登录数据 6 } 7} 8
加载这些处理器到SecurityConfig中
1@Configuration 2public class SecurityConfig extends WebSecurityConfigurerAdapter { 3 @Autowired 4 private AuthenticationSuccessHandler successHandler; 5 @Autowired 6 private AuthenticationFailureHandler failureHandler; 7 @Autowired 8 private LogoutSuccessHandler logoutSuccessHandler; 9 @Override 10 protected void configure(HttpSecurity http) throws Exception { 11 http.formLogin() 12 // 配置认证成功处理器 13 .successHandler(successHandler) 14 // 配置认证失败处理器 15 .failureHandler(failureHandler); 16 17 http.logout() 18 //配置注销成功处理器 19 .logoutSuccessHandler(logoutSuccessHandler); 20 21 http.authorizeRequests().anyRequest().authenticated(); 22 } 23} 24
《SpringSecurity自定义认证成功、失败、登出成功处理器》 是转载文章,点击查看原文。
