下面列出了怎么用org.springframework.security.authentication.ProviderNotFoundException的API类实例代码及写法,或者点击链接到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;
}
/**
*
* @param authentication
* @return Authentication
*/
@Override
public Authentication authenticate(Authentication authentication) {
Realm realm;
Set<GrantedAuthority> auths = new HashSet<>();
try {
realm = getTomcatContextRealm();
if(realm instanceof NullRealm) {
throw new ProviderNotFoundException("No Realms configured for Jwala to Authenticate");
}
Principal principal = realm.authenticate(authentication.getName(),
authentication.getCredentials().toString());
if (principal == null) {
throw new BadCredentialsException("Username or Password not found.");
} else {
if (principal instanceof GenericPrincipal) {
String[] roles = ((GenericPrincipal) principal).getRoles();
for (String role : roles) {
auths.add(new SimpleGrantedAuthority(role));
}
}
GrantedAuthoritiesMapperImpl grantedAuthoritiesMapper = new GrantedAuthoritiesMapperImpl();
return new UsernamePasswordAuthenticationToken(authentication.getName(),
authentication.getCredentials(), grantedAuthoritiesMapper.mapAuthorities(auths));
}
} catch (AttributeNotFoundException | InstanceNotFoundException | MBeanException | ReflectionException e) {
LOGGER.error("Error getting realms", e);
throw new ProviderNotFoundException(e.getMessage());
}
}