|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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.configurers; |
| 18 | + |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Configuration; |
| 24 | +import org.springframework.security.config.annotation.ObjectPostProcessor; |
| 25 | +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
| 26 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 27 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 28 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 29 | +import org.springframework.security.config.test.SpringTestRule; |
| 30 | +import org.springframework.security.core.context.SecurityContext; |
| 31 | +import org.springframework.security.web.context.HttpRequestResponseHolder; |
| 32 | +import org.springframework.security.web.context.SecurityContextPersistenceFilter; |
| 33 | +import org.springframework.security.web.context.SecurityContextRepository; |
| 34 | +import org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter; |
| 35 | +import org.springframework.test.web.servlet.MockMvc; |
| 36 | + |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.mockito.Mockito.*; |
| 39 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for {@link SecurityContextConfigurer} |
| 43 | + * |
| 44 | + * @author Rob Winch |
| 45 | + * @author Eleftheria Stein |
| 46 | + */ |
| 47 | +public class SecurityContextConfigurerTests { |
| 48 | + @Rule |
| 49 | + public final SpringTestRule spring = new SpringTestRule(); |
| 50 | + |
| 51 | + @Autowired |
| 52 | + MockMvc mvc; |
| 53 | + |
| 54 | + @Test |
| 55 | + public void configureWhenRegisteringObjectPostProcessorThenInvokedOnSecurityContextPersistenceFilter() { |
| 56 | + this.spring.register(ObjectPostProcessorConfig.class).autowire(); |
| 57 | + |
| 58 | + verify(ObjectPostProcessorConfig.objectPostProcessor) |
| 59 | + .postProcess(any(SecurityContextPersistenceFilter.class)); |
| 60 | + } |
| 61 | + |
| 62 | + @EnableWebSecurity |
| 63 | + static class ObjectPostProcessorConfig extends WebSecurityConfigurerAdapter { |
| 64 | + static ObjectPostProcessor<Object> objectPostProcessor = spy(ReflectingObjectPostProcessor.class); |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void configure(HttpSecurity http) throws Exception { |
| 68 | + // @formatter:off |
| 69 | + http |
| 70 | + .securityContext(); |
| 71 | + // @formatter:on |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + static ObjectPostProcessor<Object> objectPostProcessor() { |
| 76 | + return objectPostProcessor; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + static class ReflectingObjectPostProcessor implements ObjectPostProcessor<Object> { |
| 81 | + @Override |
| 82 | + public <O> O postProcess(O object) { |
| 83 | + return object; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void securityContextWhenInvokedTwiceThenUsesOriginalSecurityContextRepository() throws Exception { |
| 89 | + this.spring.register(DuplicateDoesNotOverrideConfig.class).autowire(); |
| 90 | + when(DuplicateDoesNotOverrideConfig.SCR.loadContext(any())).thenReturn(mock(SecurityContext.class)); |
| 91 | + |
| 92 | + this.mvc.perform(get("/")); |
| 93 | + |
| 94 | + verify(DuplicateDoesNotOverrideConfig.SCR) |
| 95 | + .loadContext(any(HttpRequestResponseHolder.class)); |
| 96 | + } |
| 97 | + |
| 98 | + @EnableWebSecurity |
| 99 | + static class DuplicateDoesNotOverrideConfig extends WebSecurityConfigurerAdapter { |
| 100 | + static SecurityContextRepository SCR = mock(SecurityContextRepository.class); |
| 101 | + |
| 102 | + @Override |
| 103 | + protected void configure(HttpSecurity http) throws Exception { |
| 104 | + // @formatter:off |
| 105 | + http |
| 106 | + .securityContext() |
| 107 | + .securityContextRepository(SCR) |
| 108 | + .and() |
| 109 | + .securityContext(); |
| 110 | + // @formatter:on |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + //SEC-2932 |
| 115 | + @Test |
| 116 | + public void securityContextWhenSecurityContextRepositoryNotConfiguredThenDoesNotThrowException() throws Exception { |
| 117 | + this.spring.register(SecurityContextRepositoryDefaultsSecurityContextRepositoryConfig.class).autowire(); |
| 118 | + |
| 119 | + this.mvc.perform(get("/")); |
| 120 | + } |
| 121 | + |
| 122 | + @Configuration |
| 123 | + @EnableWebSecurity |
| 124 | + static class SecurityContextRepositoryDefaultsSecurityContextRepositoryConfig extends WebSecurityConfigurerAdapter { |
| 125 | + public SecurityContextRepositoryDefaultsSecurityContextRepositoryConfig() { |
| 126 | + super(true); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + protected void configure(HttpSecurity http) throws Exception { |
| 131 | + // @formatter:off |
| 132 | + http |
| 133 | + .addFilter(new WebAsyncManagerIntegrationFilter()) |
| 134 | + .anonymous() |
| 135 | + .and() |
| 136 | + .securityContext() |
| 137 | + .and() |
| 138 | + .authorizeRequests() |
| 139 | + .anyRequest().permitAll() |
| 140 | + .and() |
| 141 | + .httpBasic(); |
| 142 | + // @formatter:on |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
| 147 | + // @formatter:off |
| 148 | + auth |
| 149 | + .inMemoryAuthentication() |
| 150 | + .withUser("user").password("password").roles("USER"); |
| 151 | + // @formatter:on |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments