From dd8c9a43cb4f648b43768c5ef2502a4f32738a99 Mon Sep 17 00:00:00 2001 From: DevDengChao <2325690622@qq.com> Date: Tue, 11 Apr 2023 13:55:40 +0800 Subject: [PATCH] Replace basicAuth() with SecurityMockMvcRequestPostProcessors#httpBasic() --- ...2AuthorizationServerApplicationITests.java | 48 +++++-------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/servlet/spring-boot/java/oauth2/authorization-server/src/integTest/java/example/OAuth2AuthorizationServerApplicationITests.java b/servlet/spring-boot/java/oauth2/authorization-server/src/integTest/java/example/OAuth2AuthorizationServerApplicationITests.java index c0d71cb1a..60e120d95 100644 --- a/servlet/spring-boot/java/oauth2/authorization-server/src/integTest/java/example/OAuth2AuthorizationServerApplicationITests.java +++ b/servlet/spring-boot/java/oauth2/authorization-server/src/integTest/java/example/OAuth2AuthorizationServerApplicationITests.java @@ -16,22 +16,19 @@ package example; -import java.util.Map; - import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpHeaders; -import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.RequestPostProcessor; +import java.util.Map; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -61,7 +58,7 @@ void performTokenRequestWhenValidClientCredentialsThenOk() throws Exception { this.mockMvc.perform(post("/oauth2/token") .param("grant_type", "client_credentials") .param("scope", "message:read") - .with(basicAuth(CLIENT_ID, CLIENT_SECRET))) + .with(httpBasic(CLIENT_ID, CLIENT_SECRET))) .andExpect(status().isOk()) .andExpect(jsonPath("$.access_token").isString()) .andExpect(jsonPath("$.expires_in").isNumber()) @@ -76,7 +73,7 @@ void performTokenRequestWhenMissingScopeThenOk() throws Exception { this.mockMvc.perform(post("/oauth2/token") .param("grant_type", "client_credentials") .param("scope", "message:read message:write") - .with(basicAuth(CLIENT_ID, CLIENT_SECRET))) + .with(httpBasic(CLIENT_ID, CLIENT_SECRET))) .andExpect(status().isOk()) .andExpect(jsonPath("$.access_token").isString()) .andExpect(jsonPath("$.expires_in").isNumber()) @@ -91,7 +88,7 @@ void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Ex this.mockMvc.perform(post("/oauth2/token") .param("grant_type", "client_credentials") .param("scope", "message:read") - .with(basicAuth("bad", "password"))) + .with(httpBasic("bad", "password"))) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.error").value("invalid_client")); // @formatter:on @@ -101,7 +98,7 @@ void performTokenRequestWhenInvalidClientCredentialsThenUnauthorized() throws Ex void performTokenRequestWhenMissingGrantTypeThenUnauthorized() throws Exception { // @formatter:off this.mockMvc.perform(post("/oauth2/token") - .with(basicAuth("bad", "password"))) + .with(httpBasic("bad", "password"))) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.error").value("invalid_client")); // @formatter:on @@ -112,7 +109,7 @@ void performTokenRequestWhenGrantTypeNotRegisteredThenBadRequest() throws Except // @formatter:off this.mockMvc.perform(post("/oauth2/token") .param("grant_type", "client_credentials") - .with(basicAuth("login-client", "openid-connect"))) + .with(httpBasic("login-client", "openid-connect"))) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.error").value("unauthorized_client")); // @formatter:on @@ -123,7 +120,7 @@ void performIntrospectionRequestWhenValidTokenThenOk() throws Exception { // @formatter:off this.mockMvc.perform(post("/oauth2/introspect") .param("token", getAccessToken()) - .with(basicAuth(CLIENT_ID, CLIENT_SECRET))) + .with(httpBasic(CLIENT_ID, CLIENT_SECRET))) .andExpect(status().isOk()) .andExpect(jsonPath("$.active").value("true")) .andExpect(jsonPath("$.aud[0]").value(CLIENT_ID)) @@ -143,7 +140,7 @@ void performIntrospectionRequestWhenInvalidCredentialsThenUnauthorized() throws // @formatter:off this.mockMvc.perform(post("/oauth2/introspect") .param("token", getAccessToken()) - .with(basicAuth("bad", "password"))) + .with(httpBasic("bad", "password"))) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.error").value("invalid_client")); // @formatter:on @@ -154,7 +151,7 @@ private String getAccessToken() throws Exception { MvcResult mvcResult = this.mockMvc.perform(post("/oauth2/token") .param("grant_type", "client_credentials") .param("scope", "message:read") - .with(basicAuth(CLIENT_ID, CLIENT_SECRET))) + .with(httpBasic(CLIENT_ID, CLIENT_SECRET))) .andExpect(status().isOk()) .andExpect(jsonPath("$.access_token").exists()) .andReturn(); @@ -167,29 +164,6 @@ private String getAccessToken() throws Exception { return tokenResponse.get("access_token").toString(); } - private static BasicAuthenticationRequestPostProcessor basicAuth(String username, String password) { - return new BasicAuthenticationRequestPostProcessor(username, password); - } - - private static final class BasicAuthenticationRequestPostProcessor implements RequestPostProcessor { - - private final String username; - - private final String password; - private BasicAuthenticationRequestPostProcessor(String username, String password) { - this.username = username; - this.password = password; - } - - @Override - public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth(this.username, this.password); - request.addHeader("Authorization", headers.getFirst("Authorization")); - return request; - } - - } }