|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.security.servlet;
|
18 | 18 |
|
| 19 | +import java.util.EnumSet; |
| 20 | + |
| 21 | +import javax.servlet.DispatcherType; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
19 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
20 | 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
|
21 | 28 | import org.springframework.boot.autoconfigure.security.ConditionalOnDefaultWebSecurity;
|
22 | 29 | import org.springframework.boot.autoconfigure.security.SecurityProperties;
|
| 30 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 31 | +import org.springframework.boot.web.servlet.filter.ErrorPageSecurityFilter; |
| 32 | +import org.springframework.context.ApplicationContext; |
23 | 33 | import org.springframework.context.annotation.Bean;
|
24 | 34 | import org.springframework.context.annotation.Configuration;
|
25 | 35 | import org.springframework.core.annotation.Order;
|
| 36 | +import org.springframework.security.config.BeanIds; |
26 | 37 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
| 38 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 39 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
27 | 40 | import org.springframework.security.web.SecurityFilterChain;
|
| 41 | +import org.springframework.security.web.access.WebInvocationPrivilegeEvaluator; |
28 | 42 |
|
29 | 43 | /**
|
30 |
| - * The default configuration for web security. It relies on Spring Security's |
31 |
| - * content-negotiation strategy to determine what sort of authentication to use. If the |
32 |
| - * user specifies their own |
33 |
| - * {@link org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter} |
34 |
| - * or {@link SecurityFilterChain} bean, this will back-off completely and the users should |
35 |
| - * specify all the bits that they want to configure as part of the custom security |
36 |
| - * configuration. |
| 44 | + * {@link Configuration @Configuration} class securing servlet applications. |
37 | 45 | *
|
38 | 46 | * @author Madhura Bhave
|
39 | 47 | */
|
40 | 48 | @Configuration(proxyBeanMethods = false)
|
41 |
| -@ConditionalOnDefaultWebSecurity |
42 | 49 | @ConditionalOnWebApplication(type = Type.SERVLET)
|
43 | 50 | @SuppressWarnings("deprecation")
|
44 | 51 | class SpringBootWebSecurityConfiguration {
|
45 | 52 |
|
46 |
| - @Bean |
47 |
| - @Order(SecurityProperties.BASIC_AUTH_ORDER) |
48 |
| - SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { |
49 |
| - http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic(); |
50 |
| - return http.build(); |
| 53 | + /** |
| 54 | + * The default configuration for web security. It relies on Spring Security's |
| 55 | + * content-negotiation strategy to determine what sort of authentication to use. If |
| 56 | + * the user specifies their own {@link WebSecurityConfigurerAdapter} or |
| 57 | + * {@link SecurityFilterChain} bean, this will back-off completely and the users |
| 58 | + * should specify all the bits that they want to configure as part of the custom |
| 59 | + * security configuration. |
| 60 | + */ |
| 61 | + @Configuration(proxyBeanMethods = false) |
| 62 | + @ConditionalOnDefaultWebSecurity |
| 63 | + static class SecurityFilterChainConfiguration { |
| 64 | + |
| 65 | + @Bean |
| 66 | + @Order(SecurityProperties.BASIC_AUTH_ORDER) |
| 67 | + SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { |
| 68 | + http.authorizeRequests().anyRequest().authenticated().and().formLogin().and().httpBasic(); |
| 69 | + return http.build(); |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Configures the {@link ErrorPageSecurityFilter}. |
| 76 | + */ |
| 77 | + @Configuration(proxyBeanMethods = false) |
| 78 | + @ConditionalOnClass(WebInvocationPrivilegeEvaluator.class) |
| 79 | + @ConditionalOnBean(WebInvocationPrivilegeEvaluator.class) |
| 80 | + static class ErrorPageSecurityFilterConfiguration { |
| 81 | + |
| 82 | + @Bean |
| 83 | + FilterRegistrationBean<ErrorPageSecurityFilter> errorPageSecurityFilter(ApplicationContext context) { |
| 84 | + FilterRegistrationBean<ErrorPageSecurityFilter> registration = new FilterRegistrationBean<>( |
| 85 | + new ErrorPageSecurityFilter(context)); |
| 86 | + registration.setDispatcherTypes(EnumSet.of(DispatcherType.ERROR)); |
| 87 | + return registration; |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Adds the{@link EnableWebSecurity @EnableWebSecurity} annotation if Spring Security |
| 94 | + * is on the classpath. This will make sure that the annotation is present with |
| 95 | + * default security auto-configuration and also if the user adds custom security and |
| 96 | + * forgets to add the annotation. If {@link EnableWebSecurity @EnableWebSecurity} has |
| 97 | + * already been added or if a bean with name |
| 98 | + * {@value BeanIds#SPRING_SECURITY_FILTER_CHAIN} has been configured by the user, this |
| 99 | + * will back-off. |
| 100 | + */ |
| 101 | + @Configuration(proxyBeanMethods = false) |
| 102 | + @ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN) |
| 103 | + @ConditionalOnClass(EnableWebSecurity.class) |
| 104 | + @EnableWebSecurity |
| 105 | + static class WebSecurityEnablerConfiguration { |
| 106 | + |
51 | 107 | }
|
52 | 108 |
|
53 | 109 | }
|
0 commit comments