下面列出了org.springframework.security.authentication.AuthenticationProvider#authenticate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
Class<? extends Authentication> toTest = authentication.getClass();
Authentication result = null;
for (AuthenticationProvider provider : providers) {
if (!provider.supports(toTest)) {
continue;
}
// 调用认证提供者进行认证,如果 result 不为 null ,说明认证通过
result = provider.authenticate(authentication);
if (result != null) {
break;
}
}
if (result == null) {
throw new ProviderNotFoundException("ProviderManager.providerNotFound");
}
return result;
}
@Test
public void arbitraryCasedNameReturnsValidAuthentication() {
AuthenticationProvider provider = createProvider();
Authentication authentication = provider.authenticate(createAuthenticationTokenWithUserAndPw("AdMiN", "adm1n"));
Assert.assertNotNull(authentication);
}
@Test
public void validPasswordReturnsValidAuthentication() {
AuthenticationProvider provider = createProvider();
Authentication authentication = provider.authenticate(createAuthenticationTokenWithUserAndPw("admin", "adm1n"));
Assert.assertNotNull(authentication);
}
@Test(expected = BadCredentialsException.class)
public void invalidPasswordRaises() {
AuthenticationProvider provider = createProvider();
provider.authenticate(createAuthenticationTokenWithUserAndPw("admin", "admin"));
}
@Test(expected = BadCredentialsException.class)
public void invalidUserRaises() {
AuthenticationProvider provider = createProvider();
provider.authenticate(createAuthenticationTokenWithUserAndPw("admon", "adm1n"));
}