Skip to content

Commit

Permalink
Polish Opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
jzheaux committed Feb 19, 2025
1 parent eb22c9b commit 3d5bc54
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
Expand Down Expand Up @@ -219,7 +219,7 @@ public C requestMatchers(HttpMethod method, String... patterns) {
return requestMatchers(RequestMatchers.antMatchersAsArray(method, patterns));
}
List<RequestMatcher> matchers = new ArrayList<>();
MethodPatternRequestMatcherFactory requestMatcherFactory = getRequestMatcherFactory();
MethodPathRequestMatcherFactory requestMatcherFactory = getRequestMatcherFactory();
for (String pattern : patterns) {
matchers.add(requestMatcherFactory.matcher(method, pattern));
}
Expand Down Expand Up @@ -331,9 +331,9 @@ public C requestMatchers(HttpMethod method) {
*/
protected abstract C chainRequestMatchers(List<RequestMatcher> requestMatchers);

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
return this.context.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getIfUnique(DefaultMethodPatternRequestMatcherFactory::new);
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return this.context.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(DefaultMethodPathRequestMatcherFactory::new);
}

/**
Expand Down Expand Up @@ -409,12 +409,12 @@ static List<RequestMatcher> regexMatchers(String... regexPatterns) {

}

class DefaultMethodPatternRequestMatcherFactory implements MethodPatternRequestMatcherFactory {
class DefaultMethodPathRequestMatcherFactory implements MethodPathRequestMatcherFactory {

@Override
public RequestMatcher matcher(HttpMethod method, String pattern) {
AntPathRequestMatcher ant = new AntPathRequestMatcher(pattern, (method != null) ? method.name() : null);
MvcRequestMatcher mvc = createMvcMatchers(method, pattern).get(0);
public RequestMatcher matcher(HttpMethod method, String path) {
AntPathRequestMatcher ant = new AntPathRequestMatcher(path, (method != null) ? method.name() : null);
MvcRequestMatcher mvc = createMvcMatchers(method, path).get(0);
return new DeferredRequestMatcher((c) -> resolve(ant, mvc, c), mvc, ant);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
import org.springframework.security.web.session.HttpSessionEventPublisher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -3686,8 +3686,8 @@ public HttpSecurity securityMatcher(RequestMatcher requestMatcher) {
*/
public HttpSecurity securityMatcher(String... patterns) {
List<RequestMatcher> matchers = new ArrayList<>();
MethodPatternRequestMatcherFactory factory = getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
MethodPathRequestMatcherFactory factory = getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> (method, pattern) -> mvcPresent ? createMvcMatcher(pattern) : createAntMatcher(pattern));
for (String pattern : patterns) {
matchers.add(factory.matcher(pattern));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;

/**
Expand Down Expand Up @@ -274,9 +274,9 @@ private void initDefaultLoginFilter(H http) {
}
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -374,9 +374,9 @@ private RequestMatcher createLogoutRequestMatcher(String httpMethod) {
return getRequestMatcherFactory().matcher(HttpMethod.valueOf(httpMethod), this.logoutUrl);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.springframework.security.web.RequestMatcherRedirectFilter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -61,9 +61,9 @@ public void configure(B http) throws Exception {
http.addFilterBefore(postProcess(changePasswordFilter), UsernamePasswordAuthenticationFilter.class);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
Expand Down Expand Up @@ -169,9 +169,9 @@ private RequestMatcher notMatchingMediaType(H http, MediaType mediaType) {
return new NegatedRequestMatcher(mediaRequest);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestHeaderRequestMatcher;
Expand Down Expand Up @@ -626,9 +626,9 @@ private void registerDelegateApplicationListener(ApplicationListener<?> delegate
delegating.addListener(smartListener);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import org.springframework.security.web.authentication.ui.DefaultResourcesFilter;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -210,9 +210,9 @@ protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingU
return getRequestMatcherFactory().matcher(HttpMethod.POST, loginProcessingUrl);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.NegatedRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.ParameterRequestMatcher;
Expand Down Expand Up @@ -505,9 +505,9 @@ private Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest>
return repository;
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.ParameterRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

Expand Down Expand Up @@ -335,9 +335,9 @@ private Saml2LogoutResponseResolver createSaml2LogoutResponseResolver(
return this.logoutResponseConfigurer.logoutResponseResolver(registrations);
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.springframework.security.saml2.provider.service.web.metadata.RequestMatcherMetadataResponseResolver;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -171,9 +171,9 @@ private RelyingPartyRegistrationRepository getRelyingPartyRegistrationRepository
}
}

private MethodPatternRequestMatcherFactory getRequestMatcherFactory() {
private MethodPathRequestMatcherFactory getRequestMatcherFactory() {
return getBuilder().getSharedObject(ApplicationContext.class)
.getBeanProvider(MethodPatternRequestMatcherFactory.class)
.getBeanProvider(MethodPathRequestMatcherFactory.class)
.getIfUnique(() -> AntPathRequestMatcher::antMatcher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.test.web.servlet.MockMvc;
Expand Down Expand Up @@ -89,15 +89,15 @@ public void setUp() {
ObjectProvider<ObjectPostProcessor<Object>> given = this.context.getBeanProvider(type);
given(given).willReturn(postProcessors);
given(postProcessors.getObject()).willReturn(NO_OP_OBJECT_POST_PROCESSOR);
MethodPatternRequestMatcherFactory factory = this.matcherRegistry.new DefaultMethodPatternRequestMatcherFactory();
ObjectProvider<MethodPatternRequestMatcherFactory> requestMatcherFactory = new ObjectProvider<>() {
MethodPathRequestMatcherFactory factory = this.matcherRegistry.new DefaultMethodPathRequestMatcherFactory();
ObjectProvider<MethodPathRequestMatcherFactory> requestMatcherFactory = new ObjectProvider<>() {
@Override
@NonNull
public MethodPatternRequestMatcherFactory getObject() throws BeansException {
public MethodPathRequestMatcherFactory getObject() throws BeansException {
return factory;
}
};
given(this.context.getBeanProvider(MethodPatternRequestMatcherFactory.class)).willReturn(requestMatcherFactory);
given(this.context.getBeanProvider(MethodPathRequestMatcherFactory.class)).willReturn(requestMatcherFactory);
given(this.context.getServletContext()).willReturn(MockServletContext.mvc());
this.matcherRegistry.setApplicationContext(this.context);
mockMvcIntrospector(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.springframework.lang.Nullable;
import org.springframework.security.web.access.intercept.RequestAuthorizationContext;
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
import org.springframework.security.web.util.matcher.MethodPatternRequestMatcherFactory;
import org.springframework.security.web.util.matcher.MethodPathRequestMatcherFactory;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;
import org.springframework.web.util.ServletRequestPathUtils;
Expand Down Expand Up @@ -209,7 +209,7 @@ public String toString() {
* ...
* </code>
*/
public static final class Builder implements MethodPatternRequestMatcherFactory {
public static final class Builder implements MethodPathRequestMatcherFactory {

private final PathPatternParser parser;

Expand All @@ -226,8 +226,8 @@ private Builder(PathPatternParser parser) {
/**
* Match requests starting with this {@code servletPath}.
* @param servletPath the servlet path prefix
* @see PathPatternRequestMatcher#servletPath
* @return the {@link Builder} for more configuration
* @see PathPatternRequestMatcher#servletPath
*/
public Builder servletPath(String servletPath) {
this.servletPath = new ServletPathRequestMatcher(servletPath);
Expand Down Expand Up @@ -262,13 +262,13 @@ public Builder servletPath(String servletPath) {
* <p>
* A more comprehensive list can be found at {@link PathPattern}.
* @param method the {@link HttpMethod} to match, may be null
* @param pattern the path pattern to match
* @param path the path pattern to match
* @return the {@link Builder} for more configuration
*/
public PathPatternRequestMatcher matcher(@Nullable HttpMethod method, String pattern) {
Assert.notNull(pattern, "pattern cannot be null");
Assert.isTrue(pattern.startsWith("/"), "pattern must start with a /");
PathPattern pathPattern = this.parser.parse(pattern);
public PathPatternRequestMatcher matcher(@Nullable HttpMethod method, String path) {
Assert.notNull(path, "pattern cannot be null");
Assert.isTrue(path.startsWith("/"), "pattern must start with a /");
PathPattern pathPattern = this.parser.parse(path);
PathPatternRequestMatcher requestMatcher = new PathPatternRequestMatcher(pathPattern);
if (method != null) {
requestMatcher.setMethod(new HttpMethodRequestMatcher(method));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,25 @@
* @author Josh Cummings
* @since 6.5
*/
public interface MethodPatternRequestMatcherFactory {
public interface MethodPathRequestMatcherFactory {

/**
* Request a method-pattern request matcher given the following {{@code method} and
* {@code pattern}.
* This method in this case is treated as a wildcard.
*
* @param pattern the path pattern to use
* Request a method-pattern request matcher given the following {@code method} and
* {@code pattern}. This method in this case is treated as a wildcard.
* @param path the path pattern to use
* @return the {@link RequestMatcher}
*/
default RequestMatcher matcher(String pattern) {
return matcher(null, pattern);
default RequestMatcher matcher(String path) {
return matcher(null, path);
}

/**
* Request a method-pattern request matcher given the following
* {@code method} and {@code pattern}.
*
* Request a method-pattern request matcher given the following {@code method} and
* {@code pattern}.
* @param method the method to use, may be null
* @param pattern the path pattern to use
* @param path the path pattern to use
* @return the {@link RequestMatcher}
*/
RequestMatcher matcher(@Nullable HttpMethod method, String pattern);
RequestMatcher matcher(@Nullable HttpMethod method, String path);

}

0 comments on commit 3d5bc54

Please sign in to comment.