Skip to content

Commit 57ebef2

Browse files
committed
Add managed identity
1 parent aed1b6d commit 57ebef2

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

java-17-sample/pom.xml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
<groupId>org.springframework.cloud</groupId>
3636
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
3737
</dependency>
38+
<dependency>
39+
<groupId>com.azure</groupId>
40+
<artifactId>azure-identity</artifactId>
41+
</dependency>
3842
<dependency>
3943
<groupId>org.apache.httpcomponents</groupId>
4044
<artifactId>httpclient</artifactId>
@@ -43,7 +47,7 @@
4347
<dependency>
4448
<groupId>com.fasterxml.woodstox</groupId>
4549
<artifactId>woodstox-core</artifactId>
46-
<version>6.4.0</version>
50+
<version>7.1.0</version>
4751
</dependency>
4852
</dependencies>
4953

@@ -52,14 +56,21 @@
5256
<dependency>
5357
<groupId>org.springframework.boot</groupId>
5458
<artifactId>spring-boot-dependencies</artifactId>
55-
<version>3.3.3</version>
59+
<version>3.4.1</version>
5660
<type>pom</type>
5761
<scope>import</scope>
5862
</dependency>
5963
<dependency>
6064
<groupId>org.springframework.cloud</groupId>
6165
<artifactId>spring-cloud-dependencies</artifactId>
62-
<version>2023.0.3</version>
66+
<version>2024.0.0</version>
67+
<type>pom</type>
68+
<scope>import</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.azure</groupId>
72+
<artifactId>azure-sdk-bom</artifactId>
73+
<version>1.2.30</version>
6374
<type>pom</type>
6475
<scope>import</scope>
6576
</dependency>

java-17-sample/src/main/java/com/azure/asa/sample/EurekaController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
@RestController
1414
public class EurekaController {
1515

16-
@Autowired
16+
@Autowired(required = false)
1717
private EurekaClient discoveryClient;
1818

1919
@GetMapping("/eureka")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.azure.asa.sample;
2+
3+
import com.azure.core.credential.AccessToken;
4+
import com.azure.core.credential.TokenRequestContext;
5+
import com.azure.identity.ManagedIdentityCredential;
6+
import com.azure.identity.ManagedIdentityCredentialBuilder;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
public class ManagedIdentityTestController {
13+
14+
@GetMapping("/mi/token")
15+
public String getAccessToken(@RequestParam(name = "client-id", required = false) String clientId) {
16+
ManagedIdentityCredentialBuilder managedIdentityCredentialBuilder = new ManagedIdentityCredentialBuilder();
17+
if(clientId != null) {
18+
managedIdentityCredentialBuilder.clientId(clientId);
19+
}
20+
ManagedIdentityCredential managedIdentityCredential = managedIdentityCredentialBuilder.build();
21+
AccessToken accessToken = managedIdentityCredential.getToken(new TokenRequestContext().addScopes("https://management.core.windows.net/")).block();
22+
23+
return accessToken.getToken();
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.azure.asa.sample;
2+
3+
import org.springframework.http.HttpHeaders;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.ControllerAdvice;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.context.request.WebRequest;
9+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
10+
11+
@ControllerAdvice
12+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
13+
14+
@ExceptionHandler(value = {RuntimeException.class})
15+
protected ResponseEntity<Object> handleConflict(
16+
RuntimeException ex, WebRequest request) {
17+
String bodyOfResponse = ex.getMessage();
18+
return handleExceptionInternal(ex, bodyOfResponse,
19+
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
20+
}
21+
}

0 commit comments

Comments
 (0)