JAVA-20555 use Java Base64 instead of deprecated Spring Base64Utils (#13941)

This commit is contained in:
Kasra Madadipouya 2023-05-05 11:58:11 +02:00 committed by GitHub
parent 2e41874a5c
commit 7e404b3dfb
6 changed files with 24 additions and 19 deletions

View File

@ -5,11 +5,11 @@ import io.github.jhipster.config.JHipsterProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
/**
* Client talking to UAA's token endpoint to do different OAuth2 grants.
@ -34,7 +34,7 @@ public class UaaTokenEndpointClient extends OAuth2TokenEndpointClientAdapter imp
String clientId = getClientId();
String clientSecret = getClientSecret();
String authorization = clientId + ":" + clientSecret;
return "Basic " + Base64Utils.encodeToString(authorization.getBytes(StandardCharsets.UTF_8));
return "Basic " + Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8));
}
}

View File

@ -1,12 +1,13 @@
package com.baeldung.webclient.manualrequest.web;
import java.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.BodyInserters;
@ -41,7 +42,8 @@ public class ManualOauthRequestController {
logger.info("Creating web client...");
Mono<String> resource = client.post()
.uri(tokenUri)
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString((clientId + ":" + clientSecret).getBytes()))
.header(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder()
.encodeToString((clientId + ":" + clientSecret).getBytes()))
.body(BodyInserters.fromFormData(OAuth2ParameterNames.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS.getValue()))
.retrieve()
.bodyToMono(JsonNode.class)

View File

@ -1,5 +1,7 @@
package com.baeldung.reactive.authresolver;
import java.util.Base64;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -8,7 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.Base64Utils;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
@ -22,7 +23,7 @@ public class AuthResolverIntegrationTest {
testClient
.get()
.uri("/customer/welcome")
.header("Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
.exchange()
.expectStatus()
.isOk();
@ -33,7 +34,7 @@ public class AuthResolverIntegrationTest {
testClient
.get()
.uri("/customer/welcome")
.header("Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
.exchange()
.expectStatus()
.isUnauthorized();
@ -44,7 +45,7 @@ public class AuthResolverIntegrationTest {
testClient
.get()
.uri("/employee/welcome")
.header("Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
.exchange()
.expectStatus()
.isOk();
@ -55,7 +56,7 @@ public class AuthResolverIntegrationTest {
testClient
.get()
.uri("/employee/welcome")
.header("Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
.exchange()
.expectStatus()
.isUnauthorized();

View File

@ -11,13 +11,14 @@ import org.springframework.security.web.FilterChainProxy;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.Base64Utils;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.Base64;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -44,7 +45,7 @@ public class AuthResolverIntegrationTest {
this.mockMvc
.perform(get("/customer/welcome")
.header(
"Authorization", String.format("Basic %s", Base64Utils.encodeToString("customer1:pass1".getBytes()))
"Authorization", String.format("Basic %s", Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
)
)
.andExpect(status().is2xxSuccessful());
@ -55,7 +56,7 @@ public class AuthResolverIntegrationTest {
this.mockMvc
.perform(get("/customer/welcome")
.header(
"Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
"Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
)
.andExpect(status().isUnauthorized());
}
@ -65,7 +66,7 @@ public class AuthResolverIntegrationTest {
this.mockMvc
.perform(get("/employee/welcome")
.header(
"Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
"Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
)
.andExpect(status().is2xxSuccessful());
}
@ -75,7 +76,7 @@ public class AuthResolverIntegrationTest {
this.mockMvc
.perform(get("/employee/welcome")
.header(
"Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
"Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
)
.andExpect(status().isUnauthorized());
}

View File

@ -1,9 +1,9 @@
package com.baeldung.petstore.client.invoker.auth;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Base64Utils;
import org.springframework.util.MultiValueMap;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-03-15T06:14:01.568992-05:00[America/Chicago]")
@ -33,6 +33,7 @@ public class HttpBasicAuth implements Authentication {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
headerParams.add(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString(str.getBytes(StandardCharsets.UTF_8)));
headerParams.add(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder()
.encodeToString(str.getBytes(StandardCharsets.UTF_8)));
}
}

View File

@ -1,10 +1,9 @@
package com.baeldung.petstore.client.invoker.auth;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Base64Utils;
import org.springframework.util.MultiValueMap;
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2017-08-29T12:04:37.072+02:00")
@ -34,6 +33,7 @@ public class HttpBasicAuth implements Authentication {
return;
}
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
headerParams.add(HttpHeaders.AUTHORIZATION, "Basic " + Base64Utils.encodeToString(str.getBytes(StandardCharsets.UTF_8)));
headerParams.add(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder()
.encodeToString(str.getBytes(StandardCharsets.UTF_8)));
}
}