Skip to content

Commit 134628a

Browse files
committed
Add PathRequest to reactive security for parity
1 parent e80c22c commit 134628a

File tree

6 files changed

+114
-22
lines changed

6 files changed

+114
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.boot.autoconfigure.security.reactive;
18+
19+
import org.springframework.boot.autoconfigure.security.StaticResourceLocation;
20+
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
21+
22+
/**
23+
* Factory that can be used to create a {@link ServerWebExchangeMatcher} for commonly used paths.
24+
*
25+
* @author Madhura Bhave
26+
* @since 2.0.0
27+
*/
28+
public final class PathRequest {
29+
30+
private PathRequest() {
31+
}
32+
33+
/**
34+
* Returns a {@link StaticResourceRequest} that can be used to create a matcher for
35+
* {@link StaticResourceLocation Locations}.
36+
* @return a {@link StaticResourceRequest}
37+
*/
38+
public static StaticResourceRequest toStaticResources() {
39+
return StaticResourceRequest.get();
40+
}
41+
42+
}
43+

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequest.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333
import org.springframework.web.server.ServerWebExchange;
3434

3535
/**
36-
* Factory that can be used to create a {@link ServerWebExchangeMatcher} for static
37-
* resources in commonly used locations.
36+
* Used to create a {@link ServerWebExchangeMatcher} for static resources in
37+
* commonly used locations. Returned by {@link PathRequest#toStaticResources()}.
3838
*
3939
* @author Madhura Bhave
4040
* @since 2.0.0
41+
* @see PathRequest
4142
*/
4243
public final class StaticResourceRequest {
4344

45+
private static final StaticResourceRequest INSTANCE = new StaticResourceRequest();
46+
4447
private StaticResourceRequest() {
4548
}
4649

@@ -50,42 +53,50 @@ private StaticResourceRequest() {
5053
* {@link StaticResourceServerWebExchange#excluding(StaticResourceLocation, StaticResourceLocation...)
5154
* excluding} method can be used to remove specific locations if required. For
5255
* example: <pre class="code">
53-
* StaticResourceRequest.toCommonLocations().excluding(StaticResourceLocation.CSS)
56+
* StaticResourceRequest.atCommonLocations().excluding(StaticResourceLocation.CSS)
5457
* </pre>
5558
* @return the configured {@link ServerWebExchangeMatcher}
5659
*/
57-
public static StaticResourceServerWebExchange toCommonLocations() {
58-
return to(EnumSet.allOf(StaticResourceLocation.class));
60+
public StaticResourceServerWebExchange atCommonLocations() {
61+
return at(EnumSet.allOf(StaticResourceLocation.class));
5962
}
6063

6164
/**
6265
* Returns a matcher that includes the specified {@link StaticResourceLocation
6366
* Locations}. For example: <pre class="code">
64-
* to(StaticResourceLocation.CSS, StaticResourceLocation.JAVA_SCRIPT)
67+
* at(StaticResourceLocation.CSS, StaticResourceLocation.JAVA_SCRIPT)
6568
* </pre>
6669
* @param first the first location to include
6770
* @param rest additional locations to include
6871
* @return the configured {@link ServerWebExchangeMatcher}
6972
*/
70-
public static StaticResourceServerWebExchange to(StaticResourceLocation first,
73+
public StaticResourceServerWebExchange at(StaticResourceLocation first,
7174
StaticResourceLocation... rest) {
72-
return to(EnumSet.of(first, rest));
75+
return at(EnumSet.of(first, rest));
7376
}
7477

7578
/**
7679
* Returns a matcher that includes the specified {@link StaticResourceLocation
7780
* Locations}. For example: <pre class="code">
78-
* to(locations)
81+
* at(locations)
7982
* </pre>
8083
* @param locations the locations to include
8184
* @return the configured {@link ServerWebExchangeMatcher}
8285
*/
83-
public static StaticResourceServerWebExchange to(
86+
public StaticResourceServerWebExchange at(
8487
Set<StaticResourceLocation> locations) {
8588
Assert.notNull(locations, "Locations must not be null");
8689
return new StaticResourceServerWebExchange(new LinkedHashSet<>(locations));
8790
}
8891

92+
/**
93+
* Return the static resource request.
94+
* @return the static resource request
95+
*/
96+
static StaticResourceRequest get() {
97+
return INSTANCE;
98+
}
99+
89100
/**
90101
* The server web exchange matcher used to match against resource
91102
* {@link StaticResourceLocation Locations}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.boot.autoconfigure.security.reactive;
18+
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link PathRequest}.
25+
*
26+
* @author Madhura Bhave
27+
*/
28+
public class PathRequestTests {
29+
30+
@Test
31+
public void toStaticResourcesShouldReturnStaticResourceRequest() {
32+
assertThat(PathRequest.toStaticResources()).isInstanceOf(StaticResourceRequest.class);
33+
}
34+
35+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/reactive/StaticResourceRequestTests.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@
4444
*/
4545
public class StaticResourceRequestTests {
4646

47+
private StaticResourceRequest resourceRequest = StaticResourceRequest.get();
48+
4749
@Rule
4850
public ExpectedException thrown = ExpectedException.none();
4951

5052
@Test
51-
public void toCommonLocationsShouldMatchCommonLocations() {
52-
ServerWebExchangeMatcher matcher = StaticResourceRequest.toCommonLocations();
53+
public void atCommonLocationsShouldMatchCommonLocations() {
54+
ServerWebExchangeMatcher matcher = this.resourceRequest.atCommonLocations();
5355
assertMatcher(matcher).matches("/css/file.css");
5456
assertMatcher(matcher).matches("/js/file.js");
5557
assertMatcher(matcher).matches("/images/file.css");
@@ -59,33 +61,33 @@ public void toCommonLocationsShouldMatchCommonLocations() {
5961
}
6062

6163
@Test
62-
public void toCommonLocationsWithExcludeShouldNotMatchExcluded() {
63-
ServerWebExchangeMatcher matcher = StaticResourceRequest.toCommonLocations()
64+
public void atCommonLocationsWithExcludeShouldNotMatchExcluded() {
65+
ServerWebExchangeMatcher matcher = this.resourceRequest.atCommonLocations()
6466
.excluding(StaticResourceLocation.CSS);
6567
assertMatcher(matcher).doesNotMatch("/css/file.css");
6668
assertMatcher(matcher).matches("/js/file.js");
6769
}
6870

6971
@Test
70-
public void toLocationShouldMatchLocation() {
71-
ServerWebExchangeMatcher matcher = StaticResourceRequest
72-
.to(StaticResourceLocation.CSS);
72+
public void atLocationShouldMatchLocation() {
73+
ServerWebExchangeMatcher matcher = this.resourceRequest
74+
.at(StaticResourceLocation.CSS);
7375
assertMatcher(matcher).matches("/css/file.css");
7476
assertMatcher(matcher).doesNotMatch("/js/file.js");
7577
}
7678

7779
@Test
78-
public void toLocationsFromSetWhenSetIsNullShouldThrowException() {
80+
public void atLocationsFromSetWhenSetIsNullShouldThrowException() {
7981
this.thrown.expect(IllegalArgumentException.class);
8082
this.thrown.expectMessage("Locations must not be null");
81-
StaticResourceRequest.to(null);
83+
this.resourceRequest.at(null);
8284
}
8385

8486
@Test
8587
public void excludeFromSetWhenSetIsNullShouldThrowException() {
8688
this.thrown.expect(IllegalArgumentException.class);
8789
this.thrown.expectMessage("Locations must not be null");
88-
StaticResourceRequest.toCommonLocations().excluding(null);
90+
this.resourceRequest.atCommonLocations().excluding(null);
8991
}
9092

9193
private StaticResourceRequestTests.RequestMatcherAssert assertMatcher(

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3035,7 +3035,7 @@ Boot provides convenience methods that can be used to override access rules for
30353035
endpoints and static resources. `EndpointRequest` can be used to create a `ServerWebExchangeMatcher`
30363036
that is based on the `management.endpoints.web.base-path` property.
30373037

3038-
`StaticResourceRequest` can be used to create a `ServerWebExchangeMatcher` for static resources in
3038+
`PathRequest` can be used to create a `ServerWebExchangeMatcher` for resources in
30393039
commonly used locations.
30403040

30413041

spring-boot-samples/spring-boot-sample-secure-webflux/src/test/java/sample/secure/webflux/SampleSecureWebFluxCustomSecurityTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.boot.actuate.autoconfigure.security.reactive.EndpointRequest;
26+
import org.springframework.boot.autoconfigure.security.reactive.PathRequest;
2627
import org.springframework.boot.autoconfigure.security.reactive.StaticResourceRequest;
2728
import org.springframework.boot.test.context.SpringBootTest;
2829
import org.springframework.context.annotation.Bean;
@@ -101,7 +102,7 @@ public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http)
101102
http.authorizeExchange().matchers(EndpointRequest.to("health", "info"))
102103
.permitAll().matchers(EndpointRequest.toAnyEndpoint())
103104
.hasRole("ACTUATOR")
104-
.matchers(StaticResourceRequest.toCommonLocations()).permitAll()
105+
.matchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
105106
.pathMatchers("/login").permitAll().anyExchange().authenticated()
106107
.and().httpBasic();
107108
return http.build();

0 commit comments

Comments
 (0)