/** * * @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()); } }
/** * Iterates over the internal list of authentication providers an tries to authenticate. * * @param authentication * The authentication. * @return The resulting authentication. */ public Authentication authenticate(Authentication authentication) { ProviderManager providerManager = getManager(); if (providerManager != null) { try { return providerManager.authenticate(authentication); } catch (ProviderNotFoundException e) { // will be thrown if there is no supporting provider, ignore it since we are not // calling supports methods of the registered providers } } return null; }
private ProviderNotFoundException noAuthDetailsException(String name) { return new ProviderNotFoundException("Auth details '" + name + "' are not configured"); }