mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-04 01:32:14 +00:00
SEC-2789: Add Default WebSecurityConfigurerAdapter
This commit is contained in:
parent
3171cc4364
commit
62e127e978
@ -62,6 +62,7 @@ final class AutowireBeanFactoryObjectPostProcessor implements ObjectPostProcesso
|
|||||||
Class<?> type = object.getClass();
|
Class<?> type = object.getClass();
|
||||||
throw new RuntimeException("Could not postProcess " + object + " of type " + type, e);
|
throw new RuntimeException("Could not postProcess " + object + " of type " + type, e);
|
||||||
}
|
}
|
||||||
|
autowireBeanFactory.autowireBean(object);
|
||||||
if(result instanceof DisposableBean) {
|
if(result instanceof DisposableBean) {
|
||||||
disposableBeans.add((DisposableBean) result);
|
disposableBeans.add((DisposableBean) result);
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ import org.springframework.core.type.AnnotationMetadata;
|
|||||||
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
||||||
import org.springframework.security.config.annotation.ObjectPostProcessor;
|
import org.springframework.security.config.annotation.ObjectPostProcessor;
|
||||||
import org.springframework.security.config.annotation.SecurityConfigurer;
|
import org.springframework.security.config.annotation.SecurityConfigurer;
|
||||||
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
|
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
|
||||||
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
import org.springframework.security.config.annotation.web.builders.WebSecurity;
|
||||||
import org.springframework.security.context.DelegatingApplicationListener;
|
import org.springframework.security.context.DelegatingApplicationListener;
|
||||||
@ -73,6 +74,9 @@ public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAwa
|
|||||||
|
|
||||||
private ClassLoader beanClassLoader;
|
private ClassLoader beanClassLoader;
|
||||||
|
|
||||||
|
@Autowired(required = false)
|
||||||
|
private ObjectPostProcessor<Object> objectObjectPostProcessor;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public static DelegatingApplicationListener delegatingApplicationListener() {
|
public static DelegatingApplicationListener delegatingApplicationListener() {
|
||||||
return new DelegatingApplicationListener();
|
return new DelegatingApplicationListener();
|
||||||
@ -93,7 +97,8 @@ public class WebSecurityConfiguration implements ImportAware, BeanClassLoaderAwa
|
|||||||
public Filter springSecurityFilterChain() throws Exception {
|
public Filter springSecurityFilterChain() throws Exception {
|
||||||
boolean hasConfigurers = webSecurityConfigurers != null && !webSecurityConfigurers.isEmpty();
|
boolean hasConfigurers = webSecurityConfigurers != null && !webSecurityConfigurers.isEmpty();
|
||||||
if(!hasConfigurers) {
|
if(!hasConfigurers) {
|
||||||
throw new IllegalStateException("At least one non-null instance of "+ WebSecurityConfigurer.class.getSimpleName()+" must be exposed as a @Bean when using @EnableWebSecurity. Hint try extending "+ WebSecurityConfigurerAdapter.class.getSimpleName());
|
WebSecurityConfigurerAdapter adapter = objectObjectPostProcessor.postProcess(new WebSecurityConfigurerAdapter() {});
|
||||||
|
webSecurity.apply(adapter);
|
||||||
}
|
}
|
||||||
return webSecurity.build();
|
return webSecurity.build();
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package org.springframework.security.config.annotation.web.configurers
|
package org.springframework.security.config.annotation.web.configurers
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanCreationException
|
import org.springframework.beans.factory.BeanCreationException
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
@ -23,6 +24,8 @@ import org.springframework.mock.web.MockFilterChain
|
|||||||
import org.springframework.mock.web.MockHttpServletRequest
|
import org.springframework.mock.web.MockHttpServletRequest
|
||||||
import org.springframework.mock.web.MockHttpServletResponse
|
import org.springframework.mock.web.MockHttpServletResponse
|
||||||
import org.springframework.security.config.annotation.BaseSpringSpec
|
import org.springframework.security.config.annotation.BaseSpringSpec
|
||||||
|
import org.springframework.security.config.annotation.ObjectPostProcessor
|
||||||
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
|
||||||
import org.springframework.security.config.annotation.web.WebSecurityConfigurer
|
import org.springframework.security.config.annotation.web.WebSecurityConfigurer
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||||
import org.springframework.security.config.annotation.web.builders.WebSecurity
|
import org.springframework.security.config.annotation.web.builders.WebSecurity
|
||||||
@ -50,32 +53,29 @@ import org.springframework.security.web.util.matcher.AnyRequestMatcher
|
|||||||
* @author Rob Winch
|
* @author Rob Winch
|
||||||
*/
|
*/
|
||||||
class DefaultFiltersTests extends BaseSpringSpec {
|
class DefaultFiltersTests extends BaseSpringSpec {
|
||||||
def missingConfigMessage = "At least one non-null instance of "+ WebSecurityConfigurer.class.getSimpleName()+" must be exposed as a @Bean when using @EnableWebSecurity. Hint try extending "+ WebSecurityConfigurerAdapter.class.getSimpleName()
|
|
||||||
|
|
||||||
def "DefaultSecurityFilterChainBuilder cannot be null"() {
|
def "Default the WebSecurityConfigurerAdapter"() {
|
||||||
when:
|
when:
|
||||||
context = new AnnotationConfigApplicationContext(FilterChainProxyBuilderMissingConfig)
|
context = new AnnotationConfigApplicationContext(FilterChainProxyBuilderMissingConfig)
|
||||||
then:
|
then:
|
||||||
BeanCreationException e = thrown()
|
context.getBean(FilterChainProxy) != null
|
||||||
e.message.contains missingConfigMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
static class FilterChainProxyBuilderMissingConfig { }
|
static class FilterChainProxyBuilderMissingConfig {
|
||||||
|
@Autowired
|
||||||
def "FilterChainProxyBuilder no DefaultSecurityFilterChainBuilder specified"() {
|
public void configureGlobal(AuthenticationManagerBuilder auth) {
|
||||||
when:
|
auth
|
||||||
context = new AnnotationConfigApplicationContext(FilterChainProxyBuilderNoSecurityFilterBuildersConfig)
|
.inMemoryAuthentication()
|
||||||
then:
|
.withUser("user").password("password").roles("USER")
|
||||||
BeanCreationException e = thrown()
|
}
|
||||||
e.message.contains missingConfigMessage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
static class FilterChainProxyBuilderNoSecurityFilterBuildersConfig {
|
static class FilterChainProxyBuilderNoSecurityFilterBuildersConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public WebSecurity filterChainProxyBuilder() {
|
public WebSecurity filterChainProxyBuilder(ObjectPostProcessor<Object> opp) {
|
||||||
new WebSecurity()
|
new WebSecurity(opp)
|
||||||
.ignoring()
|
.ignoring()
|
||||||
.antMatchers("/resources/**")
|
.antMatchers("/resources/**")
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,10 @@ package org.springframework.security.samples.config;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
|
||||||
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
|
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
|
||||||
|
|
||||||
@EnableWebMvcSecurity
|
@EnableWebMvcSecurity
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void registerGlobalAuthentication(
|
public void registerGlobalAuthentication(
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package org.springframework.security.samples.config;
|
package org.springframework.security.samples.config;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.security.config.annotation.authentication.builders.* ;
|
import org.springframework.security.config.annotation.authentication.builders.* ;
|
||||||
import org.springframework.security.config.annotation.web.configuration.*;
|
import org.springframework.security.config.annotation.web.configuration.*;
|
||||||
|
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user