Skip to content

Commit f0766d9

Browse files
authored
Update samples
patch diff from Azure/azure-sdk-for-java#22717
2 parents 8b1b1bf + 83cb016 commit f0766d9

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

aad/azure-spring-boot-sample-active-directory-resource-server/src/main/java/com/azure/spring/sample/aad/controller/HomeController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class HomeController {
1717
@ResponseBody
1818
@PreAuthorize("hasAuthority('SCOPE_WebApiB.ExampleScope')")
1919
public String file() {
20-
return "Response from WebApiB.";
20+
return "Response from webApiB.";
2121
}
2222

2323
@GetMapping("/user")
@@ -27,4 +27,10 @@ public String user() {
2727
return "User read success.";
2828
}
2929

30+
@GetMapping("/webapiB/clientCredential")
31+
@ResponseBody
32+
public String clientCredential() {
33+
return "Response from webApiB: clientCredential";
34+
}
35+
3036
}

aad/azure-spring-boot-sample-active-directory-webapp/src/main/java/com/azure/spring/sample/aad/controller/WebApiController.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,55 @@
2020
public class WebApiController {
2121

2222
private static final Logger LOGGER = LoggerFactory.getLogger(WebApiController.class);
23-
24-
private static final String CUSTOM_LOCAL_FILE_ENDPOINT = "http://localhost:8081/webapiA";
23+
private static final String WEB_API_A_URI = "http://localhost:8081/webapiA";
24+
private static final String WEB_API_B_URI = "http://localhost:8082/webapiB/clientCredential";
2525

2626
@Autowired
2727
private WebClient webClient;
2828

2929
/**
30-
* Call webapiA endpoint, combine all the response and return.
31-
* @param webapiAClient authorized client for Custom
30+
* Check whether webapiA is accessible.
31+
*
32+
* @param client authorized client for webapiA
3233
* @return Response webapiA data.
3334
*/
3435
@GetMapping("/webapp/webapiA/webapiB")
3536
@ResponseBody
36-
public String callWebApi(@RegisteredOAuth2AuthorizedClient("webapiA") OAuth2AuthorizedClient webapiAClient) {
37-
return callWebApiAEndpoint(webapiAClient);
37+
public String webapiA(@RegisteredOAuth2AuthorizedClient("webapiA") OAuth2AuthorizedClient client) {
38+
return canVisitUri(client, WEB_API_A_URI);
39+
}
40+
41+
/**
42+
* Check whether webapiB/clientCredential is accessible.
43+
*
44+
* @param client authorized client for webapiB
45+
* @return Response webapiA data.
46+
*/
47+
@GetMapping("/webapp/webapiB/clientCredential")
48+
@ResponseBody
49+
public String webapiB(@RegisteredOAuth2AuthorizedClient("webapiB") OAuth2AuthorizedClient client) {
50+
return canVisitUri(client, WEB_API_B_URI);
3851
}
3952

4053
/**
41-
* Call webapiA endpoint
42-
* @param webapiAClient Authorized Client
43-
* @return Response string data.
54+
* Check whether uri is accessible by provided client.
55+
*
56+
* @param client Authorized client.
57+
* @param uri The request uri.
58+
* @return "Get http response successfully." or "Get http response failed."
4459
*/
45-
private String callWebApiAEndpoint(OAuth2AuthorizedClient webapiAClient) {
46-
if (null != webapiAClient) {
47-
String body = webClient
48-
.get()
49-
.uri(CUSTOM_LOCAL_FILE_ENDPOINT)
50-
.attributes(oauth2AuthorizedClient(webapiAClient))
51-
.retrieve()
52-
.bodyToMono(String.class)
53-
.block();
54-
LOGGER.info("Response from webapiA : {}", body);
55-
return "webapiA response " + (null != body ? "success." : "failed.");
56-
} else {
57-
return "webapiA response failed.";
60+
private String canVisitUri(OAuth2AuthorizedClient client, String uri) {
61+
if (null == client) {
62+
return "Get response failed.";
5863
}
64+
String body = webClient
65+
.get()
66+
.uri(uri)
67+
.attributes(oauth2AuthorizedClient(client))
68+
.retrieve()
69+
.bodyToMono(String.class)
70+
.block();
71+
LOGGER.info("Response from {} : {}", uri, body);
72+
return "Get response " + (null != body ? "successfully" : "failed");
5973
}
6074
}

aad/azure-spring-boot-sample-active-directory-webapp/src/main/resources/application.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ azure:
1818
scopes:
1919
- https://graph.microsoft.com/User.Read
2020
- https://graph.microsoft.com/Directory.Read.All
21-
# webapiA:
21+
# webapiA: # This is used to demonstrate on-behalf-of function. Refs: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow
2222
# scopes:
2323
# - <Web-API-A-app-id-url>/Obo.WebApiA.ExampleScope
24+
# webapiB: # This is used to demonstrate client_credentials type. Refs: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow
25+
# scopes:
26+
# - api://<Web-API-B-app-id-url>/.default
27+
# authorization-grant-type: client_credentials

aad/azure-spring-boot-sample-active-directory-webapp/src/main/resources/templates/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h1>Azure Active Directory OAuth 2.0 Login with Spring Security</h1>
3737
<a href="/graph" >Graph Client</a> |
3838
<a href="/arm" >Arm Client</a> |
3939
<a href="/webapp/webapiA/webapiB" >Obo Client</a> |
40+
<a href="/webapp/webapiB/clientCredential" >Client Credential Client</a> |
4041
</div>
4142
</body>
4243
</html>

0 commit comments

Comments
 (0)