@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)该如何实现???
迷茫2017-04-18 10:25:20
重寫它這個類,為它添加驗證碼校驗就可以;
這裡又一個shiro的添加驗證碼的方式範例
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;
}
}