Class AbstractUserDetailsAuthenticationProvider
- java.lang.Object
-
- org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider
-
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.InitializingBean,org.springframework.context.MessageSourceAware,AuthenticationProvider
- Direct Known Subclasses:
DaoAuthenticationProvider
public abstract class AbstractUserDetailsAuthenticationProvider extends java.lang.Object implements AuthenticationProvider, org.springframework.beans.factory.InitializingBean, org.springframework.context.MessageSourceAware
A baseAuthenticationProviderthat allows subclasses to override and work withUserDetailsobjects. The class is designed to respond toUsernamePasswordAuthenticationTokenauthentication requests.Upon successful validation, a
UsernamePasswordAuthenticationTokenwill be created and returned to the caller. The token will include as its principal either aStringrepresentation of the username, or theUserDetailsthat was returned from the authentication repository. UsingStringis appropriate if a container adapter is being used, as it expectsStringrepresentations of the username. UsingUserDetailsis appropriate if you require access to additional properties of the authenticated user, such as email addresses, human-friendly names etc. As container adapters are not recommended to be used, andUserDetailsimplementations provide additional flexibility, by default aUserDetailsis returned. To override this default, set thesetForcePrincipalAsString(boolean)totrue.Caching is handled by storing the
UserDetailsobject being placed in theUserCache. This ensures that subsequent requests with the same username can be validated without needing to query theUserDetailsService. It should be noted that if a user appears to present an incorrect password, theUserDetailsServicewill be queried to confirm the most up-to-date password was used for comparison. Caching is only likely to be required for stateless applications. In a normal web application, for example, the SecurityContext is stored in the user's session and the user isn't reauthenticated on each request. The default cache implementation is thereforeNullUserCache.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanhideUserNotFoundExceptionsprotected org.apache.commons.logging.Logloggerprotected org.springframework.context.support.MessageSourceAccessormessages
-
Constructor Summary
Constructors Constructor Description AbstractUserDetailsAuthenticationProvider()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidadditionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication)Allows subclasses to perform any additional checks of a returned (or cached)UserDetailsfor a given authentication request.voidafterPropertiesSet()Authenticationauthenticate(Authentication authentication)Performs authentication with the same contract asAuthenticationManager.authenticate(Authentication).protected AuthenticationcreateSuccessAuthentication(java.lang.Object principal, Authentication authentication, UserDetails user)Creates a successfulAuthenticationobject.protected voiddoAfterPropertiesSet()protected UserDetailsCheckergetPostAuthenticationChecks()protected UserDetailsCheckergetPreAuthenticationChecks()UserCachegetUserCache()booleanisForcePrincipalAsString()booleanisHideUserNotFoundExceptions()protected abstract UserDetailsretrieveUser(java.lang.String username, UsernamePasswordAuthenticationToken authentication)Allows subclasses to actually retrieve theUserDetailsfrom an implementation-specific location, with the option of throwing anAuthenticationExceptionimmediately if the presented credentials are incorrect (this is especially useful if it is necessary to bind to a resource as the user in order to obtain or generate aUserDetails).voidsetAuthoritiesMapper(GrantedAuthoritiesMapper authoritiesMapper)voidsetForcePrincipalAsString(boolean forcePrincipalAsString)voidsetHideUserNotFoundExceptions(boolean hideUserNotFoundExceptions)By default theAbstractUserDetailsAuthenticationProviderthrows aBadCredentialsExceptionif a username is not found or the password is incorrect.voidsetMessageSource(org.springframework.context.MessageSource messageSource)voidsetPostAuthenticationChecks(UserDetailsChecker postAuthenticationChecks)voidsetPreAuthenticationChecks(UserDetailsChecker preAuthenticationChecks)Sets the policy will be used to verify the status of the loaded UserDetails before validation of the credentials takes place.voidsetUserCache(UserCache userCache)booleansupports(java.lang.Class<?> authentication)Returnstrueif thisAuthenticationProvidersupports the indicatedAuthenticationobject.
-
-
-
Method Detail
-
additionalAuthenticationChecks
protected abstract void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException
Allows subclasses to perform any additional checks of a returned (or cached)UserDetailsfor a given authentication request. Generally a subclass will at least compare theAuthentication.getCredentials()with aUserDetails.getPassword(). If custom logic is needed to compare additional properties ofUserDetailsand/orUsernamePasswordAuthenticationToken, these should also appear in this method.- Parameters:
userDetails- as retrieved from theretrieveUser(String, UsernamePasswordAuthenticationToken)orUserCacheauthentication- the current request that needs to be authenticated- Throws:
AuthenticationException- AuthenticationException if the credentials could not be validated (generally aBadCredentialsException, anAuthenticationServiceException)
-
afterPropertiesSet
public final void afterPropertiesSet() throws java.lang.Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
java.lang.Exception
-
authenticate
public Authentication authenticate(Authentication authentication) throws AuthenticationException
Description copied from interface:AuthenticationProviderPerforms authentication with the same contract asAuthenticationManager.authenticate(Authentication).- Specified by:
authenticatein interfaceAuthenticationProvider- Parameters:
authentication- the authentication request object.- Returns:
- a fully authenticated object including credentials. May return
nullif theAuthenticationProvideris unable to support authentication of the passedAuthenticationobject. In such a case, the nextAuthenticationProviderthat supports the presentedAuthenticationclass will be tried. - Throws:
AuthenticationException- if authentication fails.
-
createSuccessAuthentication
protected Authentication createSuccessAuthentication(java.lang.Object principal, Authentication authentication, UserDetails user)
Creates a successfulAuthenticationobject.Protected so subclasses can override.
Subclasses will usually store the original credentials the user supplied (not salted or encoded passwords) in the returned
Authenticationobject.- Parameters:
principal- that should be the principal in the returned object (defined by theisForcePrincipalAsString()method)authentication- that was presented to the provider for validationuser- that was loaded by the implementation- Returns:
- the successful authentication token
-
doAfterPropertiesSet
protected void doAfterPropertiesSet() throws java.lang.Exception- Throws:
java.lang.Exception
-
getUserCache
public UserCache getUserCache()
-
isForcePrincipalAsString
public boolean isForcePrincipalAsString()
-
isHideUserNotFoundExceptions
public boolean isHideUserNotFoundExceptions()
-
retrieveUser
protected abstract UserDetails retrieveUser(java.lang.String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException
Allows subclasses to actually retrieve theUserDetailsfrom an implementation-specific location, with the option of throwing anAuthenticationExceptionimmediately if the presented credentials are incorrect (this is especially useful if it is necessary to bind to a resource as the user in order to obtain or generate aUserDetails).Subclasses are not required to perform any caching, as the
AbstractUserDetailsAuthenticationProviderwill by default cache theUserDetails. The caching ofUserDetailsdoes present additional complexity as this means subsequent requests that rely on the cache will need to still have their credentials validated, even if the correctness of credentials was assured by subclasses adopting a binding-based strategy in this method. Accordingly it is important that subclasses either disable caching (if they want to ensure that this method is the only method that is capable of authenticating a request, as noUserDetailswill ever be cached) or ensure subclasses implementadditionalAuthenticationChecks(UserDetails, UsernamePasswordAuthenticationToken)to compare the credentials of a cachedUserDetailswith subsequent authentication requests.Most of the time subclasses will not perform credentials inspection in this method, instead performing it in
additionalAuthenticationChecks(UserDetails, UsernamePasswordAuthenticationToken)so that code related to credentials validation need not be duplicated across two methods.- Parameters:
username- The username to retrieveauthentication- The authentication request, which subclasses may need to perform a binding-based retrieval of theUserDetails- Returns:
- the user information (never
null- instead an exception should the thrown) - Throws:
AuthenticationException- if the credentials could not be validated (generally aBadCredentialsException, anAuthenticationServiceExceptionorUsernameNotFoundException)
-
setForcePrincipalAsString
public void setForcePrincipalAsString(boolean forcePrincipalAsString)
-
setHideUserNotFoundExceptions
public void setHideUserNotFoundExceptions(boolean hideUserNotFoundExceptions)
By default theAbstractUserDetailsAuthenticationProviderthrows aBadCredentialsExceptionif a username is not found or the password is incorrect. Setting this property tofalsewill causeUsernameNotFoundExceptions to be thrown instead for the former. Note this is considered less secure than throwingBadCredentialsExceptionfor both exceptions.- Parameters:
hideUserNotFoundExceptions- set tofalseif you wishUsernameNotFoundExceptions to be thrown instead of the non-specificBadCredentialsException(defaults totrue)
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource)
- Specified by:
setMessageSourcein interfaceorg.springframework.context.MessageSourceAware
-
setUserCache
public void setUserCache(UserCache userCache)
-
supports
public boolean supports(java.lang.Class<?> authentication)
Description copied from interface:AuthenticationProviderReturnstrueif thisAuthenticationProvidersupports the indicatedAuthenticationobject.Returning
truedoes not guarantee anAuthenticationProviderwill be able to authenticate the presented instance of theAuthenticationclass. It simply indicates it can support closer evaluation of it. AnAuthenticationProvidercan still returnnullfrom theAuthenticationProvider.authenticate(Authentication)method to indicate anotherAuthenticationProvidershould be tried.Selection of an
AuthenticationProvidercapable of performing authentication is conducted at runtime theProviderManager.- Specified by:
supportsin interfaceAuthenticationProvider- Returns:
trueif the implementation can more closely evaluate theAuthenticationclass presented
-
getPreAuthenticationChecks
protected UserDetailsChecker getPreAuthenticationChecks()
-
setPreAuthenticationChecks
public void setPreAuthenticationChecks(UserDetailsChecker preAuthenticationChecks)
Sets the policy will be used to verify the status of the loaded UserDetails before validation of the credentials takes place.- Parameters:
preAuthenticationChecks- strategy to be invoked prior to authentication.
-
getPostAuthenticationChecks
protected UserDetailsChecker getPostAuthenticationChecks()
-
setPostAuthenticationChecks
public void setPostAuthenticationChecks(UserDetailsChecker postAuthenticationChecks)
-
setAuthoritiesMapper
public void setAuthoritiesMapper(GrantedAuthoritiesMapper authoritiesMapper)
-
-