|
@@ -16,9 +16,6 @@
|
|
|
|
|
|
package org.springframework.security.config.annotation.web.configurers;
|
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
-import java.lang.reflect.Modifier;
|
|
|
-
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
@@ -34,13 +31,13 @@ import org.springframework.mock.web.MockFilterChain;
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
import org.springframework.mock.web.MockServletContext;
|
|
|
-import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
|
import org.springframework.security.core.userdetails.User;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
|
|
+import org.springframework.security.web.DefaultSecurityFilterChain;
|
|
|
import org.springframework.security.web.FilterChainProxy;
|
|
|
import org.springframework.security.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
|
|
@@ -78,7 +75,6 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
this.request.setMethod("GET");
|
|
|
this.response = new MockHttpServletResponse();
|
|
|
this.chain = new MockFilterChain();
|
|
|
- mockMvcPresentClasspath(true);
|
|
|
}
|
|
|
|
|
|
@AfterEach
|
|
@@ -104,23 +100,6 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void securityMatcherWhenNoMvcThenAntMatcher() throws Exception {
|
|
|
- mockMvcPresentClasspath(false);
|
|
|
- loadConfig(SecurityMatcherNoMvcConfig.class, LegacyMvcMatchingConfig.class);
|
|
|
- this.request.setServletPath("/path");
|
|
|
- this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
- assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
- setup();
|
|
|
- this.request.setServletPath("/path.html");
|
|
|
- this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
- assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
|
|
- setup();
|
|
|
- this.request.setServletPath("/path/");
|
|
|
- this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
- assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
public void securityMatcherWhenMvcMatcherAndGetFiltersNoUnsupportedMethodExceptionFromDummyRequest() {
|
|
|
loadConfig(SecurityMatcherMvcConfig.class);
|
|
@@ -141,6 +120,9 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
this.request.setServletPath("/path/");
|
|
|
this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
+ assertThat(this.springSecurityFilterChain.getFilterChains())
|
|
|
+ .extracting((c) -> ((DefaultSecurityFilterChain) c).getRequestMatcher())
|
|
|
+ .hasOnlyElementsOfType(MvcRequestMatcher.class);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -237,20 +219,6 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
this.context.getAutowireCapableBeanFactory().autowireBean(this);
|
|
|
}
|
|
|
|
|
|
- private void mockMvcPresentClasspath(Object newValue) throws Exception {
|
|
|
- mockMvcPresentClasspath(HttpSecurity.class, newValue);
|
|
|
- mockMvcPresentClasspath(AbstractRequestMatcherRegistry.class, newValue);
|
|
|
- }
|
|
|
-
|
|
|
- private void mockMvcPresentClasspath(Class<?> clazz, Object newValue) throws Exception {
|
|
|
- Field mvcPresentField = clazz.getDeclaredField("mvcPresent");
|
|
|
- mvcPresentField.setAccessible(true);
|
|
|
- Field modifiersField = Field.class.getDeclaredField("modifiers");
|
|
|
- modifiersField.setAccessible(true);
|
|
|
- modifiersField.setInt(mvcPresentField, mvcPresentField.getModifiers() & ~Modifier.FINAL);
|
|
|
- mvcPresentField.set(null, newValue);
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
@Configuration
|
|
|
@EnableWebMvc
|
|
@@ -376,35 +344,6 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
|
|
|
}
|
|
|
|
|
|
- @EnableWebSecurity
|
|
|
- @Configuration
|
|
|
- @Import(UsersConfig.class)
|
|
|
- static class SecurityMatcherNoMvcConfig {
|
|
|
-
|
|
|
- @Bean
|
|
|
- SecurityFilterChain appSecurity(HttpSecurity http) throws Exception {
|
|
|
- // @formatter:off
|
|
|
- http
|
|
|
- .securityMatcher("/path")
|
|
|
- .httpBasic().and()
|
|
|
- .authorizeHttpRequests()
|
|
|
- .anyRequest().denyAll();
|
|
|
- // @formatter:on
|
|
|
- return http.build();
|
|
|
- }
|
|
|
-
|
|
|
- @RestController
|
|
|
- static class PathController {
|
|
|
-
|
|
|
- @RequestMapping("/path")
|
|
|
- String path() {
|
|
|
- return "path";
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
@EnableWebSecurity
|
|
|
@Configuration
|
|
|
@EnableWebMvc
|
|
@@ -415,9 +354,7 @@ public class HttpSecuritySecurityMatchersTests {
|
|
|
SecurityFilterChain appSecurity(HttpSecurity http) throws Exception {
|
|
|
// @formatter:off
|
|
|
http
|
|
|
- .securityMatchers()
|
|
|
- .requestMatchers("/path")
|
|
|
- .and()
|
|
|
+ .securityMatcher("/path")
|
|
|
.httpBasic().and()
|
|
|
.authorizeHttpRequests()
|
|
|
.anyRequest().denyAll();
|