Class WebSecurityConfigurerAdapter
- java.lang.Object
-
- org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
-
- All Implemented Interfaces:
SecurityConfigurer<javax.servlet.Filter,WebSecurity>,WebSecurityConfigurer<WebSecurity>
@Order(100) public abstract class WebSecurityConfigurerAdapter extends java.lang.Object implements WebSecurityConfigurer<WebSecurity>
Provides a convenient base class for creating aWebSecurityConfigurerinstance. The implementation allows customization by overriding methods.Will automatically apply the result of looking up
AbstractHttpConfigurerfromSpringFactoriesLoaderto allow developers to extend the defaults. To do this, you must create a class that extends AbstractHttpConfigurer and then create a file in the classpath at "META-INF/spring.factories" that looks something like:org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = sample.MyClassThatExtendsAbstractHttpConfigurer
If you have multiple classes that should be added you can use "," to separate the values. For example:org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer = sample.MyClassThatExtendsAbstractHttpConfigurer, sample.OtherThatExtendsAbstractHttpConfigurer
- See Also:
EnableWebSecurity
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedWebSecurityConfigurerAdapter()Creates an instance with the default configuration enabled.protectedWebSecurityConfigurerAdapter(boolean disableDefaults)Creates an instance which allows specifying if the default configuration should be enabled.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected org.springframework.security.authentication.AuthenticationManagerauthenticationManager()Gets theAuthenticationManagerto use.org.springframework.security.authentication.AuthenticationManagerauthenticationManagerBean()Override this method to expose theAuthenticationManagerfromconfigure(AuthenticationManagerBuilder)to be exposed as a Bean.protected voidconfigure(AuthenticationManagerBuilder auth)Used by the default implementation ofauthenticationManager()to attempt to obtain anAuthenticationManager.protected voidconfigure(HttpSecurity http)Override this method to configure theHttpSecurity.voidconfigure(WebSecurity web)Override this method to configureWebSecurity.protected org.springframework.context.ApplicationContextgetApplicationContext()Gets the ApplicationContextprotected HttpSecuritygetHttp()Creates theHttpSecurityor returns the current instancevoidinit(WebSecurity web)Initialize theSecurityBuilder.voidsetApplicationContext(org.springframework.context.ApplicationContext context)voidsetAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration)voidsetContentNegotationStrategy(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy)voidsetObjectPostProcessor(ObjectPostProcessor<java.lang.Object> objectPostProcessor)voidsetTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver trustResolver)protected org.springframework.security.core.userdetails.UserDetailsServiceuserDetailsService()Allows modifying and accessing theUserDetailsServicefromuserDetailsServiceBean()without interacting with theApplicationContext.org.springframework.security.core.userdetails.UserDetailsServiceuserDetailsServiceBean()Override this method to expose aUserDetailsServicecreated fromconfigure(AuthenticationManagerBuilder)as a bean.
-
-
-
Constructor Detail
-
WebSecurityConfigurerAdapter
protected WebSecurityConfigurerAdapter()
Creates an instance with the default configuration enabled.
-
WebSecurityConfigurerAdapter
protected WebSecurityConfigurerAdapter(boolean disableDefaults)
Creates an instance which allows specifying if the default configuration should be enabled. Disabling the default configuration should be considered more advanced usage as it requires more understanding of how the framework is implemented.- Parameters:
disableDefaults- true if the default configuration should be disabled, else false
-
-
Method Detail
-
configure
protected void configure(AuthenticationManagerBuilder auth) throws java.lang.Exception
Used by the default implementation ofauthenticationManager()to attempt to obtain anAuthenticationManager. If overridden, theAuthenticationManagerBuildershould be used to specify theAuthenticationManager.The
authenticationManagerBean()method can be used to expose the resultingAuthenticationManageras a Bean. TheuserDetailsServiceBean()can be used to expose the last populatedUserDetailsServicethat is created with theAuthenticationManagerBuilderas a Bean. TheUserDetailsServicewill also automatically be populated onAbstractConfiguredSecurityBuilder.getSharedObject(Class)for use with otherSecurityContextConfigurer(i.e. RememberMeConfigurer )For example, the following configuration could be used to register in memory authentication that exposes an in memory
UserDetailsService:@Override protected void configure(AuthenticationManagerBuilder auth) { auth // enable in memory based authentication with a user named // "user" and "admin" .inMemoryAuthentication().withUser("user").password("password").roles("USER").and() .withUser("admin").password("password").roles("USER", "ADMIN"); } // Expose the UserDetailsService as a Bean @Bean @Override public UserDetailsService userDetailsServiceBean() throws Exception { return super.userDetailsServiceBean(); }- Parameters:
auth- theAuthenticationManagerBuilderto use- Throws:
java.lang.Exception
-
getHttp
protected final HttpSecurity getHttp() throws java.lang.Exception
Creates theHttpSecurityor returns the current instance- Returns:
- the
HttpSecurity - Throws:
java.lang.Exception
-
authenticationManagerBean
public org.springframework.security.authentication.AuthenticationManager authenticationManagerBean() throws java.lang.ExceptionOverride this method to expose theAuthenticationManagerfromconfigure(AuthenticationManagerBuilder)to be exposed as a Bean. For example:@Bean(name name="myAuthenticationManager") @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }- Returns:
- the
AuthenticationManager - Throws:
java.lang.Exception
-
authenticationManager
protected org.springframework.security.authentication.AuthenticationManager authenticationManager() throws java.lang.ExceptionGets theAuthenticationManagerto use. The default strategy is ifconfigure(AuthenticationManagerBuilder)method is overridden to use theAuthenticationManagerBuilderthat was passed in. Otherwise, autowire theAuthenticationManagerby type.- Returns:
- the
AuthenticationManagerto use - Throws:
java.lang.Exception
-
userDetailsServiceBean
public org.springframework.security.core.userdetails.UserDetailsService userDetailsServiceBean() throws java.lang.ExceptionOverride this method to expose aUserDetailsServicecreated fromconfigure(AuthenticationManagerBuilder)as a bean. In general only the following override should be done of this method:@Bean(name = "myUserDetailsService") // any or no name specified is allowed @Override public UserDetailsService userDetailsServiceBean() throws Exception { return super.userDetailsServiceBean(); }To change the instance returned, developers should changeuserDetailsService()instead- Returns:
- the
UserDetailsService - Throws:
java.lang.Exception- See Also:
userDetailsService()
-
userDetailsService
protected org.springframework.security.core.userdetails.UserDetailsService userDetailsService()
Allows modifying and accessing theUserDetailsServicefromuserDetailsServiceBean()without interacting with theApplicationContext. Developers should override this method when changing the instance ofuserDetailsServiceBean().- Returns:
- the
UserDetailsServiceto use
-
init
public void init(WebSecurity web) throws java.lang.Exception
Description copied from interface:SecurityConfigurerInitialize theSecurityBuilder. Here only shared state should be created and modified, but not properties on theSecurityBuilderused for building the object. This ensures that theSecurityConfigurer.configure(SecurityBuilder)method uses the correct shared objects when building. Configurers should be applied here.- Specified by:
initin interfaceSecurityConfigurer<javax.servlet.Filter,WebSecurity>- Throws:
java.lang.Exception
-
configure
public void configure(WebSecurity web) throws java.lang.Exception
Override this method to configureWebSecurity. For example, if you wish to ignore certain requests. Endpoints specified in this method will be ignored by Spring Security, meaning it will not protect them from CSRF, XSS, Clickjacking, and so on. Instead, if you want to protect endpoints against common vulnerabilities, then seeconfigure(HttpSecurity)and theHttpSecurity.authorizeRequests()configuration method.- Specified by:
configurein interfaceSecurityConfigurer<javax.servlet.Filter,WebSecurity>- Throws:
java.lang.Exception
-
configure
protected void configure(HttpSecurity http) throws java.lang.Exception
Override this method to configure theHttpSecurity. Typically subclasses should not invoke this method by calling super as it may override their configuration. The default configuration is:http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic();
Any endpoint that requires defense against common vulnerabilities can be specified here, including public ones. SeeHttpSecurity.authorizeRequests()and the `permitAll()` authorization rule for more details on public endpoints.- Parameters:
http- theHttpSecurityto modify- Throws:
java.lang.Exception- if an error occurs
-
getApplicationContext
protected final org.springframework.context.ApplicationContext getApplicationContext()
Gets the ApplicationContext- Returns:
- the context
-
setApplicationContext
@Autowired public void setApplicationContext(org.springframework.context.ApplicationContext context)
-
setTrustResolver
@Autowired(required=false) public void setTrustResolver(org.springframework.security.authentication.AuthenticationTrustResolver trustResolver)
-
setContentNegotationStrategy
@Autowired(required=false) public void setContentNegotationStrategy(org.springframework.web.accept.ContentNegotiationStrategy contentNegotiationStrategy)
-
setObjectPostProcessor
@Autowired public void setObjectPostProcessor(ObjectPostProcessor<java.lang.Object> objectPostProcessor)
-
setAuthenticationConfiguration
@Autowired public void setAuthenticationConfiguration(AuthenticationConfiguration authenticationConfiguration)
-
-