forked from spring-projects/spring-security
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Servlet Path support to Java DSL
Closes spring-projectsgh-16430
- Loading branch information
Showing
6 changed files
with
259 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
.../org/springframework/security/web/servlet/util/matcher/ServletRequestMatcherBuilders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.servlet.util.matcher; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
import org.springframework.security.web.util.matcher.AndRequestMatcher; | ||
import org.springframework.security.web.util.matcher.RequestMatcher; | ||
import org.springframework.security.web.util.matcher.RequestMatcherBuilder; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link RequestMatcherBuilder} for specifying the servlet path separately from the | ||
* rest of the URI. This is helpful when you have more than one servlet. | ||
* | ||
* <p> | ||
* For example, if Spring MVC is deployed to `/mvc` and another servlet to `/other`, then | ||
* you can do | ||
* </p> | ||
* | ||
* <code> | ||
* http | ||
* .authorizeHttpRequests((authorize) -> authorize | ||
* .requestMatchers(servletPath("/mvc").pattern("/my/**", "/controller/**", "/endpoints/**")).hasAuthority(... | ||
* .requestMatchers(servletPath("/other").pattern("/my/**", "/non-mvc/**", "/endpoints/**")).hasAuthority(... | ||
* } | ||
* ... | ||
* </code> | ||
* | ||
* @author Josh Cummings | ||
* @since 6.5 | ||
*/ | ||
public final class ServletRequestMatcherBuilders { | ||
|
||
private static final ServletPathRequestMatcher DEFAULT_SERVLET = new ServletPathRequestMatcher(""); | ||
|
||
private ServletRequestMatcherBuilders() { | ||
} | ||
|
||
/** | ||
* Create {@link RequestMatcher}s that will only match URIs against the default | ||
* servlet. | ||
* @return a {@link ServletRequestMatcherBuilders} that matches URIs mapped to the | ||
* default servlet | ||
*/ | ||
public static RequestMatcherBuilder defaultServlet() { | ||
return servletPathInternal(DEFAULT_SERVLET, ""); | ||
} | ||
|
||
/** | ||
* Create {@link RequestMatcher}s that will only match URIs against the given servlet | ||
* path | ||
* | ||
* <p> | ||
* The path must be of the format {@code /path}. It should not end in `/` or `/*`, nor | ||
* should it be a file extension. To specify the default servlet, use | ||
* {@link #defaultServlet()}. | ||
* </p> | ||
* @return a {@link ServletRequestMatcherBuilders} that matches URIs mapped to the | ||
* given servlet path | ||
*/ | ||
public static RequestMatcherBuilder servletPath(String servletPath) { | ||
Assert.notNull(servletPath, "servletPath cannot be null"); | ||
Assert.isTrue(servletPath.startsWith("/"), "servletPath must start with '/'"); | ||
Assert.isTrue(!servletPath.endsWith("/"), "servletPath must not end with a slash"); | ||
Assert.isTrue(!servletPath.contains("*"), "servletPath must not contain a star"); | ||
return servletPathInternal(new ServletPathRequestMatcher(servletPath), servletPath); | ||
} | ||
|
||
private static RequestMatcherBuilder servletPathInternal(RequestMatcher servletPathMatcher, String servletPath) { | ||
return (method, pattern) -> { | ||
Assert.notNull(pattern, "pattern cannot be null"); | ||
Assert.isTrue(pattern.startsWith("/"), "pattern must start with '/'"); | ||
PathPatternRequestMatcher pathPattern = PathPatternRequestMatcher.pathPattern(method, | ||
servletPath + pattern); | ||
return new AndRequestMatcher(servletPathMatcher, pathPattern); | ||
}; | ||
} | ||
|
||
private record ServletPathRequestMatcher(String path) implements RequestMatcher { | ||
|
||
@Override | ||
public boolean matches(HttpServletRequest request) { | ||
return this.path.equals(request.getServletPath()); | ||
} | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
...springframework/security/web/servlet/util/matcher/ServletRequestMatcherBuildersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2002-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.security.web.servlet.util.matcher; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.http.HttpMethod; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.security.web.util.matcher.RequestMatcher; | ||
import org.springframework.security.web.util.matcher.RequestMatcherBuilder; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
/** | ||
* Tests for {@link ServletRequestMatcherBuilders} | ||
*/ | ||
class ServletRequestMatcherBuildersTests { | ||
|
||
@Test | ||
void patternWhenServletPathThenMatchesOnlyServletPath() { | ||
RequestMatcherBuilder builder = ServletRequestMatcherBuilders.servletPath("/servlet/path"); | ||
RequestMatcher requestMatcher = builder.pattern(HttpMethod.GET, "/endpoint"); | ||
assertThat(requestMatcher.matches(request("/servlet/path/endpoint", "/servlet/path"))).isTrue(); | ||
assertThat(requestMatcher.matches(request("/endpoint", ""))).isFalse(); | ||
} | ||
|
||
@Test | ||
void patternWhenDefaultServletThenMatchesOnlyDefaultServlet() { | ||
RequestMatcherBuilder builder = ServletRequestMatcherBuilders.defaultServlet(); | ||
RequestMatcher requestMatcher = builder.pattern(HttpMethod.GET, "/endpoint"); | ||
assertThat(requestMatcher.matches(request("/servlet/path/endpoint", "/servlet/path"))).isFalse(); | ||
assertThat(requestMatcher.matches(request("/endpoint", ""))).isTrue(); | ||
} | ||
|
||
@Test | ||
void servletPathWhenEndsWithSlashOrStarThenIllegalArgument() { | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> ServletRequestMatcherBuilders.servletPath("/path/**")); | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> ServletRequestMatcherBuilders.servletPath("/path/*")); | ||
assertThatExceptionOfType(IllegalArgumentException.class) | ||
.isThrownBy(() -> ServletRequestMatcherBuilders.servletPath("/path/")); | ||
} | ||
|
||
MockHttpServletRequest request(String uri, String servletPath) { | ||
MockHttpServletRequest request = new MockHttpServletRequest("GET", uri); | ||
request.setServletPath(servletPath); | ||
return request; | ||
} | ||
|
||
} |