Polish Base64 usages

See gh-17459
pull/17471/head
dreis2211 5 years ago committed by Andy Wilkinson
parent a40bc64f6e
commit 5051916f6f

@ -31,7 +31,6 @@ import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.assertj.core.api.Assertions.assertThat; 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.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -61,12 +60,12 @@ class SampleActuatorLog4J2ApplicationTests {
@Test @Test
void validateLoggersEndpoint() throws Exception { void validateLoggersEndpoint() throws Exception {
this.mvc.perform(get("/actuator/loggers/org.apache.coyote.http11.Http11NioProtocol").header("Authorization", this.mvc.perform(get("/actuator/loggers/org.apache.coyote.http11.Http11NioProtocol").header("Authorization",
"Basic " + getBasicAuth())).andExpect(status().isOk()).andExpect( getBasicAuth())).andExpect(status().isOk())
content().string(equalTo("{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}"))); .andExpect(content().string("{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}"));
} }
private String getBasicAuth() { private String getBasicAuth() {
return new String(Base64.getEncoder().encode(("user:password").getBytes())); return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
} }
} }

@ -56,9 +56,8 @@ class ManagementPortSampleSecureWebFluxTests {
@Test @Test
void testHome() { void testHome() {
this.webClient.get().uri("http://localhost:" + this.port, String.class) this.webClient.get().uri("http://localhost:" + this.port, String.class).header("Authorization", getBasicAuth())
.header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isOk() .exchange().expectStatus().isOk().expectBody(String.class).isEqualTo("Hello user");
.expectBody(String.class).isEqualTo("Hello user");
} }
@Test @Test
@ -84,7 +83,7 @@ class ManagementPortSampleSecureWebFluxTests {
} }
private String getBasicAuth() { private String getBasicAuth() {
return new String(Base64.getEncoder().encode(("user:password").getBytes())); return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -63,20 +63,19 @@ class SampleSecureWebFluxApplicationTests {
@Test @Test
void userDefinedMappingsAccessibleOnLogin() { void userDefinedMappingsAccessibleOnLogin() {
this.webClient.get().uri("/").accept(MediaType.APPLICATION_JSON) this.webClient.get().uri("/").accept(MediaType.APPLICATION_JSON).header("Authorization", getBasicAuth())
.header("Authorization", "basic " + getBasicAuth()).exchange().expectBody(String.class) .exchange().expectBody(String.class).isEqualTo("Hello user");
.isEqualTo("Hello user");
} }
@Test @Test
void actuatorsAccessibleOnLogin() { void actuatorsAccessibleOnLogin() {
this.webClient.get().uri("/actuator/health").accept(MediaType.APPLICATION_JSON) 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\"}"); .isEqualTo("{\"status\":\"UP\"}");
} }
private String getBasicAuth() { private String getBasicAuth() {
return new String(Base64.getEncoder().encode(("user:password").getBytes())); return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
} }
} }

@ -63,19 +63,19 @@ class SampleSecureWebFluxCustomSecurityTests {
@Test @Test
void actuatorsSecuredByRole() { void actuatorsSecuredByRole() {
this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON) this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON)
.header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isForbidden(); .header("Authorization", getBasicAuth()).exchange().expectStatus().isForbidden();
} }
@Test @Test
void actuatorsAccessibleOnCorrectLogin() { void actuatorsAccessibleOnCorrectLogin() {
this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON) this.webClient.get().uri("/actuator/env").accept(MediaType.APPLICATION_JSON)
.header("Authorization", "basic " + getBasicAuthForAdmin()).exchange().expectStatus().isOk(); .header("Authorization", getBasicAuthForAdmin()).exchange().expectStatus().isOk();
} }
@Test @Test
void actuatorExcludedFromEndpointRequestMatcher() { void actuatorExcludedFromEndpointRequestMatcher() {
this.webClient.get().uri("/actuator/mappings").accept(MediaType.APPLICATION_JSON) this.webClient.get().uri("/actuator/mappings").accept(MediaType.APPLICATION_JSON)
.header("Authorization", "basic " + getBasicAuth()).exchange().expectStatus().isOk(); .header("Authorization", getBasicAuth()).exchange().expectStatus().isOk();
} }
@Test @Test
@ -89,15 +89,15 @@ class SampleSecureWebFluxCustomSecurityTests {
this.webClient.get().uri("/actuator").accept(MediaType.APPLICATION_JSON).exchange().expectStatus() this.webClient.get().uri("/actuator").accept(MediaType.APPLICATION_JSON).exchange().expectStatus()
.isUnauthorized(); .isUnauthorized();
this.webClient.get().uri("/actuator").accept(MediaType.APPLICATION_JSON) 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() { private String getBasicAuth() {
return new String(Base64.getEncoder().encode(("user:password").getBytes())); return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
} }
private String getBasicAuthForAdmin() { private String getBasicAuthForAdmin() {
return new String(Base64.getEncoder().encode(("admin:admin").getBytes())); return "Basic " + Base64.getEncoder().encodeToString("admin:admin".getBytes());
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -65,7 +65,7 @@ class SampleSessionApplicationTests {
private ResponseEntity<String> firstRequest(RestTemplate restTemplate, URI uri) { private ResponseEntity<String> firstRequest(RestTemplate restTemplate, URI uri) {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes())); headers.set("Authorization", getBasicAuth());
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri); RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
return restTemplate.exchange(request, String.class); return restTemplate.exchange(request, String.class);
} }
@ -77,4 +77,8 @@ class SampleSessionApplicationTests {
return restTemplate.exchange(request, String.class); return restTemplate.exchange(request, String.class);
} }
private String getBasicAuth() {
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
}
} }

Loading…
Cancel
Save