oxsean commented on code in PR #1193: URL: https://github.com/apache/dubbo-samples/pull/1193#discussion_r1865903686
########## 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-spring-security/spring-security-resource-server/src/test/java/ResourceServerIT.java: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestClient; +import org.springframework.web.client.RestClientResponseException; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@EnableDubbo +@RunWith(SpringRunner.class) +public class ResourceServerIT { + + // @LocalServerPort + // private int port; + +// @DubboReference(url = "tri://localhost:50051") +// private HelloService helloService; + + private final String clientId = "49fd8518-12eb-422b-9264-2bae0ab89f66"; + private final String clientSecret = "H3DTtm2fR3GRAdr4ls1mcg"; + + private static final String OAUTH2HOST = System.getProperty("authorization.address", "localhost"); + private static final String HOST = System.getProperty("resource.address", "localhost"); + + @Test + public void testGetUserEndpoint() { + String credentials = clientId + ":" + clientSecret; + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); + + // build RestClient request + RestClient restClient = RestClient.builder().build(); + String url = "http://"+ OAUTH2HOST + ":9000/oauth2/token"; + + try { + // make a post request + String response = restClient.post() + .uri(url) + .header(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) + .body("grant_type=client_credentials&scope=read") + .retrieve() + .body(String.class); + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(response); + String accessToken = jsonNode.get("access_token").asText(); + + // Use the access token to authenticate the request to the /user endpoint + assert accessToken != null; + String userUrl = "http://" + HOST + ":50051/hello/sayHello/World"; + try { + String userResponse = restClient.get() + .uri(userUrl) + .header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) + .retrieve() + .body(String.class); + + assertEquals("\"Hello, World\"", userResponse, "error"); + } catch (RestClientResponseException e) { + System.err.println("Error Response: " + e.getResponseBodyAsString()); Review Comment: This catch will cause the result of the exception to succeed ########## 2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-spring-security/spring-security-resource-server/src/test/java/ResourceServerIT.java: ########## @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestClient; +import org.springframework.web.client.RestClientResponseException; + +import java.util.Base64; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@EnableDubbo +@RunWith(SpringRunner.class) +public class ResourceServerIT { + + // @LocalServerPort + // private int port; + +// @DubboReference(url = "tri://localhost:50051") +// private HelloService helloService; + + private final String clientId = "49fd8518-12eb-422b-9264-2bae0ab89f66"; + private final String clientSecret = "H3DTtm2fR3GRAdr4ls1mcg"; + + private static final String OAUTH2HOST = System.getProperty("authorization.address", "localhost"); + private static final String HOST = System.getProperty("resource.address", "localhost"); + + @Test + public void testGetUserEndpoint() { + String credentials = clientId + ":" + clientSecret; + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); + + // build RestClient request + RestClient restClient = RestClient.builder().build(); + String url = "http://"+ OAUTH2HOST + ":9000/oauth2/token"; + + try { + // make a post request + String response = restClient.post() + .uri(url) + .header(HttpHeaders.AUTHORIZATION, "Basic " + encodedCredentials) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) + .body("grant_type=client_credentials&scope=read") + .retrieve() + .body(String.class); + + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(response); + String accessToken = jsonNode.get("access_token").asText(); + + // Use the access token to authenticate the request to the /user endpoint + assert accessToken != null; Review Comment: Please also add a scenario when the accessToken is invalid -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
