Merge pull request #17459 from dreis2211

* gh-17459:
  Polish Base64 usages

Closes gh-17459
pull/17471/head
Andy Wilkinson 5 years ago
commit e5964d5f86

@ -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());
}
}

@ -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)

@ -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());
}
}

@ -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)

@ -65,7 +65,7 @@ class SampleSessionApplicationTests {
private ResponseEntity<String> firstRequest(RestTemplate restTemplate, URI uri) {
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);
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());
}
}

Loading…
Cancel
Save