spring boot security 自定义登陆、成功处理、失败处理

spring security 自定义登录,推荐文章地址:https://www.jianshu.com/p/779d3071e98d

 

附加信息项,自定义登录成功与失败

定义自定义成功处理类,继承SavedRequestAwareAuthenticationSuccessHandler  


   
   
  1. @Component("myAuthenctiationSuccessHandler")
  2. public class MyAuthenctiationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  3. private Logger logger = LoggerFactory.getLogger(getClass());
  4. @Autowired
  5. private ObjectMapper objectMapper;
  6. @Override
  7. public void onAuthenticationSuccess (HttpServletRequest request, HttpServletResponse response, Authentication authentication)
  8. throws IOException, ServletException {
  9. logger.info( "登录成功");
  10. response.setContentType( "application/json;charset=UTF-8");
  11. response.getWriter().write( objectMapper.writeValueAsString(authentication));
  12. }
  13. }

 

定义自定义失败处理类 继承 SimpleUrlAuthenticationFailureHandler


   
   
  1. @Component("myAuthenctiationFailureHandler")
  2. public class MyAuthenctiationFailureHandler extends SimpleUrlAuthenticationFailureHandler {
  3. private Logger logger = LoggerFactory.getLogger(getClass());
  4. @Autowired
  5. private ObjectMapper objectMapper;
  6. @Override
  7. public void onAuthenticationFailure (HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
  8. throws IOException, ServletException {
  9. logger.info( "进入认证失败处理类");
  10. // response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
  11. response.setContentType( "application/json;charset=UTF-8");
  12. //转发到login
  13. // request.getRequestDispatcher("/login?error="+exception.getMessage()).forward(request, response);
  14. response.sendRedirect( "/login?error="+objectMapper.writeValueAsString(exception.getMessage()));
  15. return;
  16. }
  17. }

 

将自定义处理类加入配置

注意:

网上默认添加认证成功处理类在我们自己定义的安全配置类中  BrowerSecurityConfig extends WebSecurityConfigurerAdapter

configure 方法中设置添加


   
   
  1. @Autowired
  2. MyAuthenctiationSuccessHandler myAuthenctiationSuccessHandler; //认证成功处理类
  3. @Autowired
  4. MyAuthenctiationFailureHandler myAuthenctiationFailureHandler; //认证失败处理类
  5. @Override
  6. protected void configure (HttpSecurity http) throws Exception {
  7. http.formLogin() // 定义当需要用户登录时候,转到的登录页面。
  8. .loginPage( "/login") // 设置登录页面
  9. .successHandler(myAuthenctiationSuccessHandler) // 自定义登录成功处理
  10. .failureHandler(myAuthenctiationFailureHandler); // 自定义登录失败处理
  11. ... 此处省略不关键代码
  12. }

当我们使用了自定义过滤器(BhAuthenticationFilter 继承至 AbstractAuthenticationProcessingFilter)开头链接文章有讲解如何自定义过滤器,自定义登录等。

 

使用自定义过滤器后在configure 方法中的http 设置了自定义登录成功与登录失败处理(如上) 不生效

解决办法:需要定义 filter的bean上设置。

例如:


   
   
  1. @Bean
  2. public BhAuthenticationFilter bhAuthenticationFilter () {
  3. BhAuthenticationFilter filter = new BhAuthenticationFilter();
  4. filter.setAuthenticationManager(authenticationManager);
  5. filter.setAuthenticationFailureHandler(myAuthenctiationFailureHandler); //处理失败
  6. filter.setAuthenticationSuccessHandler(myAuthenctiationSuccessHandler); //处理成功
  7. return filter;
  8. }

学习spring security 遇到的坑,仅供参考。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值