From bc7fc905500c5b379fa0feca9b3fb395fbbf1cd5 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Wed, 25 Jan 2023 01:44:46 +0900 Subject: [PATCH] Replace Base64Utils with JDK's Base64 See gh-33967 --- .../artifactory/ArtifactoryServiceTests.java | 5 ++-- .../autoconfigure/cloudfoundry/Token.java | 6 ++--- .../reactive/ReactiveTokenValidator.java | 4 ++-- .../cloudfoundry/servlet/TokenValidator.java | 4 ++-- .../cloudfoundry/TokenTests.java | 24 +++++++++---------- ...oundryWebFluxEndpointIntegrationTests.java | 4 ++-- ...eCloudFoundrySecurityInterceptorTests.java | 5 ++-- .../reactive/ReactiveTokenValidatorTests.java | 10 ++++---- ...FoundryMvcWebEndpointIntegrationTests.java | 4 ++-- .../CloudFoundrySecurityInterceptorTests.java | 5 ++-- .../servlet/TokenValidatorTests.java | 10 ++++---- .../boot/devtools/livereload/Connection.java | 4 ++-- .../MockMvcSecurityIntegrationTests.java | 9 ++++--- .../web/client/TestRestTemplateTests.java | 6 ++--- ...onEncodedDockerRegistryAuthentication.java | 5 ++-- .../docker/ssl/CertificateParser.java | 5 ++-- .../platform/docker/ssl/PrivateKeyParser.java | 5 ++-- ...ockerRegistryTokenAuthenticationTests.java | 4 ++-- ...DockerRegistryUserAuthenticationTests.java | 4 ++-- .../tasks/bundling/DockerSpecTests.java | 4 ++-- .../boot/maven/DockerTests.java | 5 ++-- .../boot/web/server/CertificateParser.java | 4 ++-- .../boot/web/server/PrivateKeyParser.java | 4 ++-- 23 files changed, 73 insertions(+), 67 deletions(-) diff --git a/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/artifactory/ArtifactoryServiceTests.java b/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/artifactory/ArtifactoryServiceTests.java index 19ab95e1255..e1748f06fe5 100644 --- a/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/artifactory/ArtifactoryServiceTests.java +++ b/ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/artifactory/ArtifactoryServiceTests.java @@ -16,6 +16,8 @@ package io.spring.concourse.releasescripts.artifactory; +import java.util.Base64; + import io.spring.concourse.releasescripts.ReleaseInfo; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -29,7 +31,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.response.DefaultResponseCreator; -import org.springframework.util.Base64Utils; import org.springframework.web.client.HttpClientErrorException; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -69,7 +70,7 @@ class ArtifactoryServiceTests { .andExpect(method(HttpMethod.POST)) .andExpect(content().json( "{\"status\": \"staged\", \"sourceRepo\": \"libs-staging-local\", \"targetRepo\": \"libs-milestone-local\"}")) - .andExpect(header("Authorization", "Basic " + Base64Utils.encodeToString(String + .andExpect(header("Authorization", "Basic " + Base64.getEncoder().encodeToString(String .format("%s:%s", this.properties.getUsername(), this.properties.getPassword()).getBytes()))) .andExpect(header("Content-Type", MediaType.APPLICATION_JSON.toString())).andRespond(withSuccess()); this.service.promote("libs-milestone-local", getReleaseInfo()); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java index 813073a37a0..07a0973379a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java @@ -17,12 +17,12 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry; import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.List; import java.util.Map; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; import org.springframework.boot.json.JsonParserFactory; -import org.springframework.util.Base64Utils; import org.springframework.util.StringUtils; /** @@ -60,7 +60,7 @@ public class Token { private Map parseJson(String base64) { try { - byte[] bytes = Base64Utils.decodeFromUrlSafeString(base64); + byte[] bytes = Base64.getUrlDecoder().decode(base64); return JsonParserFactory.getJsonParser().parseMap(new String(bytes, StandardCharsets.UTF_8)); } catch (RuntimeException ex) { @@ -73,7 +73,7 @@ public class Token { } public byte[] getSignature() { - return Base64Utils.decodeFromUrlSafeString(this.signature); + return Base64.getUrlDecoder().decode(this.signature); } public String getSignatureAlgorithm() { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidator.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidator.java index 6c3df56d0d9..6decc61033b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidator.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidator.java @@ -23,6 +23,7 @@ import java.security.PublicKey; import java.security.Signature; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -33,7 +34,6 @@ import reactor.core.publisher.Mono; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token; -import org.springframework.util.Base64Utils; /** * Validator used to ensure that a signed {@link Token} has not been tampered with. @@ -108,7 +108,7 @@ class ReactiveTokenValidator { key = key.replace("-----BEGIN PUBLIC KEY-----\n", ""); key = key.replace("-----END PUBLIC KEY-----", ""); key = key.trim().replace("\n", ""); - byte[] bytes = Base64Utils.decodeFromString(key); + byte[] bytes = Base64.getDecoder().decode(key); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); return KeyFactory.getInstance("RSA").generatePublic(keySpec); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidator.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidator.java index 0746536c837..58bb7822cfc 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidator.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidator.java @@ -23,13 +23,13 @@ import java.security.PublicKey; import java.security.Signature; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; import java.util.Map; import java.util.concurrent.TimeUnit; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token; -import org.springframework.util.Base64Utils; /** * Validator used to ensure that a signed {@link Token} has not been tampered with. @@ -102,7 +102,7 @@ class TokenValidator { key = key.replace("-----BEGIN PUBLIC KEY-----\n", ""); key = key.replace("-----END PUBLIC KEY-----", ""); key = key.trim().replace("\n", ""); - byte[] bytes = Base64Utils.decodeFromString(key); + byte[] bytes = Base64.getDecoder().decode(key); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); return KeyFactory.getInstance("RSA").generatePublic(keySpec); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/TokenTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/TokenTests.java index 45e9e2f3621..48106991512 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/TokenTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/TokenTests.java @@ -16,12 +16,12 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry; +import java.util.Base64; import java.util.function.Consumer; import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; -import org.springframework.util.Base64Utils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -44,8 +44,8 @@ class TokenTests { String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}"; String claims = "invalid-claims"; assertThatExceptionOfType(CloudFoundryAuthorizationException.class) - .isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes()) + "." - + Base64Utils.encodeToString(claims.getBytes()))) + .isThrownBy(() -> new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "." + + Base64.getEncoder().encodeToString(claims.getBytes()))) .satisfies(reasonRequirement(Reason.INVALID_TOKEN)); } @@ -54,8 +54,8 @@ class TokenTests { String header = "invalid-header"; String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}"; assertThatExceptionOfType(CloudFoundryAuthorizationException.class) - .isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes()) + "." - + Base64Utils.encodeToString(claims.getBytes()))) + .isThrownBy(() -> new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "." + + Base64.getEncoder().encodeToString(claims.getBytes()))) .satisfies(reasonRequirement(Reason.INVALID_TOKEN)); } @@ -71,16 +71,16 @@ class TokenTests { void validJwt() { String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}"; String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}"; - String content = Base64Utils.encodeToString(header.getBytes()) + "." - + Base64Utils.encodeToString(claims.getBytes()); - String signature = Base64Utils.encodeToString("signature".getBytes()); + String content = Base64.getEncoder().encodeToString(header.getBytes()) + "." + + Base64.getEncoder().encodeToString(claims.getBytes()); + String signature = Base64.getEncoder().encodeToString("signature".getBytes()); Token token = new Token(content + "." + signature); assertThat(token.getExpiry()).isEqualTo(2147483647); assertThat(token.getIssuer()).isEqualTo("http://localhost:8080/uaa/oauth/token"); assertThat(token.getSignatureAlgorithm()).isEqualTo("RS256"); assertThat(token.getKeyId()).isEqualTo("key-id"); assertThat(token.getContent()).isEqualTo(content.getBytes()); - assertThat(token.getSignature()).isEqualTo(Base64Utils.decodeFromString(signature)); + assertThat(token.getSignature()).isEqualTo(Base64.getDecoder().decode(signature)); } @Test @@ -120,9 +120,9 @@ class TokenTests { } private Token createToken(String header, String claims) { - Token token = new Token( - Base64Utils.encodeToString(header.getBytes()) + "." + Base64Utils.encodeToString(claims.getBytes()) - + "." + Base64Utils.encodeToString("signature".getBytes())); + Token token = new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "." + + Base64.getEncoder().encodeToString(claims.getBytes()) + "." + + Base64.getEncoder().encodeToString("signature".getBytes())); return token; } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java index e412d347317..d8d20c95f1a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive; import java.time.Duration; import java.util.Arrays; +import java.util.Base64; import java.util.Collections; import java.util.Map; import java.util.function.Consumer; @@ -54,7 +55,6 @@ import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; -import org.springframework.util.Base64Utils; import org.springframework.web.cors.CorsConfiguration; import static org.mockito.ArgumentMatchers.any; @@ -160,7 +160,7 @@ class CloudFoundryWebFluxEndpointIntegrationTests { private String mockAccessToken() { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu" + "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ." - + Base64Utils.encodeToString("signature".getBytes()); + + Base64.getEncoder().encodeToString("signature".getBytes()); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundrySecurityInterceptorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundrySecurityInterceptorTests.java index 5e88fd55cb6..f7b9bf5eda6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundrySecurityInterceptorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundrySecurityInterceptorTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive; +import java.util.Base64; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -31,7 +33,6 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; import org.springframework.mock.web.server.MockServerWebExchange; -import org.springframework.util.Base64Utils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -151,7 +152,7 @@ class ReactiveCloudFoundrySecurityInterceptorTests { private String mockAccessToken() { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu" + "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ." - + Base64Utils.encodeToString("signature".getBytes()); + + Base64.getEncoder().encodeToString("signature".getBytes()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java index 4e0e0687890..bb0ccb61004 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java @@ -25,6 +25,7 @@ import java.security.PrivateKey; import java.security.Signature; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; +import java.util.Base64; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -42,7 +43,6 @@ import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryA import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token; import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.util.Base64Utils; import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -251,11 +251,11 @@ class ReactiveTokenValidatorTests { PrivateKey privateKey = getPrivateKey(); Signature signature = Signature.getInstance("SHA256WithRSA"); signature.initSign(privateKey); - byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims)); + byte[] content = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getEncoder().encode(claims)); signature.update(content); byte[] crypto = signature.sign(); - byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims), - Base64Utils.encodeUrlSafe(crypto)); + byte[] token = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getUrlEncoder().encode(claims), + Base64.getUrlEncoder().encode(crypto)); return new String(token, StandardCharsets.UTF_8); } @@ -292,7 +292,7 @@ class ReactiveTokenValidatorTests { String privateKey = signingKey.replace("-----BEGIN PRIVATE KEY-----\n", ""); privateKey = privateKey.replace("-----END PRIVATE KEY-----", ""); privateKey = privateKey.replace("\n", ""); - byte[] pkcs8EncodedBytes = Base64Utils.decodeFromString(privateKey); + byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(privateKey); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(keySpec); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryMvcWebEndpointIntegrationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryMvcWebEndpointIntegrationTests.java index 7ec1d07d6bb..0a556e1c3b9 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryMvcWebEndpointIntegrationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryMvcWebEndpointIntegrationTests.java @@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet; import java.time.Duration; import java.util.Arrays; +import java.util.Base64; import java.util.Collections; import java.util.Map; import java.util.function.BiConsumer; @@ -48,7 +49,6 @@ import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.test.web.reactive.server.WebTestClient; -import org.springframework.util.Base64Utils; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -155,7 +155,7 @@ class CloudFoundryMvcWebEndpointIntegrationTests { private String mockAccessToken() { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu" + "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ." - + Base64Utils.encodeToString("signature".getBytes()); + + Base64.getEncoder().encodeToString("signature".getBytes()); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java index a5065512da1..5b95e0f3bf0 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet; +import java.util.Base64; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -31,7 +33,6 @@ import org.springframework.boot.actuate.endpoint.EndpointId; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.util.Base64Utils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; @@ -139,7 +140,7 @@ class CloudFoundrySecurityInterceptorTests { private String mockAccessToken() { return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu" + "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ." - + Base64Utils.encodeToString("signature".getBytes()); + + Base64.getEncoder().encodeToString("signature".getBytes()); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java index 9859a601c12..977874620f5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidatorTests.java @@ -25,6 +25,7 @@ import java.security.PrivateKey; import java.security.Signature; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; +import java.util.Base64; import java.util.Collections; import java.util.Map; import java.util.function.Consumer; @@ -39,7 +40,6 @@ import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryA import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason; import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token; import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.util.Base64Utils; import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -193,11 +193,11 @@ class TokenValidatorTests { PrivateKey privateKey = getPrivateKey(); Signature signature = Signature.getInstance("SHA256WithRSA"); signature.initSign(privateKey); - byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims)); + byte[] content = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getEncoder().encode(claims)); signature.update(content); byte[] crypto = signature.sign(); - byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims), - Base64Utils.encodeUrlSafe(crypto)); + byte[] token = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getUrlEncoder().encode(claims), + Base64.getUrlEncoder().encode(crypto)); return new String(token, StandardCharsets.UTF_8); } @@ -234,7 +234,7 @@ class TokenValidatorTests { String privateKey = signingKey.replace("-----BEGIN PRIVATE KEY-----\n", ""); privateKey = privateKey.replace("-----END PRIVATE KEY-----", ""); privateKey = privateKey.replace("\n", ""); - byte[] pkcs8EncodedBytes = Base64Utils.decodeFromString(privateKey); + byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(privateKey); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(keySpec); diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java index 7c332ef3bcd..e4cc02047b3 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/livereload/Connection.java @@ -23,6 +23,7 @@ import java.net.Socket; import java.net.SocketTimeoutException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Base64; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -31,7 +32,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.core.log.LogMessage; import org.springframework.util.Assert; -import org.springframework.util.Base64Utils; /** * A {@link LiveReloadServer} connection. @@ -148,7 +148,7 @@ class Connection { String response = matcher.group(1).trim() + WEBSOCKET_GUID; MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); messageDigest.update(response.getBytes(), 0, response.length()); - return Base64Utils.encodeToString(messageDigest.digest()); + return Base64.getEncoder().encodeToString(messageDigest.digest()); } /** diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/MockMvcSecurityIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/MockMvcSecurityIntegrationTests.java index 8a4ead9d31f..d76fe97b42f 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/MockMvcSecurityIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/MockMvcSecurityIntegrationTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.test.autoconfigure.security; +import java.util.Base64; + import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +27,6 @@ import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.util.Base64Utils; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -55,8 +56,10 @@ class MockMvcSecurityIntegrationTests { @Test void okResponseWithBasicAuthCredentialsForKnownUser() throws Exception { - this.mockMvc.perform(get("/").header(HttpHeaders.AUTHORIZATION, - "Basic " + Base64Utils.encodeToString("user:secret".getBytes()))).andExpect(status().isOk()); + this.mockMvc + .perform(get("/").header(HttpHeaders.AUTHORIZATION, + "Basic " + Base64.getEncoder().encodeToString("user:secret".getBytes()))) + .andExpect(status().isOk()); } } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java index 6d68769c017..5085f9be0d2 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/TestRestTemplateTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.net.URI; +import java.util.Base64; import java.util.stream.Stream; import org.apache.hc.client5.http.config.RequestConfig; @@ -43,7 +44,6 @@ import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.http.client.MockClientHttpRequest; import org.springframework.mock.http.client.MockClientHttpResponse; import org.springframework.test.util.ReflectionTestUtils; -import org.springframework.util.Base64Utils; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.MethodCallback; import org.springframework.web.client.ResponseErrorHandler; @@ -372,8 +372,8 @@ class TestRestTemplateTests { } else { assertThat(request.getHeaders()).containsKeys(HttpHeaders.AUTHORIZATION); - assertThat(request.getHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly( - "Basic " + Base64Utils.encodeToString(String.format("%s:%s", username, password).getBytes())); + assertThat(request.getHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly("Basic " + + Base64.getEncoder().encodeToString(String.format("%s:%s", username, password).getBytes())); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/JsonEncodedDockerRegistryAuthentication.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/JsonEncodedDockerRegistryAuthentication.java index 9710ff90d92..2cc834bba31 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/JsonEncodedDockerRegistryAuthentication.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/configuration/JsonEncodedDockerRegistryAuthentication.java @@ -16,10 +16,11 @@ package org.springframework.boot.buildpack.platform.docker.configuration; +import java.util.Base64; + import com.fasterxml.jackson.core.JsonProcessingException; import org.springframework.boot.buildpack.platform.json.SharedObjectMapper; -import org.springframework.util.Base64Utils; /** * {@link DockerRegistryAuthentication} that uses a Base64 encoded auth header value based @@ -38,7 +39,7 @@ class JsonEncodedDockerRegistryAuthentication implements DockerRegistryAuthentic protected void createAuthHeader() { try { - this.authHeader = Base64Utils.encodeToUrlSafeString(SharedObjectMapper.get().writeValueAsBytes(this)); + this.authHeader = Base64.getUrlEncoder().encodeToString(SharedObjectMapper.get().writeValueAsBytes(this)); } catch (JsonProcessingException ex) { throw new IllegalStateException("Error creating Docker registry authentication header", ex); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java index 124b7eb5b8d..366c9bd4296 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/CertificateParser.java @@ -24,13 +24,12 @@ import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.ArrayList; +import java.util.Base64; import java.util.List; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.springframework.util.Base64Utils; - /** * Parser for X.509 certificates in PEM format. * @@ -93,7 +92,7 @@ final class CertificateParser { private static byte[] decodeBase64(String content) { byte[] bytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes(); - return Base64Utils.decode(bytes); + return Base64.getDecoder().decode(bytes); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java index 2982e6a0bed..7fe6cb360c0 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ssl/PrivateKeyParser.java @@ -25,14 +25,13 @@ import java.security.KeyFactory; import java.security.PrivateKey; import java.security.spec.PKCS8EncodedKeySpec; import java.util.ArrayList; +import java.util.Base64; import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.springframework.util.Base64Utils; - /** * Parser for PKCS private key files in PEM format. * @@ -153,7 +152,7 @@ final class PrivateKeyParser { private static byte[] decodeBase64(String content) { byte[] contentBytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes(); - return Base64Utils.decode(contentBytes); + return Base64.getDecoder().decode(contentBytes); } private PrivateKey parse(byte[] bytes) { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryTokenAuthenticationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryTokenAuthenticationTests.java index 272dbeb0b58..a120e3082d2 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryTokenAuthenticationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryTokenAuthenticationTests.java @@ -18,13 +18,13 @@ package org.springframework.boot.buildpack.platform.docker.configuration; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.Base64; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.boot.buildpack.platform.json.AbstractJsonTests; -import org.springframework.util.Base64Utils; import org.springframework.util.StreamUtils; /** @@ -39,7 +39,7 @@ class DockerRegistryTokenAuthenticationTests extends AbstractJsonTests { DockerRegistryTokenAuthentication auth = new DockerRegistryTokenAuthentication("tokenvalue"); String header = auth.getAuthHeader(); String expectedJson = StreamUtils.copyToString(getContent("auth-token.json"), StandardCharsets.UTF_8); - JSONAssert.assertEquals(expectedJson, new String(Base64Utils.decodeFromUrlSafeString(header)), false); + JSONAssert.assertEquals(expectedJson, new String(Base64.getUrlDecoder().decode(header)), false); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryUserAuthenticationTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryUserAuthenticationTests.java index d4720876877..72dc2dadc7f 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryUserAuthenticationTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/configuration/DockerRegistryUserAuthenticationTests.java @@ -18,13 +18,13 @@ package org.springframework.boot.buildpack.platform.docker.configuration; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.Base64; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; import org.springframework.boot.buildpack.platform.json.AbstractJsonTests; -import org.springframework.util.Base64Utils; import org.springframework.util.StreamUtils; /** @@ -52,7 +52,7 @@ class DockerRegistryUserAuthenticationTests extends AbstractJsonTests { } private String decoded(String header) { - return new String(Base64Utils.decodeFromUrlSafeString(header)); + return new String(Base64.getUrlDecoder().decode(header)); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/DockerSpecTests.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/DockerSpecTests.java index 601110815a0..eef57478945 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/DockerSpecTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/DockerSpecTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.gradle.tasks.bundling; import java.io.File; +import java.util.Base64; import org.gradle.api.GradleException; import org.junit.jupiter.api.BeforeEach; @@ -26,7 +27,6 @@ import org.junit.jupiter.api.io.TempDir; import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration; import org.springframework.boot.buildpack.platform.docker.configuration.DockerHost; import org.springframework.boot.gradle.junit.GradleProjectBuilder; -import org.springframework.util.Base64Utils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -176,7 +176,7 @@ class DockerSpecTests { } String decoded(String value) { - return new String(Base64Utils.decodeFromString(value)); + return new String(Base64.getDecoder().decode(value)); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DockerTests.java b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DockerTests.java index bc347932ded..5a83b17b247 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DockerTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DockerTests.java @@ -16,11 +16,12 @@ package org.springframework.boot.maven; +import java.util.Base64; + import org.junit.jupiter.api.Test; import org.springframework.boot.buildpack.platform.docker.configuration.DockerConfiguration; import org.springframework.boot.buildpack.platform.docker.configuration.DockerHost; -import org.springframework.util.Base64Utils; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; @@ -142,7 +143,7 @@ class DockerTests { } String decoded(String value) { - return new String(Base64Utils.decodeFromString(value)); + return new String(Base64.getDecoder().decode(value)); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/CertificateParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/CertificateParser.java index 11867b095fb..64926eb057d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/CertificateParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/CertificateParser.java @@ -25,12 +25,12 @@ import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.ArrayList; +import java.util.Base64; import java.util.List; import java.util.function.Consumer; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.springframework.util.Base64Utils; import org.springframework.util.FileCopyUtils; import org.springframework.util.ResourceUtils; @@ -103,7 +103,7 @@ final class CertificateParser { private static byte[] decodeBase64(String content) { byte[] bytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes(); - return Base64Utils.decode(bytes); + return Base64.getDecoder().decode(bytes); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PrivateKeyParser.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PrivateKeyParser.java index 93df27cb1c6..c8a458372be 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PrivateKeyParser.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/PrivateKeyParser.java @@ -26,13 +26,13 @@ import java.security.KeyFactory; import java.security.PrivateKey; import java.security.spec.PKCS8EncodedKeySpec; import java.util.ArrayList; +import java.util.Base64; import java.util.Collections; import java.util.List; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.springframework.util.Base64Utils; import org.springframework.util.FileCopyUtils; import org.springframework.util.ResourceUtils; @@ -163,7 +163,7 @@ final class PrivateKeyParser { private static byte[] decodeBase64(String content) { byte[] contentBytes = content.replaceAll("\r", "").replaceAll("\n", "").getBytes(); - return Base64Utils.decode(contentBytes); + return Base64.getDecoder().decode(contentBytes); } private PrivateKey parse(byte[] bytes) {