pull/11626/merge
Phillip Webb 7 years ago
parent d9ff51ccd3
commit dc935fba48

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -33,8 +33,8 @@ import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebExchange;
/**
* Factory that can be used to create a {@link ServerWebExchangeMatcher} for static resources in
* commonly used locations.
* Factory that can be used to create a {@link ServerWebExchangeMatcher} for static
* resources in commonly used locations.
*
* @author Madhura Bhave
* @since 2.0.0
@ -45,10 +45,11 @@ public final class StaticResourceRequest {
}
/**
* Returns a matcher that includes all commonly used {@link StaticResourceLocation Locations}. The
* {@link StaticResourceServerWebExchange#excluding(StaticResourceLocation, StaticResourceLocation...) excluding}
* method can be used to remove specific locations if required. For example:
* <pre class="code">
* Returns a matcher that includes all commonly used {@link StaticResourceLocation
* Locations}. The
* {@link StaticResourceServerWebExchange#excluding(StaticResourceLocation, StaticResourceLocation...)
* excluding} method can be used to remove specific locations if required. For
* example: <pre class="code">
* StaticResourceRequest.toCommonLocations().excluding(StaticResourceLocation.CSS)
* </pre>
* @return the configured {@link ServerWebExchangeMatcher}
@ -58,33 +59,36 @@ public final class StaticResourceRequest {
}
/**
* Returns a matcher that includes the specified {@link StaticResourceLocation Locations}. For
* example: <pre class="code">
* Returns a matcher that includes the specified {@link StaticResourceLocation
* Locations}. For example: <pre class="code">
* to(StaticResourceLocation.CSS, StaticResourceLocation.JAVA_SCRIPT)
* </pre>
* @param first the first location to include
* @param rest additional locations to include
* @return the configured {@link ServerWebExchangeMatcher}
*/
public static StaticResourceServerWebExchange to(StaticResourceLocation first, StaticResourceLocation... rest) {
public static StaticResourceServerWebExchange to(StaticResourceLocation first,
StaticResourceLocation... rest) {
return to(EnumSet.of(first, rest));
}
/**
* Returns a matcher that includes the specified {@link StaticResourceLocation Locations}. For
* example: <pre class="code">
* Returns a matcher that includes the specified {@link StaticResourceLocation
* Locations}. For example: <pre class="code">
* to(locations)
* </pre>
* @param locations the locations to include
* @return the configured {@link ServerWebExchangeMatcher}
*/
public static StaticResourceServerWebExchange to(Set<StaticResourceLocation> locations) {
public static StaticResourceServerWebExchange to(
Set<StaticResourceLocation> locations) {
Assert.notNull(locations, "Locations must not be null");
return new StaticResourceServerWebExchange(new LinkedHashSet<>(locations));
}
/**
* The server web exchange matcher used to match against resource {@link StaticResourceLocation Locations}.
* The server web exchange matcher used to match against resource
* {@link StaticResourceLocation Locations}.
*/
public final static class StaticResourceServerWebExchange
implements ServerWebExchangeMatcher {
@ -102,7 +106,8 @@ public final class StaticResourceRequest {
* @param rest additional locations to exclude
* @return a new {@link StaticResourceServerWebExchange}
*/
public StaticResourceServerWebExchange excluding(StaticResourceLocation first, StaticResourceLocation... rest) {
public StaticResourceServerWebExchange excluding(StaticResourceLocation first,
StaticResourceLocation... rest) {
return excluding(EnumSet.of(first, rest));
}
@ -112,7 +117,8 @@ public final class StaticResourceRequest {
* @param locations the locations to exclude
* @return a new {@link StaticResourceServerWebExchange}
*/
public StaticResourceServerWebExchange excluding(Set<StaticResourceLocation> locations) {
public StaticResourceServerWebExchange excluding(
Set<StaticResourceLocation> locations) {
Assert.notNull(locations, "Locations must not be null");
Set<StaticResourceLocation> subset = new LinkedHashSet<>(this.locations);
subset.removeAll(locations);
@ -130,10 +136,10 @@ public final class StaticResourceRequest {
@Override
public Mono<MatchResult> matches(ServerWebExchange exchange) {
OrServerWebExchangeMatcher matcher = new OrServerWebExchangeMatcher(getDelegateMatchers());
OrServerWebExchangeMatcher matcher = new OrServerWebExchangeMatcher(
getDelegateMatchers());
return matcher.matches(exchange);
}
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -47,10 +47,11 @@ public final class StaticResourceRequest {
}
/**
* Returns a matcher that includes all commonly used {@link StaticResourceLocation Locations}. The
* {@link StaticResourceRequestMatcher#excluding(StaticResourceLocation, StaticResourceLocation...) excluding}
* method can be used to remove specific locations if required. For example:
* <pre class="code">
* Returns a matcher that includes all commonly used {@link StaticResourceLocation
* Locations}. The
* {@link StaticResourceRequestMatcher#excluding(StaticResourceLocation, StaticResourceLocation...)
* excluding} method can be used to remove specific locations if required. For
* example: <pre class="code">
* StaticResourceRequest.toCommonLocations().excluding(StaticResourceLocation.CSS)
* </pre>
* @return the configured {@link RequestMatcher}
@ -60,21 +61,22 @@ public final class StaticResourceRequest {
}
/**
* Returns a matcher that includes the specified {@link StaticResourceLocation Locations}. For
* example: <pre class="code">
* Returns a matcher that includes the specified {@link StaticResourceLocation
* Locations}. For example: <pre class="code">
* StaticResourceRequest.to(StaticResourceLocation.CSS, StaticResourceLocation.JAVA_SCRIPT)
* </pre>
* @param first the first location to include
* @param rest additional locations to include
* @return the configured {@link RequestMatcher}
*/
public static StaticResourceRequestMatcher to(StaticResourceLocation first, StaticResourceLocation... rest) {
public static StaticResourceRequestMatcher to(StaticResourceLocation first,
StaticResourceLocation... rest) {
return to(EnumSet.of(first, rest));
}
/**
* Returns a matcher that includes the specified {@link StaticResourceLocation Locations}. For
* example: <pre class="code">
* Returns a matcher that includes the specified {@link StaticResourceLocation
* Locations}. For example: <pre class="code">
* StaticResourceRequest.to(locations)
* </pre>
* @param locations the locations to include
@ -86,7 +88,8 @@ public final class StaticResourceRequest {
}
/**
* The request matcher used to match against resource {@link StaticResourceLocation Locations}.
* The request matcher used to match against resource {@link StaticResourceLocation
* Locations}.
*/
public static final class StaticResourceRequestMatcher
extends ApplicationContextRequestMatcher<ServerProperties> {
@ -107,7 +110,8 @@ public final class StaticResourceRequest {
* @param rest additional locations to exclude
* @return a new {@link StaticResourceRequestMatcher}
*/
public StaticResourceRequestMatcher excluding(StaticResourceLocation first, StaticResourceLocation... rest) {
public StaticResourceRequestMatcher excluding(StaticResourceLocation first,
StaticResourceLocation... rest) {
return excluding(EnumSet.of(first, rest));
}
@ -117,7 +121,8 @@ public final class StaticResourceRequest {
* @param locations the locations to exclude
* @return a new {@link StaticResourceRequestMatcher}
*/
public StaticResourceRequestMatcher excluding(Set<StaticResourceLocation> locations) {
public StaticResourceRequestMatcher excluding(
Set<StaticResourceLocation> locations) {
Assert.notNull(locations, "Locations must not be null");
Set<StaticResourceLocation> subset = new LinkedHashSet<>(this.locations);
subset.removeAll(locations);

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -187,24 +187,25 @@ public abstract class AbstractErrorWebExceptionHandler
protected Mono<ServerResponse> renderDefaultErrorView(
ServerResponse.BodyBuilder responseBody, Map<String, Object> error) {
StringBuilder builder = new StringBuilder();
Object message = error.get("message");
Date timestamp = (Date) error.get("timestamp");
builder.append("<html><body><h1>Whitelabel Error Page</h1>")
.append("<p>This application has no configured error view, so you are seeing this as a fallback.</p>")
.append("<div id='created'>").append(timestamp.toString())
.append("</div>").append("<div>There was an unexpected error (type=")
.append(HtmlUtils.htmlEscape(error.get("error").toString()))
.append(", status=")
.append(HtmlUtils.htmlEscape(error.get("status").toString()))
.append(").</div>");
if (error.get("message") != null) {
builder.append("<div>")
.append(HtmlUtils.htmlEscape(error.get("message").toString()))
.append("</div>");
.append(htmlEscape(error.get("error"))).append(", status=")
.append(htmlEscape(error.get("status"))).append(").</div>");
if (message != null) {
builder.append("<div>").append(htmlEscape(message)).append("</div>");
}
builder.append("</body></html>");
return responseBody.syncBody(builder.toString());
}
private String htmlEscape(Object input) {
return (input == null ? null : HtmlUtils.htmlEscape(input.toString()));
}
@Override
public void afterPropertiesSet() throws Exception {
if (CollectionUtils.isEmpty(this.messageWriters)) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -68,7 +68,8 @@ public class StaticResourceRequestTests {
@Test
public void toLocationShouldMatchLocation() {
ServerWebExchangeMatcher matcher = StaticResourceRequest.to(StaticResourceLocation.CSS);
ServerWebExchangeMatcher matcher = StaticResourceRequest
.to(StaticResourceLocation.CSS);
assertMatcher(matcher).matches("/css/file.css");
assertMatcher(matcher).doesNotMatch("/js/file.js");
}
@ -87,10 +88,12 @@ public class StaticResourceRequestTests {
StaticResourceRequest.toCommonLocations().excluding(null);
}
private StaticResourceRequestTests.RequestMatcherAssert assertMatcher(ServerWebExchangeMatcher matcher) {
private StaticResourceRequestTests.RequestMatcherAssert assertMatcher(
ServerWebExchangeMatcher matcher) {
StaticWebApplicationContext context = new StaticWebApplicationContext();
context.registerBean(ServerProperties.class);
return assertThat(new StaticResourceRequestTests.RequestMatcherAssert(context, matcher));
return assertThat(
new StaticResourceRequestTests.RequestMatcherAssert(context, matcher));
}
private static class RequestMatcherAssert implements AssertDelegateTarget {
@ -99,13 +102,16 @@ public class StaticResourceRequestTests {
private final ServerWebExchangeMatcher matcher;
RequestMatcherAssert(StaticApplicationContext context, ServerWebExchangeMatcher matcher) {
RequestMatcherAssert(StaticApplicationContext context,
ServerWebExchangeMatcher matcher) {
this.context = context;
this.matcher = matcher;
}
void matches(String path) {
ServerWebExchange exchange = webHandler().createExchange(MockServerHttpRequest.get(path).build(), new MockServerHttpResponse());
ServerWebExchange exchange = webHandler().createExchange(
MockServerHttpRequest.get(path).build(),
new MockServerHttpResponse());
matches(exchange);
}
@ -115,7 +121,9 @@ public class StaticResourceRequestTests {
}
void doesNotMatch(String path) {
ServerWebExchange exchange = webHandler().createExchange(MockServerHttpRequest.get(path).build(), new MockServerHttpResponse());
ServerWebExchange exchange = webHandler().createExchange(
MockServerHttpRequest.get(path).build(),
new MockServerHttpResponse());
doesNotMatch(exchange);
}
@ -125,7 +133,8 @@ public class StaticResourceRequestTests {
}
private TestHttpWebHandlerAdapter webHandler() {
TestHttpWebHandlerAdapter adapter = new TestHttpWebHandlerAdapter(mock(WebHandler.class));
TestHttpWebHandlerAdapter adapter = new TestHttpWebHandlerAdapter(
mock(WebHandler.class));
adapter.setApplicationContext(this.context);
return adapter;
}
@ -143,7 +152,8 @@ public class StaticResourceRequestTests {
}
@Override
protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttpResponse response) {
protected ServerWebExchange createExchange(ServerHttpRequest request,
ServerHttpResponse response) {
return super.createExchange(request, response);
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -92,7 +92,8 @@ public class StaticResourceRequestTests {
public void excludeFromSetWhenSetIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Locations must not be null");
StaticResourceRequest.toCommonLocations().excluding((Set<StaticResourceLocation>) null);
StaticResourceRequest.toCommonLocations()
.excluding((Set<StaticResourceLocation>) null);
}
private RequestMatcherAssert assertMatcher(RequestMatcher matcher) {

@ -247,8 +247,8 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests {
.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context)
.build();
String body = client.get().uri("/notfound").accept(MediaType.TEXT_HTML)
.exchange().expectStatus()
String body = client.get().uri("/notfound")
.accept(MediaType.TEXT_HTML).exchange().expectStatus()
.isEqualTo(HttpStatus.NOT_FOUND).expectHeader()
.contentType(MediaType.TEXT_HTML).expectBody(String.class)
.returnResult().getResponseBody();

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

@ -80,8 +80,8 @@ public class SampleSecureWebFluxCustomSecurityTests {
@Test
public void staticResourceShouldBeAccessible() {
this.webClient.get().uri("/css/bootstrap.min.css").accept(MediaType.APPLICATION_JSON).exchange()
.expectStatus().isOk();
this.webClient.get().uri("/css/bootstrap.min.css")
.accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk();
}
@Configuration
@ -100,9 +100,10 @@ public class SampleSecureWebFluxCustomSecurityTests {
SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.authorizeExchange().matchers(EndpointRequest.to("health", "info"))
.permitAll().matchers(EndpointRequest.toAnyEndpoint())
.hasRole("ACTUATOR").matchers(StaticResourceRequest.toCommonLocations()).permitAll()
.pathMatchers("/login").permitAll().anyExchange()
.authenticated().and().httpBasic();
.hasRole("ACTUATOR")
.matchers(StaticResourceRequest.toCommonLocations()).permitAll()
.pathMatchers("/login").permitAll().anyExchange().authenticated()
.and().httpBasic();
return http.build();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.

Loading…
Cancel
Save