java - springBoot+spring security+Kaptcha如何实现图片验证码
阿神
阿神 2017-04-18 10:23:20
0
1
1119
@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 BadCredentialsException("用户不存在");
        }

        if (!password.equals(user.getPassword())) {
            throw new BadCredentialsException("错误的密码");
        }

        Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
        return new UsernamePasswordAuthenticationToken(user, password, authorities);
    }

    @Override
    public boolean supports(Class<?> arg0) {
        return true;
    }


}

在springBoot项目中有这个类验证用户名和密码。现在想添加图片验证码(Kaptcha)该如何实现???

阿神
阿神

闭关修行中......

reply all(1)
迷茫

Rewrite this class and add verification code verification to it;
Here is another example of Shiro’s method of adding verification code


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) ;
}

}

public class CaptchaUsernamePasswordToken extends UsernamePasswordToken {

private static final long serialVersionUID = 8446567829844028162L;
//验证码字符串  
private String captcha;  

public CaptchaUsernamePasswordToken(String username, char[] password,  
        boolean rememberMe, String host, String captcha) {  
    super(username, password, rememberMe, host);  
    this.captcha = captcha;  
}  

public String getCaptcha() {  
    return captcha;  
}  

public void setCaptcha(String captcha) {  
    this.captcha = captcha;  
}  

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!