|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 javax.servlet.FilterChain; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 28 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 29 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 30 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
| 31 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 32 | +import org.springframework.security.config.test.SpringTestContext; |
| 33 | +import org.springframework.security.config.test.SpringTestContextExtension; |
| 34 | +import org.springframework.security.web.DefaultSecurityFilterChain; |
| 35 | +import org.springframework.security.web.FilterChainProxy; |
| 36 | +import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; |
| 37 | +import org.springframework.security.web.csrf.LazyCsrfTokenRepository; |
| 38 | +import org.springframework.security.web.savedrequest.HttpSessionRequestCache; |
| 39 | + |
| 40 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 41 | +import static org.mockito.Mockito.never; |
| 42 | +import static org.mockito.Mockito.spy; |
| 43 | +import static org.mockito.Mockito.verify; |
| 44 | + |
| 45 | +@ExtendWith(SpringTestContextExtension.class) |
| 46 | +public class DeferHttpSessionJavaConfigTests { |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private FilterChainProxy springSecurityFilterChain; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private Service service; |
| 53 | + |
| 54 | + public final SpringTestContext spring = new SpringTestContext(this); |
| 55 | + |
| 56 | + @Test |
| 57 | + public void explicitDeferHttpSession() throws Exception { |
| 58 | + this.spring.register(DeferHttpSessionConfig.class).autowire(); |
| 59 | + |
| 60 | + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
| 61 | + MockHttpServletRequest mockRequest = spy(request); |
| 62 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 63 | + FilterChain chain = (httpRequest, httpResponse) -> httpResponse.getWriter().write(this.service.getMessage()); |
| 64 | + |
| 65 | + this.springSecurityFilterChain.doFilter(mockRequest, response, chain); |
| 66 | + |
| 67 | + verify(mockRequest, never()).getSession(anyBoolean()); |
| 68 | + verify(mockRequest, never()).getSession(); |
| 69 | + } |
| 70 | + |
| 71 | + @Configuration |
| 72 | + @EnableWebSecurity |
| 73 | + @EnableMethodSecurity(prePostEnabled = true) |
| 74 | + static class DeferHttpSessionConfig { |
| 75 | + |
| 76 | + @Bean |
| 77 | + Service service() { |
| 78 | + return new Service(); |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + DefaultSecurityFilterChain springSecurity(HttpSecurity http) throws Exception { |
| 83 | + LazyCsrfTokenRepository csrfRepository = new LazyCsrfTokenRepository(new HttpSessionCsrfTokenRepository()); |
| 84 | + csrfRepository.setDeferLoadToken(true); |
| 85 | + HttpSessionRequestCache requestCache = new HttpSessionRequestCache(); |
| 86 | + requestCache.setMatchingRequestParameterName("continue"); |
| 87 | + // @formatter:off |
| 88 | + http |
| 89 | + .requestCache((cache) -> cache |
| 90 | + .requestCache(requestCache) |
| 91 | + ) |
| 92 | + .securityContext((securityContext) -> securityContext |
| 93 | + .requireExplicitSave(true) |
| 94 | + ) |
| 95 | + .authorizeHttpRequests((requests) -> requests |
| 96 | + .anyRequest().permitAll() |
| 97 | + ) |
| 98 | + .sessionManagement((sessions) -> sessions |
| 99 | + .requireExplicitAuthenticationStrategy(true) |
| 100 | + ) |
| 101 | + .csrf((csrf) -> csrf |
| 102 | + .csrfRequestAttributeName("_csrf") |
| 103 | + .csrfTokenRepository(csrfRepository) |
| 104 | + ); |
| 105 | + // @formatter:on |
| 106 | + return http.build(); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + public static class Service { |
| 112 | + |
| 113 | + @PreAuthorize("permitAll") |
| 114 | + public String getMessage() { |
| 115 | + return "message"; |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments