Read-only mirror of https://github.com/swisscom/sample-uaa-spring-boot-resource-server — Swisscom. Issues & pull requests at the source.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2024-10-16 17:19:12 +02:00
gradle/wrapper commit first working version 2019-02-20 19:48:26 +01:00
src commit first working version 2019-02-20 19:48:26 +01:00
.gitignore commit first working version 2019-02-20 19:48:26 +01:00
build.gradle commit first working version 2019-02-20 19:48:26 +01:00
CODE_OF_CONDUCT.md Compliance bulk update. Add CODE_OF_CONDUCT.md 2024-10-16 17:19:12 +02:00
CONTRIBUTING.md Compliance bulk update. Add CONTRIBUTING.md 2024-10-16 17:19:10 +02:00
gradlew initalized from spring initializr 2019-02-18 17:00:10 +01:00
gradlew.bat initalized from spring initializr 2019-02-18 17:00:10 +01:00
LICENSE initalized from spring initializr 2019-02-18 17:00:10 +01:00
manifest.yml commit first working version 2019-02-20 19:48:26 +01:00
README.md removed trailing " 2019-02-20 20:27:14 +01:00
settings.gradle initalized from spring initializr 2019-02-18 17:00:10 +01:00

Sample Spring boot OAuth2/OIDC resource server for UAA

Demo app that acts as OAuth2 resource server, meaning it offers a stateless API and is protected by an OAuth2 JWT access token verification. It's mostly used in conjunction with a javascript frontend that obtains the OAuth2 JWT access token using the implicit flow and then uses this API as its backend.

It is primarily designed to run on CloudFoundry, since it expects the OAuth2 configuration to be passed in VCAP_SERVICES.

How it works

The client invokes the backend API with the HTTP header Authorization: Bearer <JWT access token>. The backend extracts this JWT token and validates it on the OAuth2 IDP. See the comments in the code for details.

It was tested with UAA acting as OAuth2/OIDC provider, but it should work with every OIDC provider.

Configure, deploy and test

The app expects a VCAP_SERVICES env variable containing a service with a tag oauth2 so make sure it is set when running the app.

When deploying to CloudFoundry, one can use a User provided service instance to achieve this.

Full example:

# compile the app
mvn clean package

# adjust ALLOWED_CORS_ORIGIN in manifest.yml if you want to use a browser client, then push
cf push sample --random-route --no-start -p target/sample-uaa-spring-boot-resource-server-0.0.1-SNAPSHOT.jar

# Now create the user provided service which will be provided to the app in VCAP_SERVICES.
# The client specified here must be created manually beforehand on the OAuth2 provider.
CREDENTIALS='{"userInfoEndpoint": "<uaa-url>/userinfo", "introspectEndpoint": "<uaa-url>/introspect", "clientId": "<client-id>", "clientSecret": "<client-secret>"}'
cf create-user-provided-service OAUTH2-CLIENT -p $CREDENTIALS -t oauth2

# Bind & start the app to make the service instance available
cf bind-service sample OAUTH2-CLIENT
cf start sample

You can now speak to the API, providing a valid token:

TOKEN=obtain token from IDP
curl -v https://<path-to-your-app>/env -H "Authorization: Bearer $TOKEN"

And the app will return the user attributes it received from the IDP.

For a full demo, you can deploy a client sample app (see below) that will act as the client for you.

Sample overview

Authorization code

Implicit flow & Client Credentials