Skip to content

Commit 1a9f1cd

Browse files
committed
Merge pull request #33967 from izeye
* pr/33967: Replace Base64Utils with JDK's Base64 Closes gh-33967
2 parents b62b883 + bc7fc90 commit 1a9f1cd

File tree

23 files changed

+73
-67
lines changed

23 files changed

+73
-67
lines changed

ci/images/releasescripts/src/test/java/io/spring/concourse/releasescripts/artifactory/ArtifactoryServiceTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.spring.concourse.releasescripts.artifactory;
1818

19+
import java.util.Base64;
20+
1921
import io.spring.concourse.releasescripts.ReleaseInfo;
2022
import org.junit.jupiter.api.AfterEach;
2123
import org.junit.jupiter.api.Test;
@@ -29,7 +31,6 @@
2931
import org.springframework.http.MediaType;
3032
import org.springframework.test.web.client.MockRestServiceServer;
3133
import org.springframework.test.web.client.response.DefaultResponseCreator;
32-
import org.springframework.util.Base64Utils;
3334
import org.springframework.web.client.HttpClientErrorException;
3435

3536
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -69,7 +70,7 @@ void promoteWhenSuccessful() {
6970
.andExpect(method(HttpMethod.POST))
7071
.andExpect(content().json(
7172
"{\"status\": \"staged\", \"sourceRepo\": \"libs-staging-local\", \"targetRepo\": \"libs-milestone-local\"}"))
72-
.andExpect(header("Authorization", "Basic " + Base64Utils.encodeToString(String
73+
.andExpect(header("Authorization", "Basic " + Base64.getEncoder().encodeToString(String
7374
.format("%s:%s", this.properties.getUsername(), this.properties.getPassword()).getBytes())))
7475
.andExpect(header("Content-Type", MediaType.APPLICATION_JSON.toString())).andRespond(withSuccess());
7576
this.service.promote("libs-milestone-local", getReleaseInfo());

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/Token.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry;
1818

1919
import java.nio.charset.StandardCharsets;
20+
import java.util.Base64;
2021
import java.util.List;
2122
import java.util.Map;
2223

2324
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
2425
import org.springframework.boot.json.JsonParserFactory;
25-
import org.springframework.util.Base64Utils;
2626
import org.springframework.util.StringUtils;
2727

2828
/**
@@ -60,7 +60,7 @@ public Token(String encoded) {
6060

6161
private Map<String, Object> parseJson(String base64) {
6262
try {
63-
byte[] bytes = Base64Utils.decodeFromUrlSafeString(base64);
63+
byte[] bytes = Base64.getUrlDecoder().decode(base64);
6464
return JsonParserFactory.getJsonParser().parseMap(new String(bytes, StandardCharsets.UTF_8));
6565
}
6666
catch (RuntimeException ex) {
@@ -73,7 +73,7 @@ public byte[] getContent() {
7373
}
7474

7575
public byte[] getSignature() {
76-
return Base64Utils.decodeFromUrlSafeString(this.signature);
76+
return Base64.getUrlDecoder().decode(this.signature);
7777
}
7878

7979
public String getSignatureAlgorithm() {

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.security.Signature;
2424
import java.security.spec.InvalidKeySpecException;
2525
import java.security.spec.X509EncodedKeySpec;
26+
import java.util.Base64;
2627
import java.util.Map;
2728
import java.util.concurrent.ConcurrentHashMap;
2829
import java.util.concurrent.ConcurrentMap;
@@ -33,7 +34,6 @@
3334
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException;
3435
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
3536
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token;
36-
import org.springframework.util.Base64Utils;
3737

3838
/**
3939
* Validator used to ensure that a signed {@link Token} has not been tampered with.
@@ -108,7 +108,7 @@ private PublicKey getPublicKey(String key) throws NoSuchAlgorithmException, Inva
108108
key = key.replace("-----BEGIN PUBLIC KEY-----\n", "");
109109
key = key.replace("-----END PUBLIC KEY-----", "");
110110
key = key.trim().replace("\n", "");
111-
byte[] bytes = Base64Utils.decodeFromString(key);
111+
byte[] bytes = Base64.getDecoder().decode(key);
112112
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
113113
return KeyFactory.getInstance("RSA").generatePublic(keySpec);
114114
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/TokenValidator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import java.security.Signature;
2424
import java.security.spec.InvalidKeySpecException;
2525
import java.security.spec.X509EncodedKeySpec;
26+
import java.util.Base64;
2627
import java.util.Map;
2728
import java.util.concurrent.TimeUnit;
2829

2930
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException;
3031
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
3132
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token;
32-
import org.springframework.util.Base64Utils;
3333

3434
/**
3535
* Validator used to ensure that a signed {@link Token} has not been tampered with.
@@ -102,7 +102,7 @@ private PublicKey getPublicKey(String key) throws NoSuchAlgorithmException, Inva
102102
key = key.replace("-----BEGIN PUBLIC KEY-----\n", "");
103103
key = key.replace("-----END PUBLIC KEY-----", "");
104104
key = key.trim().replace("\n", "");
105-
byte[] bytes = Base64Utils.decodeFromString(key);
105+
byte[] bytes = Base64.getDecoder().decode(key);
106106
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
107107
return KeyFactory.getInstance("RSA").generatePublic(keySpec);
108108
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/TokenTests.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry;
1818

19+
import java.util.Base64;
1920
import java.util.function.Consumer;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
24-
import org.springframework.util.Base64Utils;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
2727
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -44,8 +44,8 @@ void invalidJwtClaimsShouldThrowException() {
4444
String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}";
4545
String claims = "invalid-claims";
4646
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
47-
.isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes()) + "."
48-
+ Base64Utils.encodeToString(claims.getBytes())))
47+
.isThrownBy(() -> new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "."
48+
+ Base64.getEncoder().encodeToString(claims.getBytes())))
4949
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
5050
}
5151

@@ -54,8 +54,8 @@ void invalidJwtHeaderShouldThrowException() {
5454
String header = "invalid-header";
5555
String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}";
5656
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
57-
.isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes()) + "."
58-
+ Base64Utils.encodeToString(claims.getBytes())))
57+
.isThrownBy(() -> new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "."
58+
+ Base64.getEncoder().encodeToString(claims.getBytes())))
5959
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
6060
}
6161

@@ -71,16 +71,16 @@ void emptyJwtSignatureShouldThrowException() {
7171
void validJwt() {
7272
String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}";
7373
String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}";
74-
String content = Base64Utils.encodeToString(header.getBytes()) + "."
75-
+ Base64Utils.encodeToString(claims.getBytes());
76-
String signature = Base64Utils.encodeToString("signature".getBytes());
74+
String content = Base64.getEncoder().encodeToString(header.getBytes()) + "."
75+
+ Base64.getEncoder().encodeToString(claims.getBytes());
76+
String signature = Base64.getEncoder().encodeToString("signature".getBytes());
7777
Token token = new Token(content + "." + signature);
7878
assertThat(token.getExpiry()).isEqualTo(2147483647);
7979
assertThat(token.getIssuer()).isEqualTo("http://localhost:8080/uaa/oauth/token");
8080
assertThat(token.getSignatureAlgorithm()).isEqualTo("RS256");
8181
assertThat(token.getKeyId()).isEqualTo("key-id");
8282
assertThat(token.getContent()).isEqualTo(content.getBytes());
83-
assertThat(token.getSignature()).isEqualTo(Base64Utils.decodeFromString(signature));
83+
assertThat(token.getSignature()).isEqualTo(Base64.getDecoder().decode(signature));
8484
}
8585

8686
@Test
@@ -120,9 +120,9 @@ void getExpiryWhenExpIsNullShouldThrowException() {
120120
}
121121

122122
private Token createToken(String header, String claims) {
123-
Token token = new Token(
124-
Base64Utils.encodeToString(header.getBytes()) + "." + Base64Utils.encodeToString(claims.getBytes())
125-
+ "." + Base64Utils.encodeToString("signature".getBytes()));
123+
Token token = new Token(Base64.getEncoder().encodeToString(header.getBytes()) + "."
124+
+ Base64.getEncoder().encodeToString(claims.getBytes()) + "."
125+
+ Base64.getEncoder().encodeToString("signature".getBytes()));
126126
return token;
127127
}
128128

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/CloudFoundryWebFluxEndpointIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020
import java.util.Arrays;
21+
import java.util.Base64;
2122
import java.util.Collections;
2223
import java.util.Map;
2324
import java.util.function.Consumer;
@@ -54,7 +55,6 @@
5455
import org.springframework.http.HttpStatus;
5556
import org.springframework.http.MediaType;
5657
import org.springframework.test.web.reactive.server.WebTestClient;
57-
import org.springframework.util.Base64Utils;
5858
import org.springframework.web.cors.CorsConfiguration;
5959

6060
import static org.mockito.ArgumentMatchers.any;
@@ -160,7 +160,7 @@ private ContextConsumer<AssertableReactiveWebApplicationContext> withWebTestClie
160160
private String mockAccessToken() {
161161
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
162162
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
163-
+ Base64Utils.encodeToString("signature".getBytes());
163+
+ Base64.getEncoder().encodeToString("signature".getBytes());
164164
}
165165

166166
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundrySecurityInterceptorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry.reactive;
1818

19+
import java.util.Base64;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123
import org.junit.jupiter.api.extension.ExtendWith;
@@ -31,7 +33,6 @@
3133
import org.springframework.http.HttpStatus;
3234
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
3335
import org.springframework.mock.web.server.MockServerWebExchange;
34-
import org.springframework.util.Base64Utils;
3536

3637
import static org.assertj.core.api.Assertions.assertThat;
3738
import static org.mockito.ArgumentMatchers.any;
@@ -151,7 +152,7 @@ void preHandleSuccessfulWithRestrictedAccess() {
151152
private String mockAccessToken() {
152153
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
153154
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
154-
+ Base64Utils.encodeToString("signature".getBytes());
155+
+ Base64.getEncoder().encodeToString("signature".getBytes());
155156
}
156157

157158
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveTokenValidatorTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.security.Signature;
2626
import java.security.spec.InvalidKeySpecException;
2727
import java.security.spec.PKCS8EncodedKeySpec;
28+
import java.util.Base64;
2829
import java.util.Collections;
2930
import java.util.Map;
3031
import java.util.concurrent.ConcurrentHashMap;
@@ -42,7 +43,6 @@
4243
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
4344
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.Token;
4445
import org.springframework.test.util.ReflectionTestUtils;
45-
import org.springframework.util.Base64Utils;
4646
import org.springframework.util.StreamUtils;
4747

4848
import static org.assertj.core.api.Assertions.assertThat;
@@ -251,11 +251,11 @@ private String getSignedToken(byte[] header, byte[] claims) throws Exception {
251251
PrivateKey privateKey = getPrivateKey();
252252
Signature signature = Signature.getInstance("SHA256WithRSA");
253253
signature.initSign(privateKey);
254-
byte[] content = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encode(claims));
254+
byte[] content = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getEncoder().encode(claims));
255255
signature.update(content);
256256
byte[] crypto = signature.sign();
257-
byte[] token = dotConcat(Base64Utils.encodeUrlSafe(header), Base64Utils.encodeUrlSafe(claims),
258-
Base64Utils.encodeUrlSafe(crypto));
257+
byte[] token = dotConcat(Base64.getUrlEncoder().encode(header), Base64.getUrlEncoder().encode(claims),
258+
Base64.getUrlEncoder().encode(crypto));
259259
return new String(token, StandardCharsets.UTF_8);
260260
}
261261

@@ -292,7 +292,7 @@ private PrivateKey getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorit
292292
String privateKey = signingKey.replace("-----BEGIN PRIVATE KEY-----\n", "");
293293
privateKey = privateKey.replace("-----END PRIVATE KEY-----", "");
294294
privateKey = privateKey.replace("\n", "");
295-
byte[] pkcs8EncodedBytes = Base64Utils.decodeFromString(privateKey);
295+
byte[] pkcs8EncodedBytes = Base64.getDecoder().decode(privateKey);
296296
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8EncodedBytes);
297297
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
298298
return keyFactory.generatePrivate(keySpec);

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundryMvcWebEndpointIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Duration;
2020
import java.util.Arrays;
21+
import java.util.Base64;
2122
import java.util.Collections;
2223
import java.util.Map;
2324
import java.util.function.BiConsumer;
@@ -48,7 +49,6 @@
4849
import org.springframework.http.HttpStatus;
4950
import org.springframework.http.MediaType;
5051
import org.springframework.test.web.reactive.server.WebTestClient;
51-
import org.springframework.util.Base64Utils;
5252
import org.springframework.web.cors.CorsConfiguration;
5353
import org.springframework.web.servlet.DispatcherServlet;
5454
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@@ -155,7 +155,7 @@ private void load(Class<?> configuration, Consumer<WebTestClient> clientConsumer
155155
private String mockAccessToken() {
156156
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
157157
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
158-
+ Base64Utils.encodeToString("signature".getBytes());
158+
+ Base64.getEncoder().encodeToString("signature".getBytes());
159159
}
160160

161161
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityInterceptorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet;
1818

19+
import java.util.Base64;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123
import org.junit.jupiter.api.extension.ExtendWith;
@@ -31,7 +33,6 @@
3133
import org.springframework.http.HttpHeaders;
3234
import org.springframework.http.HttpStatus;
3335
import org.springframework.mock.web.MockHttpServletRequest;
34-
import org.springframework.util.Base64Utils;
3536

3637
import static org.assertj.core.api.Assertions.assertThat;
3738
import static org.mockito.BDDMockito.given;
@@ -139,7 +140,7 @@ void preHandleSuccessfulWithRestrictedAccess() {
139140
private String mockAccessToken() {
140141
return "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
141142
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ."
142-
+ Base64Utils.encodeToString("signature".getBytes());
143+
+ Base64.getEncoder().encodeToString("signature".getBytes());
143144
}
144145

145146
}

0 commit comments

Comments
 (0)