JAVA-20555 use Java Base64 instead of deprecated Spring Base64Utils (#13941)
This commit is contained in:
parent
2e41874a5c
commit
7e404b3dfb
|
@ -5,11 +5,11 @@ import io.github.jhipster.config.JHipsterProperties;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Client talking to UAA's token endpoint to do different OAuth2 grants.
|
* 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 clientId = getClientId();
|
||||||
String clientSecret = getClientSecret();
|
String clientSecret = getClientSecret();
|
||||||
String authorization = clientId + ":" + clientSecret;
|
String authorization = clientId + ":" + clientSecret;
|
||||||
return "Basic " + Base64Utils.encodeToString(authorization.getBytes(StandardCharsets.UTF_8));
|
return "Basic " + Base64.getEncoder().encodeToString(authorization.getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
package com.baeldung.webclient.manualrequest.web;
|
package com.baeldung.webclient.manualrequest.web;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
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.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
@ -41,7 +42,8 @@ public class ManualOauthRequestController {
|
||||||
logger.info("Creating web client...");
|
logger.info("Creating web client...");
|
||||||
Mono<String> resource = client.post()
|
Mono<String> resource = client.post()
|
||||||
.uri(tokenUri)
|
.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()))
|
.body(BodyInserters.fromFormData(OAuth2ParameterNames.GRANT_TYPE, GrantType.CLIENT_CREDENTIALS.getValue()))
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(JsonNode.class)
|
.bodyToMono(JsonNode.class)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.baeldung.reactive.authresolver;
|
package com.baeldung.reactive.authresolver;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
import org.junit.FixMethodOrder;
|
import org.junit.FixMethodOrder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
|
||||||
|
@ -22,7 +23,7 @@ public class AuthResolverIntegrationTest {
|
||||||
testClient
|
testClient
|
||||||
.get()
|
.get()
|
||||||
.uri("/customer/welcome")
|
.uri("/customer/welcome")
|
||||||
.header("Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
|
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk();
|
.isOk();
|
||||||
|
@ -33,7 +34,7 @@ public class AuthResolverIntegrationTest {
|
||||||
testClient
|
testClient
|
||||||
.get()
|
.get()
|
||||||
.uri("/customer/welcome")
|
.uri("/customer/welcome")
|
||||||
.header("Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
|
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isUnauthorized();
|
.isUnauthorized();
|
||||||
|
@ -44,7 +45,7 @@ public class AuthResolverIntegrationTest {
|
||||||
testClient
|
testClient
|
||||||
.get()
|
.get()
|
||||||
.uri("/employee/welcome")
|
.uri("/employee/welcome")
|
||||||
.header("Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
|
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isOk();
|
.isOk();
|
||||||
|
@ -55,7 +56,7 @@ public class AuthResolverIntegrationTest {
|
||||||
testClient
|
testClient
|
||||||
.get()
|
.get()
|
||||||
.uri("/employee/welcome")
|
.uri("/employee/welcome")
|
||||||
.header("Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
|
.header("Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
|
||||||
.exchange()
|
.exchange()
|
||||||
.expectStatus()
|
.expectStatus()
|
||||||
.isUnauthorized();
|
.isUnauthorized();
|
||||||
|
|
|
@ -11,13 +11,14 @@ import org.springframework.security.web.FilterChainProxy;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
|
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.request.MockMvcRequestBuilders.get;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
|
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AuthResolverApplication.class)
|
||||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||||
|
@ -44,7 +45,7 @@ public class AuthResolverIntegrationTest {
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(get("/customer/welcome")
|
.perform(get("/customer/welcome")
|
||||||
.header(
|
.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());
|
.andExpect(status().is2xxSuccessful());
|
||||||
|
@ -55,7 +56,7 @@ public class AuthResolverIntegrationTest {
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(get("/customer/welcome")
|
.perform(get("/customer/welcome")
|
||||||
.header(
|
.header(
|
||||||
"Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
|
"Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
|
||||||
)
|
)
|
||||||
.andExpect(status().isUnauthorized());
|
.andExpect(status().isUnauthorized());
|
||||||
}
|
}
|
||||||
|
@ -65,7 +66,7 @@ public class AuthResolverIntegrationTest {
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(get("/employee/welcome")
|
.perform(get("/employee/welcome")
|
||||||
.header(
|
.header(
|
||||||
"Authorization", "Basic " + Base64Utils.encodeToString("employee1:pass1".getBytes()))
|
"Authorization", "Basic " + Base64.getEncoder().encodeToString("employee1:pass1".getBytes()))
|
||||||
)
|
)
|
||||||
.andExpect(status().is2xxSuccessful());
|
.andExpect(status().is2xxSuccessful());
|
||||||
}
|
}
|
||||||
|
@ -75,7 +76,7 @@ public class AuthResolverIntegrationTest {
|
||||||
this.mockMvc
|
this.mockMvc
|
||||||
.perform(get("/employee/welcome")
|
.perform(get("/employee/welcome")
|
||||||
.header(
|
.header(
|
||||||
"Authorization", "Basic " + Base64Utils.encodeToString("customer1:pass1".getBytes()))
|
"Authorization", "Basic " + Base64.getEncoder().encodeToString("customer1:pass1".getBytes()))
|
||||||
)
|
)
|
||||||
.andExpect(status().isUnauthorized());
|
.andExpect(status().isUnauthorized());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package com.baeldung.petstore.client.invoker.auth;
|
package com.baeldung.petstore.client.invoker.auth;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
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]")
|
@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;
|
return;
|
||||||
}
|
}
|
||||||
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
|
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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
package com.baeldung.petstore.client.invoker.auth;
|
package com.baeldung.petstore.client.invoker.auth;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.util.Base64Utils;
|
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
@javax.annotation.Generated(value = "io.swagger.codegen.languages.JavaClientCodegen", date = "2017-08-29T12:04:37.072+02:00")
|
@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;
|
return;
|
||||||
}
|
}
|
||||||
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
|
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)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue