From 612fb22a7f2841ea8d1c05336ea85b3f1fee1219 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 29 Jul 2020 18:18:00 -0700 Subject: [PATCH] Remove unnecessary lambda blocks Remove lambda blocks that aren't needed and replace instead with a simple expression. Issue gh-8945 --- .../rsocket/RSocketMessageHandlerITests.java | 11 ++++----- .../config/web/server/HeaderSpecTests.java | 8 +++---- .../server/HtmlUnitWebTestClient.java | 4 +--- .../SecurityJackson2ModulesTests.java | 4 +--- etc/checkstyle/checkstyle-suppressions.xml | 3 +-- ...ReactiveOAuth2AuthorizedClientManager.java | 18 +++++++------- ...uthorizedClientExchangeFilterFunction.java | 24 ++++++++----------- ...zationCodeAuthenticationProviderTests.java | 16 ++++++------- .../oauth2/core/ClaimAccessorTests.java | 9 ++----- .../web/HeaderBearerTokenResolverTests.java | 10 ++++---- .../core/PayloadInterceptorRSocketTests.java | 5 ++-- .../header/StaticServerHttpHeadersWriter.java | 4 +--- ...tSecurityContextArgumentResolverTests.java | 5 ++-- 13 files changed, 48 insertions(+), 73 deletions(-) diff --git a/config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java b/config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java index ebe6c0f996..53e11a1c77 100644 --- a/config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java +++ b/config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java @@ -221,9 +221,9 @@ public class RSocketMessageHandlerITests { @Bean PayloadSocketAcceptorInterceptor rsocketInterceptor(RSocketSecurity rsocket) { - rsocket.authorizePayload((authorize) -> { - authorize.route("secure.*").authenticated().anyExchange().permitAll(); - }).basicAuthentication(Customizer.withDefaults()); + rsocket.authorizePayload( + (authorize) -> authorize.route("secure.*").authenticated().anyExchange().permitAll()) + .basicAuthentication(Customizer.withDefaults()); return rsocket.build(); } @@ -247,9 +247,8 @@ public class RSocketMessageHandlerITests { @MessageMapping({ "secure.send", "send" }) Mono send(Mono payload) { - return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> { - doNotifyAll(); - })); + return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> doNotifyAll())); + } private synchronized void doNotifyAll() { diff --git a/config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java b/config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java index 746da35e23..a168b477f7 100644 --- a/config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java +++ b/config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java @@ -336,12 +336,10 @@ public class HeaderSpecTests { @Test public void headersWhenCustomHeadersWriter() { this.expectedHeaders.add(CUSTOM_HEADER, CUSTOM_VALUE); - this.http.headers((headers) -> headers.writer((exchange) -> { - return Mono.just(exchange).doOnNext((it) -> { - it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE); - }).then(); + this.http.headers((headers) -> headers.writer((exchange) -> Mono.just(exchange) + .doOnNext((it) -> it.getResponse().getHeaders().add(CUSTOM_HEADER, CUSTOM_VALUE)).then() - })); + )); assertHeaders(); } diff --git a/config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java b/config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java index 44f2c57947..1859625ce4 100644 --- a/config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java +++ b/config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java @@ -186,9 +186,7 @@ final class HtmlUnitWebTestClient { } private ClientRequest withClientCookies(ClientRequest request) { - return ClientRequest.from(request).cookies((c) -> { - c.addAll(clientCookies()); - }).build(); + return ClientRequest.from(request).cookies((c) -> c.addAll(clientCookies())).build(); } private MultiValueMap clientCookies() { diff --git a/core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java b/core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java index 79fb7d4fa4..305c22adc8 100644 --- a/core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java +++ b/core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java @@ -51,9 +51,7 @@ public class SecurityJackson2ModulesTests { @Test public void readValueWhenNotAllowedOrMappedThenThrowsException() { String content = "{\"@class\":\"org.springframework.security.jackson2.SecurityJackson2ModulesTests$NotAllowlisted\",\"property\":\"bar\"}"; - assertThatThrownBy(() -> { - this.mapper.readValue(content, Object.class); - }).hasStackTraceContaining("allowlist"); + assertThatThrownBy(() -> this.mapper.readValue(content, Object.class)).hasStackTraceContaining("allowlist"); } @Test diff --git a/etc/checkstyle/checkstyle-suppressions.xml b/etc/checkstyle/checkstyle-suppressions.xml index 5c12d4242a..ff6fb32e1b 100644 --- a/etc/checkstyle/checkstyle-suppressions.xml +++ b/etc/checkstyle/checkstyle-suppressions.xml @@ -3,8 +3,6 @@ "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" "https://checkstyle.org/dtds/suppressions_1_2.dtd"> - - @@ -16,6 +14,7 @@ + diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java index aa32c5841d..c1970512ff 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java @@ -146,16 +146,14 @@ public final class DefaultReactiveOAuth2AuthorizedClientManager implements React .flatMap((serverWebExchange) -> Mono.justOrEmpty(authorizeRequest.getAuthorizedClient()) .switchIfEmpty(Mono .defer(() -> loadAuthorizedClient(clientRegistrationId, principal, serverWebExchange))) - .flatMap((authorizedClient) -> { - // Re-authorize - return authorizationContext(authorizeRequest, authorizedClient) - .flatMap((authorizationContext) -> authorize(authorizationContext, principal, - serverWebExchange)) - // Default to the existing authorizedClient if the - // client was not re-authorized - .defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null - ? authorizeRequest.getAuthorizedClient() : authorizedClient); - }).switchIfEmpty(Mono.defer(() -> + .flatMap((authorizedClient) -> // Re-authorize + authorizationContext(authorizeRequest, authorizedClient).flatMap( + (authorizationContext) -> authorize(authorizationContext, principal, serverWebExchange)) + // Default to the existing authorizedClient if the + // client was not re-authorized + .defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null + ? authorizeRequest.getAuthorizedClient() : authorizedClient)) + .switchIfEmpty(Mono.defer(() -> // Authorize this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId) .switchIfEmpty(Mono.error(() -> new IllegalArgumentException( diff --git a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java index 9970b8255d..b4a8fd8c6e 100644 --- a/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java +++ b/oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java @@ -565,20 +565,16 @@ public final class ServerOAuth2AuthorizedClientExchangeFilterFunction implements String clientRegistrationId = authorizeRequest.getClientRegistrationId(); Authentication principal = authorizeRequest.getPrincipal(); - return Mono.justOrEmpty(authorizeRequest.getAuthorizedClient()) - .switchIfEmpty(Mono.defer(() -> this.authorizedClientRepository - .loadAuthorizedClient(clientRegistrationId, principal, null))) - .flatMap((authorizedClient) -> { - // Re-authorize - return Mono - .just(OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient) - .principal(principal).build()) - .flatMap((authorizationContext) -> authorize(authorizationContext, principal)) - // Default to the existing authorizedClient if the client - // was not re-authorized - .defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null - ? authorizeRequest.getAuthorizedClient() : authorizedClient); - }).switchIfEmpty(Mono.defer(() -> + return Mono.justOrEmpty(authorizeRequest.getAuthorizedClient()).switchIfEmpty(Mono.defer( + () -> this.authorizedClientRepository.loadAuthorizedClient(clientRegistrationId, principal, null))) + .flatMap((authorizedClient) -> // Re-authorize + Mono.just(OAuth2AuthorizationContext.withAuthorizedClient(authorizedClient).principal(principal) + .build()).flatMap((authorizationContext) -> authorize(authorizationContext, principal)) + // Default to the existing authorizedClient if the client + // was not re-authorized + .defaultIfEmpty(authorizeRequest.getAuthorizedClient() != null + ? authorizeRequest.getAuthorizedClient() : authorizedClient)) + .switchIfEmpty(Mono.defer(() -> // Authorize this.clientRegistrationRepository.findByRegistrationId(clientRegistrationId) .switchIfEmpty(Mono.error(() -> new IllegalArgumentException( diff --git a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java index e8d655ae23..3d82ba7717 100644 --- a/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java +++ b/oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java @@ -85,10 +85,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests { OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest, authorizationResponse); - assertThatThrownBy(() -> { - this.authenticationProvider.authenticate( - new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange)); - }).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining(OAuth2ErrorCodes.INVALID_REQUEST); + assertThatThrownBy(() -> this.authenticationProvider.authenticate( + new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange))) + .isInstanceOf(OAuth2AuthorizationException.class) + .hasMessageContaining(OAuth2ErrorCodes.INVALID_REQUEST); } @Test @@ -98,10 +98,10 @@ public class OAuth2AuthorizationCodeAuthenticationProviderTests { OAuth2AuthorizationExchange authorizationExchange = new OAuth2AuthorizationExchange(this.authorizationRequest, authorizationResponse); - assertThatThrownBy(() -> { - this.authenticationProvider.authenticate( - new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange)); - }).isInstanceOf(OAuth2AuthorizationException.class).hasMessageContaining("invalid_state_parameter"); + assertThatThrownBy(() -> this.authenticationProvider.authenticate( + new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, authorizationExchange))) + .isInstanceOf(OAuth2AuthorizationException.class) + .hasMessageContaining("invalid_state_parameter"); } @Test diff --git a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java index a51174b003..272e74c75c 100644 --- a/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java +++ b/oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java @@ -27,7 +27,7 @@ import org.junit.Before; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.catchThrowable; +import static org.assertj.core.api.Assertions.assertThatObject; /** * Tests for {@link ClaimAccessor}. @@ -142,12 +142,7 @@ public class ClaimAccessorTests { String expectedClaimValue = "true"; String claimName = "boolean"; this.claims.put(claimName, expectedClaimValue); - - Throwable thrown = catchThrowable(() -> { - boolean actualClaimValue = this.claimAccessor.getClaim(claimName); - }); - - assertThat(thrown).isInstanceOf(ClassCastException.class); + assertThatObject(this.claimAccessor.getClaim(claimName)).isNotInstanceOf(Boolean.class); } } diff --git a/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java b/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java index 20121017c0..5be30212f0 100644 --- a/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java +++ b/oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java @@ -38,16 +38,14 @@ public class HeaderBearerTokenResolverTests { @Test public void constructorWhenHeaderNullThenThrowIllegalArgumentException() { - assertThatCode(() -> { - new HeaderBearerTokenResolver(null); - }).isInstanceOf(IllegalArgumentException.class).hasMessage("header cannot be empty"); + assertThatCode(() -> new HeaderBearerTokenResolver(null)).isInstanceOf(IllegalArgumentException.class) + .hasMessage("header cannot be empty"); } @Test public void constructorWhenHeaderEmptyThenThrowIllegalArgumentException() { - assertThatCode(() -> { - new HeaderBearerTokenResolver(""); - }).isInstanceOf(IllegalArgumentException.class).hasMessage("header cannot be empty"); + assertThatCode(() -> new HeaderBearerTokenResolver("")).isInstanceOf(IllegalArgumentException.class) + .hasMessage("header cannot be empty"); } @Test diff --git a/rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java b/rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java index a96324d2f6..d41d5b2541 100644 --- a/rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java +++ b/rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java @@ -94,9 +94,8 @@ public class PayloadInterceptorRSocketTests { public void constructorWhenNullDelegateThenException() { this.delegate = null; List interceptors = Arrays.asList(this.interceptor); - assertThatCode(() -> { - new PayloadInterceptorRSocket(this.delegate, interceptors, this.metadataMimeType, this.dataMimeType); - }).isInstanceOf(IllegalArgumentException.class); + assertThatCode(() -> new PayloadInterceptorRSocket(this.delegate, interceptors, this.metadataMimeType, + this.dataMimeType)).isInstanceOf(IllegalArgumentException.class); } @Test diff --git a/web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java b/web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java index 9be1b23891..1f636f5cd3 100644 --- a/web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java +++ b/web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java @@ -43,9 +43,7 @@ public class StaticServerHttpHeadersWriter implements ServerHttpHeadersWriter { HttpHeaders headers = exchange.getResponse().getHeaders(); boolean containsOneHeaderToAdd = Collections.disjoint(headers.keySet(), this.headersToAdd.keySet()); if (containsOneHeaderToAdd) { - this.headersToAdd.forEach((name, values) -> { - headers.put(name, values); - }); + this.headersToAdd.forEach(headers::put); } return Mono.empty(); } diff --git a/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java b/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java index ae55230c7a..7dfb2df30c 100644 --- a/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java +++ b/web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java @@ -111,9 +111,8 @@ public class CurrentSecurityContextArgumentResolverTests { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); context.setAuthentication(null); - assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> { - this.resolver.resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null); - }); + assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() -> this.resolver + .resolveArgument(showSecurityContextAuthenticationWithPrincipal(), null, null, null)); context.setAuthentication(authentication); }