How does springBoot plus spring security plus Kaptcha implement the picture verification code?

  mysql, question
@Component
 public class MyAuthenticationProvider implements AuthenticationProvider {
 
 
 @Autowired
 private CustomUserDetailsService userService;
 @Override
 public Authentication authenticate(Authentication authentication) throws AuthenticationException {
 String username = authentication.getName();
 String password = (String) authentication.getCredentials();
 CustomUserDetails user = (CustomUserDetails) userService.loadUserByUsername(username);
 if(user == null){
 Throw New BadCredential Exception ("User Does Not Exist");
 bracket
 
 if (!  password.equals(user.getPassword())) {
 Throw New BadCredential Exception ("Bad Password");
 bracket
 
 Collection<?   extends GrantedAuthority> authorities = user.getAuthorities();
 return new UsernamePasswordAuthenticationToken(user, password, authorities);
 bracket
 
 @Override
 public boolean supports(Class<?  > arg0) {
 return true;
 bracket
 
 
 bracket

This class verifies the user name and password in the springBoot project. Now how to add Kaptcha? ? ?

Rewrite its class and add verification code to verify it.
Here is another example of shiro’s way of adding verification codes.


public class CaptchaFormAuthenticationFilter extends FormAuthenticationFilter{

private static final Logger LOG = LoggerFactory.getLogger(CaptchaFormAuthenticationFilter.class);
private static final String DEFAULT_CAPTCHA_PARAM = "captcha";

private String captchaParam = DEFAULT_CAPTCHA_PARAM;

public String getCaptchaParam() {
    return captchaParam;
}

public void setCaptchaParam(String captchaParam) {
    this.captchaParam = captchaParam;
}

protected String getCaptcha(ServletRequest request){
    
    return WebUtils.getCleanParam(request, captchaParam);
}

public CaptchaFormAuthenticationFilter() {
}

@Override
protected boolean executeLogin(ServletRequest request,
        ServletResponse response) throws Exception {
    CaptchaUsernamePasswordToken token = (CaptchaUsernamePasswordToken) createToken(request, response);
    boolean isValidate = doCaptchaValidate((HttpServletRequest) request,token);
    if(isValidate){
        Subject subject = getSubject(request, response);
        subject.login(token);
        return super.executeLogin(request, response);
    }
    return false;
}

protected boolean doCaptchaValidate(HttpServletRequest request,CaptchaUsernamePasswordToken token){
    String captcha = (String) request.getSession().getAttribute(DEFAULT_CAPTCHA_PARAM);
     if (captcha != null && !captcha.equalsIgnoreCase(token.getCaptcha())) {  
          return false;
      }  
    return true;
} 
/**
 * 创建口令
 */
@Override
protected AuthenticationToken createToken(ServletRequest request,
        ServletResponse response) {
    String captcha = getCaptcha(request);
    String username = getUsername(request);
    String password = getPassword(request);
    boolean rememberMe = isRememberMe(request);
    String host = getHost(request);
    return new CaptchaUsernamePasswordToken(username, password.toCharArray(), rememberMe, host, captcha) ;
}

bracket

public class CaptchaUsernamePasswordToken extends UsernamePasswordToken {

private static final long serialVersionUID = 8446567829844028162L;
 //verification code string
 private String captcha;
 
 public CaptchaUsernamePasswordToken(String username, char[] password,
 boolean rememberMe, String host, String captcha) {
 super(username, password, rememberMe, host);
 this.captcha = captcha;
 bracket
 
 public String getCaptcha() {
 return captcha;
 bracket
 
 public void setCaptcha(String captcha) {
 this.captcha = captcha;
 bracket

bracket