content: package com.www.security.handler;
  
  import cn.hutool.json.JSONUtil;
  import com.www.common.exception.CaptchaException;
  import com.www.vo.Result;
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.security.web.authentication.AuthenticationFailureHandler;
  import org.springframework.stereotype.Component;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  /**
   * Created with IntelliJ IDEA 2021.
   *
   * @Author: Mr Qin
   * @Date: 2024/03/25/13:24
   * @Description: 认证失败处理器
   */
  @Component
  public class LoginFailureHandler implements AuthenticationFailureHandler {
  
      @Override
      public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
          // 以流的形式返回
          response.setContentType("application/json;charset=UTF-8");
          ServletOutputStream outputStream = response.getOutputStream();
  
          // 获取异常信息
          String errorMessage;
          if (exception instanceof UsernameNotFoundException || exception instanceof BadCredentialsException) {
              errorMessage = "用户名或密码错误";
          } else if (exception instanceof CaptchaException) {
              errorMessage = exception.getMessage(); // 获取图片验证码错误信息
          } else {
              errorMessage = "登录失败，请稍后重试";
          }
          Result result = Result.fail(errorMessage);
          outputStream.write(JSONUtil.toJsonStr(result).getBytes("UTF-8"));
          outputStream.flush();
          outputStream.close();
      }
  }
  