From 5051916f6f37355c4f129eb3795e82ccc82bab5a Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Tue, 9 Jul 2019 00:08:16 +0200 Subject: [PATCH] Polish Base64 usages See gh-17459 --- .../log4j2/SampleActuatorLog4J2ApplicationTests.java | 7 +++---- .../ManagementPortSampleSecureWebFluxTests.java | 7 +++---- .../webflux/SampleSecureWebFluxApplicationTests.java | 9 ++++----- .../SampleSecureWebFluxCustomSecurityTests.java | 12 ++++++------ .../session/SampleSessionApplicationTests.java | 6 +++++- 5 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator-log4j2/src/test/java/smoketest/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator-log4j2/src/test/java/smoketest/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java index 1ee0a2ffd2..f20c53c537 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator-log4j2/src/test/java/smoketest/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator-log4j2/src/test/java/smoketest/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java @@ -31,7 +31,6 @@ import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.test.web.servlet.MockMvc; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.equalTo; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -61,12 +60,12 @@ class SampleActuatorLog4J2ApplicationTests { @Test void validateLoggersEndpoint() throws Exception { this.mvc.perform(get("/actuator/loggers/org.apache.coyote.http11.Http11NioProtocol").header("Authorization", - "Basic " + getBasicAuth())).andExpect(status().isOk()).andExpect( - content().string(equalTo("{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}"))); + getBasicAuth())).andExpect(status().isOk()) + .andExpect(content().string("{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}")); } private String getBasicAuth() { - return new String(Base64.getEncoder().encode(("user:password").getBytes())); + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); } } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/ManagementPortSampleSecureWebFluxTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/ManagementPortSampleSecureWebFluxTests.java index 09040632bc..48885b8c5d 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/ManagementPortSampleSecureWebFluxTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/ManagementPortSampleSecureWebFluxTests.java @@ -56,9 +56,8 @@ class ManagementPortSampleSecureWebFluxTests { @Test void testHome() { - this.webClient.get().uri("http://localhost:" + this.port, String.class) - .header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isOk() - .expectBody(String.class).isEqualTo("Hello user"); + this.webClient.get().uri("http://localhost:" + this.port, String.class).header("Authorization", getBasicAuth()) + .exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello user"); } @Test @@ -84,7 +83,7 @@ class ManagementPortSampleSecureWebFluxTests { } private String getBasicAuth() { - return new String(Base64.getEncoder().encode(("user:password").getBytes())); + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxApplicationTests.java index b2c6817230..1b724b73f9 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxApplicationTests.java @@ -63,20 +63,19 @@ class SampleSecureWebFluxApplicationTests { @Test void userDefinedMappingsAccessibleOnLogin() { - this.webClient.get().uri("/").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuth()).exchange().expectBody(String.class) - .isEqualTo("Hello user"); + this.webClient.get().uri("/").accept(MediaType.APPLICATION_JSON).header("Authorization", getBasicAuth()) + .exchange().expectBody(String.class).isEqualTo("Hello user"); } @Test void actuatorsAccessibleOnLogin() { this.webClient.get().uri("/actuator/health").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuth()).exchange().expectBody(String.class) + .header("Authorization", getBasicAuth()).exchange().expectBody(String.class) .isEqualTo("{\"status\":\"UP\"}"); } private String getBasicAuth() { - return new String(Base64.getEncoder().encode(("user:password").getBytes())); + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); } } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxCustomSecurityTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxCustomSecurityTests.java index 4e2a096ad3..5152f79a48 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxCustomSecurityTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-secure-webflux/src/test/java/smoketest/secure/webflux/SampleSecureWebFluxCustomSecurityTests.java @@ -63,19 +63,19 @@ class SampleSecureWebFluxCustomSecurityTests { @Test void actuatorsSecuredByRole() { this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isForbidden(); + .header("Authorization", getBasicAuth()).exchange().expectStatus().isForbidden(); } @Test void actuatorsAccessibleOnCorrectLogin() { this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuthForAdmin()).exchange().expectStatus().isOk(); + .header("Authorization", getBasicAuthForAdmin()).exchange().expectStatus().isOk(); } @Test void actuatorExcludedFromEndpointRequestMatcher() { this.webClient.get().uri("/actuator/mappings").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isOk(); + .header("Authorization", getBasicAuth()).exchange().expectStatus().isOk(); } @Test @@ -89,15 +89,15 @@ class SampleSecureWebFluxCustomSecurityTests { this.webClient.get().uri("/actuator").accept(MediaType.APPLICATION_JSON).exchange().expectStatus() .isUnauthorized(); this.webClient.get().uri("/actuator").accept(MediaType.APPLICATION_JSON) - .header("Authorization", "basic " + getBasicAuthForAdmin()).exchange().expectStatus().isOk(); + .header("Authorization", getBasicAuthForAdmin()).exchange().expectStatus().isOk(); } private String getBasicAuth() { - return new String(Base64.getEncoder().encode(("user:password").getBytes())); + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); } private String getBasicAuthForAdmin() { - return new String(Base64.getEncoder().encode(("admin:admin").getBytes())); + return "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes()); } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/src/test/java/smoketest/session/SampleSessionApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/src/test/java/smoketest/session/SampleSessionApplicationTests.java index 9b04110d6b..78e518c2a3 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/src/test/java/smoketest/session/SampleSessionApplicationTests.java +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session/src/test/java/smoketest/session/SampleSessionApplicationTests.java @@ -65,7 +65,7 @@ class SampleSessionApplicationTests { private ResponseEntity firstRequest(RestTemplate restTemplate, URI uri) { HttpHeaders headers = new HttpHeaders(); - headers.set("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes())); + headers.set("Authorization", getBasicAuth()); RequestEntity request = new RequestEntity<>(headers, HttpMethod.GET, uri); return restTemplate.exchange(request, String.class); } @@ -77,4 +77,8 @@ class SampleSessionApplicationTests { return restTemplate.exchange(request, String.class); } + private String getBasicAuth() { + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); + } + }