Class AuthenticationManagerBuilder
- All Implemented Interfaces:
ProviderManagerBuilder<AuthenticationManagerBuilder>,SecurityBuilder<AuthenticationManager>
SecurityBuilder used to create an AuthenticationManager. Allows for
easily building in memory authentication, LDAP authentication, JDBC based
authentication, adding UserDetailsService, and adding
AuthenticationProvider's.- Since:
- 3.2
-
Constructor Summary
ConstructorsConstructorDescriptionAuthenticationManagerBuilder(ObjectPostProcessor<Object> objectPostProcessor) Deprecated, for removal: This API element is subject to removal in a future version.AuthenticationManagerBuilder(ObjectPostProcessor<Object> objectPostProcessor) Creates a new instance -
Method Summary
Modifier and TypeMethodDescriptionauthenticationEventPublisher(AuthenticationEventPublisher eventPublisher) Sets theAuthenticationEventPublisherauthenticationProvider(AuthenticationProvider authenticationProvider) Add authentication based upon the customAuthenticationProviderthat is passed in.eraseCredentials(boolean eraseCredentials) Gets the defaultUserDetailsServicefor theAuthenticationManagerBuilder.Add in memory authentication to theAuthenticationManagerBuilderand return aInMemoryUserDetailsManagerConfigurerto allow customization of the in memory authentication.booleanDetermines if theAuthenticationManagerBuilderis configured to build a non nullAuthenticationManager.Add JDBC authentication to theAuthenticationManagerBuilderand return aJdbcUserDetailsManagerConfigurerto allow customization of the JDBC authentication.Add LDAP authentication to theAuthenticationManagerBuilderand return aLdapAuthenticationProviderConfigurerto allow customization of the LDAP authentication.parentAuthenticationManager(AuthenticationManager authenticationManager) Allows providing a parentAuthenticationManagerthat will be tried if thisAuthenticationManagerwas unable to attempt to authenticate the providedAuthentication.protected ProviderManagerSubclasses must implement this method to build the object that is being returned.userDetailsService(T userDetailsService) Add authentication based upon the customUserDetailsServicethat is passed in.Methods inherited from class org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder
apply, beforeConfigure, beforeInit, doBuild, getConfigurer, getConfigurers, getOrBuild, getSharedObject, getSharedObjects, objectPostProcessor, postProcess, removeConfigurer, removeConfigurers, setSharedObject, withMethods inherited from class org.springframework.security.config.annotation.AbstractSecurityBuilder
build, getObjectMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.security.config.annotation.SecurityBuilder
build
-
Constructor Details
-
AuthenticationManagerBuilder
Creates a new instance- Parameters:
objectPostProcessor- theObjectPostProcessorinstance to use.
-
AuthenticationManagerBuilder
@Deprecated(since="6.4", forRemoval=true) public AuthenticationManagerBuilder(ObjectPostProcessor<Object> objectPostProcessor) Deprecated, for removal: This API element is subject to removal in a future version.
-
-
Method Details
-
parentAuthenticationManager
public AuthenticationManagerBuilder parentAuthenticationManager(AuthenticationManager authenticationManager) Allows providing a parentAuthenticationManagerthat will be tried if thisAuthenticationManagerwas unable to attempt to authenticate the providedAuthentication.- Parameters:
authenticationManager- theAuthenticationManagerthat should be used if the currentAuthenticationManagerwas unable to attempt to authenticate the providedAuthentication.- Returns:
- the
AuthenticationManagerBuilderfor further adding types of authentication
-
authenticationEventPublisher
public AuthenticationManagerBuilder authenticationEventPublisher(AuthenticationEventPublisher eventPublisher) Sets theAuthenticationEventPublisher- Parameters:
eventPublisher- theAuthenticationEventPublisherto use- Returns:
- the
AuthenticationManagerBuilderfor further customizations
-
eraseCredentials
- Parameters:
eraseCredentials- true ifAuthenticationManagershould clear the credentials from theAuthenticationobject after authenticating- Returns:
- the
AuthenticationManagerBuilderfor further customizations
-
inMemoryAuthentication
public InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> inMemoryAuthentication() throws ExceptionAdd in memory authentication to theAuthenticationManagerBuilderand return aInMemoryUserDetailsManagerConfigurerto allow customization of the in memory authentication.This method also ensure that a
UserDetailsServiceis available for thegetDefaultUserDetailsService()method. Note that additionalUserDetailsService's may override thisUserDetailsServiceas the default.- Returns:
- a
InMemoryUserDetailsManagerConfigurerto allow customization of the in memory authentication - Throws:
Exception- if an error occurs when adding the in memory authentication
-
jdbcAuthentication
public JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> jdbcAuthentication() throws ExceptionAdd JDBC authentication to theAuthenticationManagerBuilderand return aJdbcUserDetailsManagerConfigurerto allow customization of the JDBC authentication.When using with a persistent data store, it is best to add users external of configuration using something like Flyway or Liquibase to create the schema and adding users to ensure these steps are only done once and that the optimal SQL is used.
This method also ensure that a
UserDetailsServiceis available for thegetDefaultUserDetailsService()method. Note that additionalUserDetailsService's may override thisUserDetailsServiceas the default. See the User Schema section of the reference for the default schema.- Returns:
- a
JdbcUserDetailsManagerConfigurerto allow customization of the JDBC authentication - Throws:
Exception- if an error occurs when adding the JDBC authentication
-
userDetailsService
public <T extends UserDetailsService> DaoAuthenticationConfigurer<AuthenticationManagerBuilder,T> userDetailsService(T userDetailsService) throws Exception Add authentication based upon the customUserDetailsServicethat is passed in. It then returns aDaoAuthenticationConfigurerto allow customization of the authentication.This method also ensure that the
UserDetailsServiceis available for thegetDefaultUserDetailsService()method. Note that additionalUserDetailsService's may override thisUserDetailsServiceas the default.- Returns:
- a
DaoAuthenticationConfigurerto allow customization of the DAO authentication - Throws:
Exception- if an error occurs when adding theUserDetailsServicebased authentication
-
ldapAuthentication
public LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthentication() throws ExceptionAdd LDAP authentication to theAuthenticationManagerBuilderand return aLdapAuthenticationProviderConfigurerto allow customization of the LDAP authentication.This method does NOT ensure that a
UserDetailsServiceis available for thegetDefaultUserDetailsService()method.- Returns:
- a
LdapAuthenticationProviderConfigurerto allow customization of the LDAP authentication - Throws:
Exception- if an error occurs when adding the LDAP authentication
-
authenticationProvider
public AuthenticationManagerBuilder authenticationProvider(AuthenticationProvider authenticationProvider) Add authentication based upon the customAuthenticationProviderthat is passed in. Since theAuthenticationProviderimplementation is unknown, all customizations must be done externally and theAuthenticationManagerBuilderis returned immediately.This method does NOT ensure that the
UserDetailsServiceis available for thegetDefaultUserDetailsService()method. Note that anExceptionmight be thrown if an error occurs when adding theAuthenticationProvider.- Specified by:
authenticationProviderin interfaceProviderManagerBuilder<AuthenticationManagerBuilder>- Returns:
- a
AuthenticationManagerBuilderto allow further authentication to be provided to theAuthenticationManagerBuilder
-
performBuild
Description copied from class:AbstractConfiguredSecurityBuilderSubclasses must implement this method to build the object that is being returned.- Specified by:
performBuildin classAbstractConfiguredSecurityBuilder<AuthenticationManager,AuthenticationManagerBuilder> - Returns:
- the Object to be buit or null if the implementation allows it
- Throws:
Exception
-
isConfigured
public boolean isConfigured()Determines if theAuthenticationManagerBuilderis configured to build a non nullAuthenticationManager. This means that either a non-null parent is specified or at least oneAuthenticationProviderhas been specified.When using
SecurityConfigurerinstances, theAuthenticationManagerBuilderwill not be configured until theSecurityConfigurer.configure(SecurityBuilder)methods. This means aSecurityConfigurerthat is last could check this method and provide a default configuration in theSecurityConfigurer.configure(SecurityBuilder)method.- Returns:
- true, if
AuthenticationManagerBuilderis configured, otherwise false
-
getDefaultUserDetailsService
Gets the defaultUserDetailsServicefor theAuthenticationManagerBuilder. The result may be null in some circumstances.- Returns:
- the default
UserDetailsServicefor theAuthenticationManagerBuilder
-