Просмотр исходного кода

Remove unnecessary lambda blocks

Remove lambda blocks that aren't needed and replace instead with a
simple expression.

Issue gh-8945
Phillip Webb 5 лет назад
Родитель
Сommit
612fb22a7f
13 измененных файлов с 48 добавлено и 73 удалено
  1. 5 6
      config/src/integration-test/java/org/springframework/security/config/annotation/rsocket/RSocketMessageHandlerITests.java
  2. 3 5
      config/src/test/java/org/springframework/security/config/web/server/HeaderSpecTests.java
  3. 1 3
      config/src/test/java/org/springframework/security/htmlunit/server/HtmlUnitWebTestClient.java
  4. 1 3
      core/src/test/java/org/springframework/security/jackson2/SecurityJackson2ModulesTests.java
  5. 1 2
      etc/checkstyle/checkstyle-suppressions.xml
  6. 8 10
      oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/DefaultReactiveOAuth2AuthorizedClientManager.java
  7. 10 14
      oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/reactive/function/client/ServerOAuth2AuthorizedClientExchangeFilterFunction.java
  8. 8 8
      oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthorizationCodeAuthenticationProviderTests.java
  9. 2 7
      oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java
  10. 4 6
      oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/HeaderBearerTokenResolverTests.java
  11. 2 3
      rsocket/src/test/java/org/springframework/security/rsocket/core/PayloadInterceptorRSocketTests.java
  12. 1 3
      web/src/main/java/org/springframework/security/web/server/header/StaticServerHttpHeadersWriter.java
  13. 2 3
      web/src/test/java/org/springframework/security/web/method/annotation/CurrentSecurityContextArgumentResolverTests.java

+ 5 - 6
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<Void> send(Mono<String> payload) {
-			return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> {
-				doNotifyAll();
-			}));
+			return payload.doOnNext(this::add).then(Mono.fromRunnable(() -> doNotifyAll()));
+
 		}
 
 		private synchronized void doNotifyAll() {

+ 3 - 5
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();
 	}

+ 1 - 3
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<String, String> clientCookies() {

+ 1 - 3
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

+ 1 - 2
etc/checkstyle/checkstyle-suppressions.xml

@@ -3,8 +3,6 @@
 		"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
 		"https://checkstyle.org/dtds/suppressions_1_2.dtd">
 <suppressions>
-	<suppress files=".*" checks="SpringJavadoc" />
-	<suppress files=".*" checks="SpringLambda" />
 	<suppress files=".*" checks="SpringMethodOrder" />
 	<suppress files=".*" checks="SpringMethodVisibility" />
 	<suppress files=".*" checks="SpringTernary" />
@@ -16,6 +14,7 @@
 	<suppress files=".*" checks="JavadocType" />
 	<suppress files=".*" checks="JavadocVariable" />
 	<suppress files=".*" checks="NonEmptyAtclauseDescription" />
+	<suppress files=".*" checks="SpringJavadoc" />
 
 	<!-- Ignore third-party code -->
 	<suppress files="BCrypt\.java|BCryptTests\.java" checks=".*"/>

+ 8 - 10
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(

+ 10 - 14
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(

+ 8 - 8
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

+ 2 - 7
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);
 	}
 
 }

+ 4 - 6
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

+ 2 - 3
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<PayloadInterceptor> 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

+ 1 - 3
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();
 	}

+ 2 - 3
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);
 	}