|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.annotation.web.configuration; |
| 18 | + |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.context.ApplicationContext; |
| 21 | +import org.springframework.context.annotation.Bean; |
| 22 | +import org.springframework.context.annotation.Configuration; |
| 23 | +import org.springframework.context.annotation.Scope; |
| 24 | +import org.springframework.security.authentication.AuthenticationManager; |
| 25 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
| 26 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 27 | +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; |
| 28 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 29 | +import org.springframework.security.config.annotation.web.configurers.DefaultLoginPageConfigurer; |
| 30 | +import org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter; |
| 31 | + |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.springframework.security.config.Customizer.withDefaults; |
| 36 | + |
| 37 | +/** |
| 38 | + * {@link Configuration} that exposes the {@link HttpSecurity} bean. |
| 39 | + * |
| 40 | + * @author Eleftheria Stein |
| 41 | + * @since 5.4 |
| 42 | + */ |
| 43 | +@Configuration(proxyBeanMethods = false) |
| 44 | +class HttpSecurityConfiguration { |
| 45 | + private static final String BEAN_NAME_PREFIX = "org.springframework.security.config.annotation.web.configuration.HttpSecurityConfiguration."; |
| 46 | + private static final String HTTPSECURITY_BEAN_NAME = BEAN_NAME_PREFIX + "httpSecurity"; |
| 47 | + |
| 48 | + private ObjectPostProcessor<Object> objectPostProcessor; |
| 49 | + |
| 50 | + private AuthenticationManager authenticationManager; |
| 51 | + |
| 52 | + private AuthenticationConfiguration authenticationConfiguration; |
| 53 | + |
| 54 | + private ApplicationContext context; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcessor) { |
| 58 | + this.objectPostProcessor = objectPostProcessor; |
| 59 | + } |
| 60 | + |
| 61 | + @Autowired(required = false) |
| 62 | + void setAuthenticationManager(AuthenticationManager authenticationManager) { |
| 63 | + this.authenticationManager = authenticationManager; |
| 64 | + } |
| 65 | + |
| 66 | + @Autowired |
| 67 | + public void setAuthenticationConfiguration( |
| 68 | + AuthenticationConfiguration authenticationConfiguration) { |
| 69 | + this.authenticationConfiguration = authenticationConfiguration; |
| 70 | + } |
| 71 | + |
| 72 | + @Autowired |
| 73 | + public void setApplicationContext(ApplicationContext context) { |
| 74 | + this.context = context; |
| 75 | + } |
| 76 | + |
| 77 | + @Bean(HTTPSECURITY_BEAN_NAME) |
| 78 | + @Scope("prototype") |
| 79 | + public HttpSecurity httpSecurity() throws Exception { |
| 80 | + WebSecurityConfigurerAdapter.LazyPasswordEncoder passwordEncoder = |
| 81 | + new WebSecurityConfigurerAdapter.LazyPasswordEncoder(this.context); |
| 82 | + |
| 83 | + AuthenticationManagerBuilder authenticationBuilder = |
| 84 | + new WebSecurityConfigurerAdapter.DefaultPasswordEncoderAuthenticationManagerBuilder(this.objectPostProcessor, passwordEncoder); |
| 85 | + authenticationBuilder.parentAuthenticationManager(authenticationManager()); |
| 86 | + |
| 87 | + HttpSecurity http = new HttpSecurity(objectPostProcessor, authenticationBuilder, createSharedObjects()); |
| 88 | + http |
| 89 | + .csrf(withDefaults()) |
| 90 | + .addFilter(new WebAsyncManagerIntegrationFilter()) |
| 91 | + .exceptionHandling(withDefaults()) |
| 92 | + .headers(withDefaults()) |
| 93 | + .sessionManagement(withDefaults()) |
| 94 | + .securityContext(withDefaults()) |
| 95 | + .requestCache(withDefaults()) |
| 96 | + .anonymous(withDefaults()) |
| 97 | + .servletApi(withDefaults()) |
| 98 | + .logout(withDefaults()) |
| 99 | + .apply(new DefaultLoginPageConfigurer<>()); |
| 100 | + |
| 101 | + return http; |
| 102 | + } |
| 103 | + |
| 104 | + private AuthenticationManager authenticationManager() throws Exception { |
| 105 | + if (this.authenticationManager != null) { |
| 106 | + return this.authenticationManager; |
| 107 | + } else { |
| 108 | + return this.authenticationConfiguration.getAuthenticationManager(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private Map<Class<?>, Object> createSharedObjects() { |
| 113 | + Map<Class<?>, Object> sharedObjects = new HashMap<>(); |
| 114 | + sharedObjects.put(ApplicationContext.class, context); |
| 115 | + return sharedObjects; |
| 116 | + } |
| 117 | +} |
0 commit comments