|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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.web.servlet.util.matcher; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.LinkedHashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +import jakarta.servlet.RequestDispatcher; |
| 26 | +import jakarta.servlet.ServletContext; |
| 27 | +import jakarta.servlet.ServletRegistration; |
| 28 | +import jakarta.servlet.http.HttpServletMapping; |
| 29 | +import jakarta.servlet.http.HttpServletRequest; |
| 30 | +import jakarta.servlet.http.MappingMatch; |
| 31 | + |
| 32 | +import org.springframework.lang.Nullable; |
| 33 | +import org.springframework.security.web.util.matcher.AndRequestMatcher; |
| 34 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 35 | +import org.springframework.security.web.util.matcher.RequestMatcherBuilder; |
| 36 | +import org.springframework.util.Assert; |
| 37 | +import org.springframework.util.ObjectUtils; |
| 38 | +import org.springframework.web.util.UriUtils; |
| 39 | +import org.springframework.web.util.WebUtils; |
| 40 | + |
| 41 | +/** |
| 42 | + * A {@link RequestMatcherBuilder} for specifying the servlet path separately from the |
| 43 | + * rest of the URI. This is helpful when you have more than one servlet. |
| 44 | + * |
| 45 | + * <p> |
| 46 | + * For example, if Spring MVC is deployed to `/mvc` and another servlet to `/other`, then |
| 47 | + * you can do |
| 48 | + * </p> |
| 49 | + * |
| 50 | + * <code> |
| 51 | + * http |
| 52 | + * .authorizeHttpRequests((authorize) -> authorize |
| 53 | + * .requestMatchers(servletPath("/mvc").pattern("/my/**", "/controller/**", "/endpoints/**")).hasAuthority(... |
| 54 | + * .requestMatchers(servletPath("/other").pattern("/my/**", "/non-mvc/**", "/endpoints/**")).hasAuthority(... |
| 55 | + * } |
| 56 | + * ... |
| 57 | + * </code> |
| 58 | + * |
| 59 | + * @author Josh Cummings |
| 60 | + * @since 6.5 |
| 61 | + */ |
| 62 | +public final class ServletRequestMatcherBuilders { |
| 63 | + |
| 64 | + private ServletRequestMatcherBuilders() { |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Create {@link RequestMatcher}s whose URIs are relative to the context path, if any. |
| 69 | + * <p> |
| 70 | + * When there is no context path, then these URIs are effectively absolute. |
| 71 | + * @return a {@link RequestMatcherBuilder} that treats URIs as relative to the context |
| 72 | + * path, if any |
| 73 | + */ |
| 74 | + public static RequestMatcherBuilder requestPath() { |
| 75 | + return PathPatternRequestMatcher::pathPattern; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Create {@link RequestMatcher}s whose URIs are relative to the given |
| 80 | + * {@code servletPath}. |
| 81 | + * |
| 82 | + * <p> |
| 83 | + * The {@code servletPath} must correlate to a configured servlet in your application. |
| 84 | + * The path must be of the format {@code /path}. |
| 85 | + * @return a {@link RequestMatcherBuilder} that treats URIs as relative to the given |
| 86 | + * {@code servletPath} |
| 87 | + */ |
| 88 | + public static RequestMatcherBuilder servletPath(String servletPath) { |
| 89 | + Assert.notNull(servletPath, "servletPath cannot be null"); |
| 90 | + Assert.isTrue(servletPath.startsWith("/"), "servletPath must start with '/'"); |
| 91 | + Assert.isTrue(!servletPath.endsWith("/"), "servletPath must not end with a slash"); |
| 92 | + Assert.isTrue(!servletPath.contains("*"), "servletPath must not contain a star"); |
| 93 | + RequestMatcher servletPathMatcher = new ServletPathRequestMatcher(servletPath); |
| 94 | + return (method, pattern) -> { |
| 95 | + Assert.notNull(pattern, "pattern cannot be null"); |
| 96 | + Assert.isTrue(pattern.startsWith("/"), "pattern must start with '/'"); |
| 97 | + PathPatternRequestMatcher pathPattern = PathPatternRequestMatcher.pathPattern(method, |
| 98 | + servletPath + pattern); |
| 99 | + return new AndRequestMatcher(servletPathMatcher, pathPattern); |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + private record ServletPathRequestMatcher(String path) implements RequestMatcher { |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean matches(HttpServletRequest request) { |
| 107 | + Assert.isTrue(servletExists(request), () -> this.path + "/* does not exist in your servlet registration " |
| 108 | + + registrationMappings(request)); |
| 109 | + return Objects.equals(this.path, getServletPathPrefix(request)); |
| 110 | + } |
| 111 | + |
| 112 | + private boolean servletExists(HttpServletRequest request) { |
| 113 | + if (request.getAttribute("org.springframework.test.web.servlet.MockMvc.MVC_RESULT_ATTRIBUTE") != null) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + ServletContext servletContext = request.getServletContext(); |
| 117 | + for (ServletRegistration registration : servletContext.getServletRegistrations().values()) { |
| 118 | + if (registration.getMappings().contains(this.path + "/*")) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + } |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + private Map<String, Collection<String>> registrationMappings(HttpServletRequest request) { |
| 126 | + Map<String, Collection<String>> map = new LinkedHashMap<>(); |
| 127 | + ServletContext servletContext = request.getServletContext(); |
| 128 | + for (ServletRegistration registration : servletContext.getServletRegistrations().values()) { |
| 129 | + map.put(registration.getName(), registration.getMappings()); |
| 130 | + } |
| 131 | + return map; |
| 132 | + } |
| 133 | + |
| 134 | + @Nullable |
| 135 | + private static String getServletPathPrefix(HttpServletRequest request) { |
| 136 | + HttpServletMapping mapping = (HttpServletMapping) request.getAttribute(RequestDispatcher.INCLUDE_MAPPING); |
| 137 | + mapping = (mapping != null) ? mapping : request.getHttpServletMapping(); |
| 138 | + if (ObjectUtils.nullSafeEquals(mapping.getMappingMatch(), MappingMatch.PATH)) { |
| 139 | + String servletPath = (String) request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE); |
| 140 | + servletPath = (servletPath != null) ? servletPath : request.getServletPath(); |
| 141 | + servletPath = servletPath.endsWith("/") ? servletPath.substring(0, servletPath.length() - 1) |
| 142 | + : servletPath; |
| 143 | + return UriUtils.encodePath(servletPath, StandardCharsets.UTF_8); |
| 144 | + } |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments