Polish reactive config

Code Checkstyle fixes
This commit is contained in:
Rob Winch 2017-09-12 21:56:09 -05:00
parent 8b32b8db74
commit b5edb58050
4 changed files with 61 additions and 55 deletions

View File

@ -66,7 +66,7 @@ public class EnableWebFluxSecurityTests {
@Test @Test
public void defaultRequiresAuthentication() { public void defaultRequiresAuthentication() {
WebTestClient client = WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build(); WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build();
client.get() client.get()
.uri("/") .uri("/")
@ -81,7 +81,7 @@ public class EnableWebFluxSecurityTests {
WebTestClient client = WebTestClientBuilder.bindToWebFilters( WebTestClient client = WebTestClientBuilder.bindToWebFilters(
(exchange, chain) -> (exchange, chain) ->
chain.filter(exchange.mutate().principal(Mono.just(currentPrincipal)).build()), chain.filter(exchange.mutate().principal(Mono.just(currentPrincipal)).build()),
springSecurityFilterChain, this.springSecurityFilterChain,
(exchange,chain) -> (exchange,chain) ->
Mono.subscriberContext() Mono.subscriberContext()
.flatMap( c -> c.<Mono<Principal>>get(Authentication.class)) .flatMap( c -> c.<Mono<Principal>>get(Authentication.class))
@ -100,7 +100,7 @@ public class EnableWebFluxSecurityTests {
@Test @Test
public void defaultPopulatesReactorContextWhenAuthenticating() { public void defaultPopulatesReactorContextWhenAuthenticating() {
WebTestClient client = WebTestClientBuilder.bindToWebFilters( WebTestClient client = WebTestClientBuilder.bindToWebFilters(
springSecurityFilterChain, this.springSecurityFilterChain,
(exchange,chain) -> (exchange,chain) ->
Mono.subscriberContext() Mono.subscriberContext()
.flatMap( c -> c.<Mono<Principal>>get(Authentication.class)) .flatMap( c -> c.<Mono<Principal>>get(Authentication.class))
@ -140,7 +140,7 @@ public class EnableWebFluxSecurityTests {
@Test @Test
public void passwordEncoderBeanIsUsed() { public void passwordEncoderBeanIsUsed() {
WebTestClient client = WebTestClientBuilder.bindToWebFilters( WebTestClient client = WebTestClientBuilder.bindToWebFilters(
springSecurityFilterChain, this.springSecurityFilterChain,
(exchange,chain) -> (exchange,chain) ->
Mono.subscriberContext() Mono.subscriberContext()
.flatMap( c -> c.<Mono<Principal>>get(Authentication.class)) .flatMap( c -> c.<Mono<Principal>>get(Authentication.class))
@ -185,7 +185,7 @@ public class EnableWebFluxSecurityTests {
@Test @Test
public void formLoginWorks() { public void formLoginWorks() {
WebTestClient client = WebTestClientBuilder.bindToWebFilters( WebTestClient client = WebTestClientBuilder.bindToWebFilters(
springSecurityFilterChain, this.springSecurityFilterChain,
(exchange,chain) -> (exchange,chain) ->
Mono.subscriberContext() Mono.subscriberContext()
.flatMap( c -> c.<Mono<Principal>>get(Authentication.class)) .flatMap( c -> c.<Mono<Principal>>get(Authentication.class))
@ -227,7 +227,7 @@ public class EnableWebFluxSecurityTests {
@Test @Test
public void multiWorks() { public void multiWorks() {
WebTestClient client = WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build(); WebTestClient client = WebTestClientBuilder.bindToWebFilters(this.springSecurityFilterChain).build();
client.get() client.get()
.uri("/api/test") .uri("/api/test")

View File

@ -33,8 +33,8 @@ public class AuthorizeExchangeBuilderTests {
@Test @Test
public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() { public void antMatchersWhenMethodAndPatternsThenDiscriminatesByMethod() {
authorization.pathMatchers(HttpMethod.POST, "/a", "/b").denyAll(); this.authorization.pathMatchers(HttpMethod.POST, "/a", "/b").denyAll();
authorization.anyExchange().permitAll(); this.authorization.anyExchange().permitAll();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -62,8 +62,8 @@ public class AuthorizeExchangeBuilderTests {
@Test @Test
public void antMatchersWhenPatternsThenAnyMethod() { public void antMatchersWhenPatternsThenAnyMethod() {
authorization.pathMatchers("/a", "/b").denyAll(); this.authorization.pathMatchers("/a", "/b").denyAll();
authorization.anyExchange().permitAll(); this.authorization.anyExchange().permitAll();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -90,23 +90,24 @@ public class AuthorizeExchangeBuilderTests {
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() { public void antMatchersWhenNoAccessAndAnotherMatcherThenThrowsException() {
authorization.pathMatchers("/incomplete"); this.authorization.pathMatchers("/incomplete");
authorization.pathMatchers("/throws-exception"); this.authorization.pathMatchers("/throws-exception");
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void anyExchangeWhenFollowedByMatcherThenThrowsException() { public void anyExchangeWhenFollowedByMatcherThenThrowsException() {
authorization.anyExchange().denyAll(); this.authorization.anyExchange().denyAll();
authorization.pathMatchers("/never-reached"); this.authorization.pathMatchers("/never-reached");
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
public void buildWhenMatcherDefinedWithNoAccessThenThrowsException() { public void buildWhenMatcherDefinedWithNoAccessThenThrowsException() {
authorization.pathMatchers("/incomplete"); this.authorization.pathMatchers("/incomplete");
authorization.build(); this.authorization.build();
} }
private WebTestClient buildClient() { private WebTestClient buildClient() {
return WebTestClientBuilder.bindToWebFilters(new ExceptionTranslationWebFilter(), authorization.build()).build(); return WebTestClientBuilder.bindToWebFilters(new ExceptionTranslationWebFilter(),
this.authorization.build()).build();
} }
} }

View File

@ -51,13 +51,15 @@ public class HeaderBuilderTests {
@Before @Before
public void setup() { public void setup() {
expectedHeaders.add(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains"); this.expectedHeaders.add(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=31536000 ; includeSubDomains");
expectedHeaders.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate"); this.expectedHeaders.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate");
expectedHeaders.add(HttpHeaders.PRAGMA, "no-cache"); this.expectedHeaders.add(HttpHeaders.PRAGMA, "no-cache");
expectedHeaders.add(HttpHeaders.EXPIRES, "0"); this.expectedHeaders.add(HttpHeaders.EXPIRES, "0");
expectedHeaders.add(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS, "nosniff"); this.expectedHeaders
expectedHeaders.add(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS, "DENY"); .add(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS, "nosniff");
expectedHeaders.add(XXssProtectionHttpHeadersWriter.X_XSS_PROTECTION, "1 ; mode=block"); this.expectedHeaders.add(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS, "DENY");
this.expectedHeaders
.add(XXssProtectionHttpHeadersWriter.X_XSS_PROTECTION, "1 ; mode=block");
} }
@Test @Test
@ -67,61 +69,62 @@ public class HeaderBuilderTests {
@Test @Test
public void headersWhenCacheDisableThenCacheNotWritten() { public void headersWhenCacheDisableThenCacheNotWritten() {
expectedHeaders.remove(HttpHeaders.CACHE_CONTROL); this.expectedHeaders.remove(HttpHeaders.CACHE_CONTROL);
expectedHeaders.remove(HttpHeaders.PRAGMA); this.expectedHeaders.remove(HttpHeaders.PRAGMA);
expectedHeaders.remove(HttpHeaders.EXPIRES); this.expectedHeaders.remove(HttpHeaders.EXPIRES);
headers.cache().disable(); this.headers.cache().disable();
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenContentOptionsDisableThenContentTypeOptionsNotWritten() { public void headersWhenContentOptionsDisableThenContentTypeOptionsNotWritten() {
expectedHeaders.remove(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS); this.expectedHeaders.remove(ContentTypeOptionsHttpHeadersWriter.X_CONTENT_OPTIONS);
headers.contentTypeOptions().disable(); this.headers.contentTypeOptions().disable();
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenHstsDisableThenHstsNotWritten() { public void headersWhenHstsDisableThenHstsNotWritten() {
expectedHeaders.remove(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY); this.expectedHeaders.remove(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY);
headers.hsts().disable(); this.headers.hsts().disable();
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenHstsCustomThenCustomHstsWritten() { public void headersWhenHstsCustomThenCustomHstsWritten() {
expectedHeaders.remove(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY); this.expectedHeaders.remove(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY);
expectedHeaders.add(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=60"); this.expectedHeaders.add(StrictTransportSecurityHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=60");
headers.hsts().maxAge(Duration.ofSeconds(60)); this.headers.hsts().maxAge(Duration.ofSeconds(60));
headers.hsts().includeSubdomains(false); this.headers.hsts().includeSubdomains(false);
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenFrameOptionsDisableThenFrameOptionsNotWritten() { public void headersWhenFrameOptionsDisableThenFrameOptionsNotWritten() {
expectedHeaders.remove(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS); this.expectedHeaders.remove(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS);
headers.frameOptions().disable(); this.headers.frameOptions().disable();
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenFrameOptionsModeThenFrameOptionsCustomMode() { public void headersWhenFrameOptionsModeThenFrameOptionsCustomMode() {
expectedHeaders.remove(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS); this.expectedHeaders.remove(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS);
expectedHeaders.add(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS, "SAMEORIGIN"); this.expectedHeaders
headers.frameOptions().mode(XFrameOptionsHttpHeadersWriter.Mode.SAMEORIGIN); .add(XFrameOptionsHttpHeadersWriter.X_FRAME_OPTIONS, "SAMEORIGIN");
this.headers.frameOptions().mode(XFrameOptionsHttpHeadersWriter.Mode.SAMEORIGIN);
assertHeaders(); assertHeaders();
} }
@Test @Test
public void headersWhenXssProtectionDisableThenXssProtectionNotWritten() { public void headersWhenXssProtectionDisableThenXssProtectionNotWritten() {
expectedHeaders.remove("X-Xss-Protection"); this.expectedHeaders.remove("X-Xss-Protection");
headers.xssProtection().disable(); this.headers.xssProtection().disable();
assertHeaders(); assertHeaders();
} }
@ -134,12 +137,13 @@ public class HeaderBuilderTests {
.returnResult(String.class); .returnResult(String.class);
Map<String,List<String>> responseHeaders = response.getResponseHeaders(); Map<String,List<String>> responseHeaders = response.getResponseHeaders();
ignoredHeaderNames.stream().forEach(responseHeaders::remove); this.ignoredHeaderNames.stream().forEach(responseHeaders::remove);
assertThat(responseHeaders).describedAs(response.toString()).isEqualTo(expectedHeaders); assertThat(responseHeaders).describedAs(response.toString()).isEqualTo(
this.expectedHeaders);
} }
private WebTestClient buildClient() { private WebTestClient buildClient() {
return WebTestClientBuilder.bindToWebFilters(headers.build()).build(); return WebTestClientBuilder.bindToWebFilters(this.headers.build()).build();
} }
} }

View File

@ -56,12 +56,12 @@ public class HttpSecurityTests {
@Before @Before
public void setup() { public void setup() {
http = HttpSecurity.http(); this.http = HttpSecurity.http();
} }
@Test @Test
public void defaults() { public void defaults() {
http.securityContextRepository(this.contextRepository); this.http.securityContextRepository(this.contextRepository);
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -73,17 +73,17 @@ public class HttpSecurityTests {
assertThat(result.getResponseCookies()).isEmpty(); assertThat(result.getResponseCookies()).isEmpty();
// there is no need to try and load the SecurityContext by default // there is no need to try and load the SecurityContext by default
verifyZeroInteractions(contextRepository); verifyZeroInteractions(this.contextRepository);
} }
@Test @Test
public void basic() { public void basic() {
given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("rob", "rob", "ROLE_USER", "ROLE_ADMIN"))); given(this.authenticationManager.authenticate(any())).willReturn(Mono.just(new TestingAuthenticationToken("rob", "rob", "ROLE_USER", "ROLE_ADMIN")));
http.securityContextRepository(new WebSessionSecurityContextRepository()); this.http.securityContextRepository(new WebSessionSecurityContextRepository());
http.httpBasic(); this.http.httpBasic();
http.authenticationManager(authenticationManager); this.http.authenticationManager(this.authenticationManager);
HttpSecurity.AuthorizeExchangeBuilder authorize = http.authorizeExchange(); HttpSecurity.AuthorizeExchangeBuilder authorize = this.http.authorizeExchange();
authorize.anyExchange().authenticated(); authorize.anyExchange().authenticated();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
@ -105,7 +105,7 @@ public class HttpSecurityTests {
@Test @Test
public void basicWhenNoCredentialsThenUnauthorized() { public void basicWhenNoCredentialsThenUnauthorized() {
http.authorizeExchange().anyExchange().authenticated(); this.http.authorizeExchange().anyExchange().authenticated();
WebTestClient client = buildClient(); WebTestClient client = buildClient();
client client
@ -118,7 +118,8 @@ public class HttpSecurityTests {
} }
private WebTestClient buildClient() { private WebTestClient buildClient() {
WebFilterChainFilter springSecurityFilterChain = WebFilterChainFilter.fromSecurityWebFilterChains(http.build()); WebFilterChainFilter springSecurityFilterChain = WebFilterChainFilter.fromSecurityWebFilterChains(
this.http.build());
return WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build(); return WebTestClientBuilder.bindToWebFilters(springSecurityFilterChain).build();
} }
} }