Simplify Java Configuration RequestMatcher Usage
If Spring MVC is present in the classpath, use MvcRequestMatcher by default. This commit also adds a new securityMatcher method in HttpSecurity Closes gh-11347 Closes gh-9159
This commit is contained in:
committed by
Marcus Hert Da Coregio
parent
bf59d7c374
commit
039e0328e1
+56
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@@ -172,4 +172,59 @@ public class MvcRequestMatcher implements RequestMatcher, RequestVariablesExtrac
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder for {@link MvcRequestMatcher}
|
||||
*
|
||||
* @author Marcus Da Coregio
|
||||
* @since 5.8
|
||||
*/
|
||||
public static final class Builder {
|
||||
|
||||
private final HandlerMappingIntrospector introspector;
|
||||
|
||||
private String servletPath;
|
||||
|
||||
/**
|
||||
* Construct a new instance of this builder
|
||||
*/
|
||||
public Builder(HandlerMappingIntrospector introspector) {
|
||||
this.introspector = introspector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the servlet path to be used by the {@link MvcRequestMatcher} generated by
|
||||
* this builder
|
||||
* @param servletPath the servlet path to use
|
||||
* @return the {@link Builder} for further configuration
|
||||
*/
|
||||
public Builder servletPath(String servletPath) {
|
||||
this.servletPath = servletPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an {@link MvcRequestMatcher} that uses the provided pattern to match
|
||||
* @param pattern the pattern used to match
|
||||
* @return the generated {@link MvcRequestMatcher}
|
||||
*/
|
||||
public MvcRequestMatcher pattern(String pattern) {
|
||||
return pattern(null, pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an {@link MvcRequestMatcher} that uses the provided pattern and HTTP
|
||||
* method to match
|
||||
* @param method the {@link HttpMethod}, can be null
|
||||
* @param pattern the patterns used to match
|
||||
* @return the generated {@link MvcRequestMatcher}
|
||||
*/
|
||||
public MvcRequestMatcher pattern(HttpMethod method, String pattern) {
|
||||
MvcRequestMatcher mvcRequestMatcher = new MvcRequestMatcher(this.introspector, pattern);
|
||||
mvcRequestMatcher.setServletPath(this.servletPath);
|
||||
mvcRequestMatcher.setMethod(method);
|
||||
return mvcRequestMatcher;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+28
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@@ -28,6 +28,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
|
||||
import org.springframework.web.servlet.handler.MatchableHandlerMapping;
|
||||
@@ -245,4 +246,30 @@ public class MvcRequestMatcherTests {
|
||||
assertThat(this.matcher.matcher(this.request).isMatch()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderWhenServletPathThenServletPathPresent() {
|
||||
MvcRequestMatcher matcher = new MvcRequestMatcher.Builder(this.introspector).servletPath("/path")
|
||||
.pattern("/endpoint");
|
||||
assertThat(matcher.getServletPath()).isEqualTo("/path");
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "pattern")).isEqualTo("/endpoint");
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "method")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderWhenPatternThenPatternPresent() {
|
||||
MvcRequestMatcher matcher = new MvcRequestMatcher.Builder(this.introspector).pattern("/endpoint");
|
||||
assertThat(matcher.getServletPath()).isNull();
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "pattern")).isEqualTo("/endpoint");
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "method")).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void builderWhenMethodAndPatternThenMethodAndPatternPresent() {
|
||||
MvcRequestMatcher matcher = new MvcRequestMatcher.Builder(this.introspector).pattern(HttpMethod.GET,
|
||||
"/endpoint");
|
||||
assertThat(matcher.getServletPath()).isNull();
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "pattern")).isEqualTo("/endpoint");
|
||||
assertThat(ReflectionTestUtils.getField(matcher, "method")).isEqualTo(HttpMethod.GET);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user