Skip to content

Commit 914ebd6

Browse files
committed
Add forServletPattern
Closes spring-projectsgh-13562
1 parent 6d96787 commit 914ebd6

17 files changed

+2190
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2023 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.config.annotation.web.configurers;
18+
19+
import org.springframework.context.ApplicationContext;
20+
import org.springframework.http.HttpMethod;
21+
import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry;
22+
import org.springframework.security.web.util.matcher.RequestMatcher;
23+
24+
abstract class AbstractRequestMatcherBuilderRegistry<C> extends AbstractRequestMatcherRegistry<C> {
25+
26+
private final RequestMatcherBuilder builder;
27+
28+
AbstractRequestMatcherBuilderRegistry(ApplicationContext context) {
29+
this(context, RequestMatcherBuilders.createDefault(context));
30+
}
31+
32+
AbstractRequestMatcherBuilderRegistry(ApplicationContext context, RequestMatcherBuilder builder) {
33+
setApplicationContext(context);
34+
this.builder = builder;
35+
}
36+
37+
@Override
38+
public final C requestMatchers(String... patterns) {
39+
return requestMatchers(null, patterns);
40+
}
41+
42+
@Override
43+
public final C requestMatchers(HttpMethod method, String... patterns) {
44+
return requestMatchers(this.builder.matchers(method, patterns).toArray(RequestMatcher[]::new));
45+
}
46+
47+
@Override
48+
public final C requestMatchers(HttpMethod method) {
49+
return requestMatchers(method, "/**");
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2002-2023 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.config.annotation.web.configurers;
18+
19+
import org.springframework.http.HttpMethod;
20+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
21+
22+
final class AntPathRequestMatcherBuilder implements RequestMatcherBuilder {
23+
24+
private final String servletPath;
25+
26+
private AntPathRequestMatcherBuilder(String servletPath) {
27+
this.servletPath = servletPath;
28+
}
29+
30+
static AntPathRequestMatcherBuilder absolute() {
31+
return new AntPathRequestMatcherBuilder(null);
32+
}
33+
34+
static AntPathRequestMatcherBuilder relativeTo(String path) {
35+
return new AntPathRequestMatcherBuilder(path);
36+
}
37+
38+
@Override
39+
public AntPathRequestMatcher matcher(String pattern) {
40+
return matcher((String) null, pattern);
41+
}
42+
43+
@Override
44+
public AntPathRequestMatcher matcher(HttpMethod method, String pattern) {
45+
return matcher((method != null) ? method.name() : null, pattern);
46+
}
47+
48+
private AntPathRequestMatcher matcher(String method, String pattern) {
49+
return new AntPathRequestMatcher(prependServletPath(pattern), method);
50+
}
51+
52+
private String prependServletPath(String pattern) {
53+
if (this.servletPath == null) {
54+
return pattern;
55+
}
56+
return this.servletPath + pattern;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)