https://github.com/officiallysingh/spring-cloud-api-grateway-azuread-auth
Demo project for Spring Cloud API Gateway with AzureAD Auth
https://github.com/officiallysingh/spring-cloud-api-grateway-azuread-auth
authentication azure azure-active-directory azure-ad oauth2 oidc openid-connect spring-cloud-gateway spring-security
Last synced: 2 months ago
JSON representation
Demo project for Spring Cloud API Gateway with AzureAD Auth
- Host: GitHub
- URL: https://github.com/officiallysingh/spring-cloud-api-grateway-azuread-auth
- Owner: officiallysingh
- Created: 2024-05-04T08:45:58.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-06T13:36:32.000Z (about 2 years ago)
- Last Synced: 2025-02-21T12:43:18.944Z (over 1 year ago)
- Topics: authentication, azure, azure-active-directory, azure-ad, oauth2, oidc, openid-connect, spring-cloud-gateway, spring-security
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Read Me First
Demo Spring cloud Gateway and applying Authentication using Azure AD. Demo downstream service available at [`spring-boot-web-auth-demo`](https://github.com/officiallysingh/spring-boot-web-auth-demo)
## Guides
The following guides illustrate how to use Spring cloud Gateway and applying Authentication using Azure AD:
* [Gateway](https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/)
* [Securing a Java Web App with the Spring Boot Starter for Azure Active Directory](https://aka.ms/spring/msdocs/aad)
* [Azure Active Directory](https://microsoft.github.io/spring-cloud-azure/current/reference/html/index.html#spring-security-with-azure-active-directory)
## Gateway Routes configurations
* `uri` of downstream service.
* Path predicate
* `TokenRelay` to forward Access Token received from Azure AD as `Authorization` header to downstream services.
```yaml
spring:
cloud:
#------------------------- API Gateway configuration -------------------------
gateway:
routes:
- id: book-service
uri: http://localhost:8081
predicates:
- Path=/api/**
filters:
- TokenRelay=
```
## Spring boot Azure AD configurations
Update `tenant-id`, `client-id` and `client-secret` properties for your Azure AD settings, others remain the same.
```yaml
spring:
cloud:
#------------------------- Azure AD configuration -------------------------
azure:
active-directory:
enabled: true
profile:
tenant-id: ${AZURE_AD_TENANT_ID}
credential:
client-id: ${AZURE_AD_CLIENT_ID}
client-secret: ${AZURE_AD_CLIENT_SECRET}
security:
oauth2:
client:
registration:
azure:
client-id: ${spring.cloud.azure.active-directory.credential.client-id}
client-secret: ${spring.cloud.azure.active-directory.credential.client-secret}
authorization-grant-type: authorization_code
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
scope: openid, profile, email
provider:
azure:
authorization-uri: https://login.microsoftonline.com/${spring.cloud.azure.active-directory.profile.tenant-id}/oauth2/v2.0/authorize
token-uri: https://login.microsoftonline.com/${spring.cloud.azure.active-directory.profile.tenant-id}/oauth2/v2.0/token
user-info-uri: https://graph.microsoft.com/oidc/userinfo
jwk-set-uri: https://login.microsoftonline.com/${spring.cloud.azure.active-directory.profile.tenant-id}/discovery/v2.0/keys
issuer-uri: https://login.microsoftonline.com/${spring.cloud.azure.active-directory.profile.tenant-id}/v2.0
# userNameAttribute: unique_name
```
## Run
Run the main class [`SpringCloudApiGratewayAzureADAuthApplication`](src/main/java/com/ksoot/gateway/SpringCloudApiGratewayAzureADAuthApplication.java)
and access the URL http://localhost:8080/api/hello in browser.
It should redirect you to Azure AD login page, fill in Username and Password.
After successful authentication, you will get following response from downstream service.
`Hello from Downstream stream service`