28
28
import jakarta .servlet .ServletResponse ;
29
29
import jakarta .servlet .http .HttpServletRequest ;
30
30
31
- import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
32
31
import org .springframework .beans .factory .ObjectProvider ;
33
32
import org .springframework .context .ApplicationContext ;
34
33
import org .springframework .core .OrderComparator ;
45
44
import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
46
45
import org .springframework .security .config .annotation .web .AbstractRequestMatcherRegistry ;
47
46
import org .springframework .security .config .annotation .web .HttpSecurityBuilder ;
48
- import org .springframework .security .config .annotation .web .RequestMatcherFactory ;
49
47
import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
50
48
import org .springframework .security .config .annotation .web .configuration .WebSecurityConfiguration ;
51
49
import org .springframework .security .config .annotation .web .configurers .AnonymousConfigurer ;
91
89
import org .springframework .security .web .authentication .UsernamePasswordAuthenticationFilter ;
92
90
import org .springframework .security .web .context .AbstractSecurityWebApplicationInitializer ;
93
91
import org .springframework .security .web .context .SecurityContextRepository ;
94
- import org .springframework .security .web .servlet .util .matcher .MvcRequestMatcher ;
92
+ import org .springframework .security .web .servlet .util .matcher .PathPatternRequestMatcher ;
95
93
import org .springframework .security .web .session .HttpSessionEventPublisher ;
96
- import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
97
94
import org .springframework .security .web .util .matcher .AnyRequestMatcher ;
98
95
import org .springframework .security .web .util .matcher .OrRequestMatcher ;
99
96
import org .springframework .security .web .util .matcher .RequestMatcher ;
100
97
import org .springframework .util .Assert ;
101
- import org .springframework .util .ClassUtils ;
102
98
import org .springframework .web .cors .CorsConfiguration ;
103
99
import org .springframework .web .filter .CorsFilter ;
104
- import org .springframework .web .servlet .handler .HandlerMappingIntrospector ;
105
100
106
101
/**
107
102
* A {@link HttpSecurity} is similar to Spring Security's XML <http> element in the
153
148
public final class HttpSecurity extends AbstractConfiguredSecurityBuilder <DefaultSecurityFilterChain , HttpSecurity >
154
149
implements SecurityBuilder <DefaultSecurityFilterChain >, HttpSecurityBuilder <HttpSecurity > {
155
150
156
- private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector" ;
157
-
158
- private static final String HANDLER_MAPPING_INTROSPECTOR = "org.springframework.web.servlet.handler.HandlerMappingIntrospector" ;
159
-
160
- private static final boolean mvcPresent ;
161
-
162
151
private final RequestMatcherConfigurer requestMatcherConfigurer ;
163
152
164
153
private List <OrderedFilter > filters = new ArrayList <>();
@@ -169,10 +158,6 @@ public final class HttpSecurity extends AbstractConfiguredSecurityBuilder<Defaul
169
158
170
159
private AuthenticationManager authenticationManager ;
171
160
172
- static {
173
- mvcPresent = ClassUtils .isPresent (HANDLER_MAPPING_INTROSPECTOR , HttpSecurity .class .getClassLoader ());
174
- }
175
-
176
161
/**
177
162
* Creates a new instance
178
163
* @param objectPostProcessor the {@link ObjectPostProcessor} that should be used
@@ -303,9 +288,7 @@ public HttpSecurity headers(Customizer<HeadersConfigurer<HttpSecurity>> headersC
303
288
/**
304
289
* Adds a {@link CorsFilter} to be used. If a bean by the name of corsFilter is
305
290
* provided, that {@link CorsFilter} is used. Else if corsConfigurationSource is
306
- * defined, then that {@link CorsConfiguration} is used. Otherwise, if Spring MVC is
307
- * on the classpath a {@link HandlerMappingIntrospector} is used. You can enable CORS
308
- * using:
291
+ * defined, then that {@link CorsConfiguration} is used. You can enable CORS using:
309
292
*
310
293
* <pre>
311
294
* @Configuration
@@ -2174,7 +2157,7 @@ public HttpSecurity securityMatchers(Customizer<RequestMatcherConfigurer> reques
2174
2157
* {@link #securityMatchers()}
2175
2158
* </p>
2176
2159
* @param requestMatcher the {@link RequestMatcher} to use, for example,
2177
- * {@code PathPatternRequestMatcher.withDefaults().matcher (HttpMethod.GET, "/admin/**")}
2160
+ * {@code PathPatternRequestMatcher.pathPattern (HttpMethod.GET, "/admin/**")}
2178
2161
* @return the {@link HttpSecurity} for further customizations
2179
2162
* @see #securityMatcher(String...)
2180
2163
*/
@@ -2185,10 +2168,8 @@ public HttpSecurity securityMatcher(RequestMatcher requestMatcher) {
2185
2168
2186
2169
/**
2187
2170
* Allows configuring the {@link HttpSecurity} to only be invoked when matching the
2188
- * provided pattern. This method creates a {@link MvcRequestMatcher} if Spring MVC is
2189
- * in the classpath or creates an {@link AntPathRequestMatcher} if not. If more
2190
- * advanced configuration is necessary, consider using
2191
- * {@link #securityMatchers(Customizer)} or {@link #securityMatcher(RequestMatcher)}.
2171
+ * provided set of {@code patterns}. See
2172
+ * {@link org.springframework.web.util.pattern.PathPattern} for matching rules
2192
2173
*
2193
2174
* <p>
2194
2175
* Invoking {@link #securityMatcher(String...)} will override previous invocations of
@@ -2198,19 +2179,14 @@ public HttpSecurity securityMatcher(RequestMatcher requestMatcher) {
2198
2179
* </p>
2199
2180
* @param patterns the pattern to match on (i.e. "/admin/**")
2200
2181
* @return the {@link HttpSecurity} for further customizations
2201
- * @see AntPathRequestMatcher
2202
- * @see MvcRequestMatcher
2182
+ * @see org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher
2183
+ * @see org.springframework.web.util.pattern.PathPattern
2203
2184
*/
2204
2185
public HttpSecurity securityMatcher (String ... patterns ) {
2205
2186
List <RequestMatcher > matchers = new ArrayList <>();
2187
+ PathPatternRequestMatcher .Builder builder = getContext ().getBean (PathPatternRequestMatcher .Builder .class );
2206
2188
for (String pattern : patterns ) {
2207
- if (RequestMatcherFactory .usesPathPatterns ()) {
2208
- matchers .add (RequestMatcherFactory .matcher (pattern ));
2209
- }
2210
- else {
2211
- RequestMatcher matcher = mvcPresent ? createMvcMatcher (pattern ) : createAntMatcher (pattern );
2212
- matchers .add (matcher );
2213
- }
2189
+ matchers .add (builder .matcher (pattern ));
2214
2190
}
2215
2191
this .requestMatcher = new OrRequestMatcher (matchers );
2216
2192
return this ;
@@ -2241,26 +2217,6 @@ public HttpSecurity webAuthn(Customizer<WebAuthnConfigurer<HttpSecurity>> webAut
2241
2217
return HttpSecurity .this ;
2242
2218
}
2243
2219
2244
- private RequestMatcher createAntMatcher (String pattern ) {
2245
- return new AntPathRequestMatcher (pattern );
2246
- }
2247
-
2248
- private RequestMatcher createMvcMatcher (String mvcPattern ) {
2249
- ResolvableType type = ResolvableType .forClassWithGenerics (ObjectPostProcessor .class , Object .class );
2250
- ObjectProvider <ObjectPostProcessor <Object >> postProcessors = getContext ().getBeanProvider (type );
2251
- ObjectPostProcessor <Object > opp = postProcessors .getObject ();
2252
- if (!getContext ().containsBean (HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME )) {
2253
- throw new NoSuchBeanDefinitionException ("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME
2254
- + " of type " + HandlerMappingIntrospector .class .getName ()
2255
- + " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext." );
2256
- }
2257
- HandlerMappingIntrospector introspector = getContext ().getBean (HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME ,
2258
- HandlerMappingIntrospector .class );
2259
- MvcRequestMatcher matcher = new MvcRequestMatcher (introspector , mvcPattern );
2260
- opp .postProcess (matcher );
2261
- return matcher ;
2262
- }
2263
-
2264
2220
/**
2265
2221
* If the {@link SecurityConfigurer} has already been specified get the original,
2266
2222
* otherwise apply the new {@link SecurityConfigurerAdapter}.
0 commit comments