/** * 此构造函数用来初始化未授信凭据. * * @param principal the principal * @param captcha the captcha * @see CaptchaAuthenticationToken#CaptchaAuthenticationToken(Object, String, Collection) */ publicCaptchaAuthenticationToken(Object principal, String captcha) { super(null); this.principal = principal; this.captcha = captcha; setAuthenticated(false); }
/** * 此构造函数用来初始化授信凭据. * * @param principal the principal * @param captcha the captcha * @param authorities the authorities * @see CaptchaAuthenticationToken#CaptchaAuthenticationToken(Object, String) */ publicCaptchaAuthenticationToken(Object principal, String captcha, Collection<? extends GrantedAuthority> authorities) { super(authorities); this.principal = principal; this.captcha = captcha; super.setAuthenticated(true); // must use super, as we override }
public Object getCredentials() { returnthis.captcha; }
public Object getPrincipal() { returnthis.principal; }
publicvoidsetAuthenticated(boolean isAuthenticated)throws IllegalArgumentException { if (isAuthenticated) { thrownewIllegalArgumentException( "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); }
/** * Instantiates a new Captcha authentication provider. * * @param userDetailsService the user details service * @param captchaService the captcha service */ publicCaptchaAuthenticationProvider(UserDetailsService userDetailsService, CaptchaService captchaService) { this.userDetailsService = userDetailsService; this.captchaService = captchaService; }
@Override public Authentication authenticate(Authentication authentication)throws AuthenticationException { Assert.isInstanceOf(CaptchaAuthenticationToken.class, authentication, () -> messages.getMessage( "CaptchaAuthenticationProvider.onlySupports", "Only CaptchaAuthenticationToken is supported"));
@Override publicvoidafterPropertiesSet()throws Exception { Assert.notNull(userDetailsService, "userDetailsService must not be null"); Assert.notNull(captchaService, "captchaService must not be null"); }
// 设置账户参数的key publicvoidsetUsernameParameter(String usernameParameter) { Assert.hasText(usernameParameter, "Username parameter must not be empty or null"); this.usernameParameter = usernameParameter; }
// 设置密码参数的key publicvoidsetPasswordParameter(String passwordParameter) { Assert.hasText(passwordParameter, "Password parameter must not be empty or null"); this.passwordParameter = passwordParameter; }