|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
- * Copyright 2002-2016 the original author or authors.
|
|
|
|
|
|
+ * Copyright 2002-2019 the original author or authors.
|
|
*
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* you may not use this file except in compliance with the License.
|
|
@@ -38,6 +38,7 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
+import static org.springframework.security.config.Customizer.withDefaults;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
@@ -195,6 +196,62 @@ public class HttpSecurityRequestMatchersTests {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void requestMatchersWhenMvcMatcherInLambdaThenPathIsSecured() throws Exception {
|
|
|
|
+ loadConfig(RequestMatchersMvcMatcherInLambdaConfig.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_UNAUTHORIZED);
|
|
|
|
+
|
|
|
|
+ setup();
|
|
|
|
+
|
|
|
|
+ this.request.setServletPath("/path/");
|
|
|
|
+ this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
|
+
|
|
|
|
+ assertThat(this.response.getStatus())
|
|
|
|
+ .isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @EnableWebSecurity
|
|
|
|
+ @Configuration
|
|
|
|
+ @EnableWebMvc
|
|
|
|
+ static class RequestMatchersMvcMatcherInLambdaConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
+ @Override
|
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
|
+ // @formatter:off
|
|
|
|
+ http
|
|
|
|
+ .requestMatchers(requestMatchers ->
|
|
|
|
+ requestMatchers
|
|
|
|
+ .mvcMatchers("/path")
|
|
|
|
+ )
|
|
|
|
+ .httpBasic(withDefaults())
|
|
|
|
+ .authorizeRequests(authorizeRequests ->
|
|
|
|
+ authorizeRequests
|
|
|
|
+ .anyRequest().denyAll()
|
|
|
|
+ );
|
|
|
|
+ // @formatter:on
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RestController
|
|
|
|
+ static class PathController {
|
|
|
|
+ @RequestMapping("/path")
|
|
|
|
+ public String path() {
|
|
|
|
+ return "path";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void requestMatchersMvcMatcherServletPath() throws Exception {
|
|
public void requestMatchersMvcMatcherServletPath() throws Exception {
|
|
loadConfig(RequestMatchersMvcMatcherServeltPathConfig.class);
|
|
loadConfig(RequestMatchersMvcMatcherServeltPathConfig.class);
|
|
@@ -260,6 +317,66 @@ public class HttpSecurityRequestMatchersTests {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void requestMatcherWhensMvcMatcherServletPathInLambdaThenPathIsSecured() throws Exception {
|
|
|
|
+ loadConfig(RequestMatchersMvcMatcherServletPathInLambdaConfig.class);
|
|
|
|
+
|
|
|
|
+ this.request.setServletPath("/spring");
|
|
|
|
+ this.request.setRequestURI("/spring/path");
|
|
|
|
+ this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
|
+
|
|
|
|
+ assertThat(this.response.getStatus())
|
|
|
|
+ .isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
|
+
|
|
|
|
+ setup();
|
|
|
|
+
|
|
|
|
+ this.request.setServletPath("");
|
|
|
|
+ this.request.setRequestURI("/path");
|
|
|
|
+ this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
|
+
|
|
|
|
+ assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
|
|
|
+
|
|
|
|
+ setup();
|
|
|
|
+
|
|
|
|
+ this.request.setServletPath("/other");
|
|
|
|
+ this.request.setRequestURI("/other/path");
|
|
|
|
+
|
|
|
|
+ this.springSecurityFilterChain.doFilter(this.request, this.response, this.chain);
|
|
|
|
+
|
|
|
|
+ assertThat(this.response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @EnableWebSecurity
|
|
|
|
+ @Configuration
|
|
|
|
+ @EnableWebMvc
|
|
|
|
+ static class RequestMatchersMvcMatcherServletPathInLambdaConfig
|
|
|
|
+ extends WebSecurityConfigurerAdapter {
|
|
|
|
+ @Override
|
|
|
|
+ protected void configure(HttpSecurity http) throws Exception {
|
|
|
|
+ // @formatter:off
|
|
|
|
+ http
|
|
|
|
+ .requestMatchers(requestMatchers ->
|
|
|
|
+ requestMatchers
|
|
|
|
+ .mvcMatchers("/path").servletPath("/spring")
|
|
|
|
+ .mvcMatchers("/never-match")
|
|
|
|
+ )
|
|
|
|
+ .httpBasic(withDefaults())
|
|
|
|
+ .authorizeRequests(authorizeRequests ->
|
|
|
|
+ authorizeRequests
|
|
|
|
+ .anyRequest().denyAll()
|
|
|
|
+ );
|
|
|
|
+ // @formatter:on
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RestController
|
|
|
|
+ static class PathController {
|
|
|
|
+ @RequestMapping("/path")
|
|
|
|
+ public String path() {
|
|
|
|
+ return "path";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public void loadConfig(Class<?>... configs) {
|
|
public void loadConfig(Class<?>... configs) {
|
|
this.context = new AnnotationConfigWebApplicationContext();
|
|
this.context = new AnnotationConfigWebApplicationContext();
|
|
this.context.register(configs);
|
|
this.context.register(configs);
|