Read-only mirror of https://github.com/swisscom/sample-uaa-spring-boot-service-provider — 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:16 +02:00
src remove configurable AFTER_LOGOUT_REDIRECT_URL (now guessed from request url) 2016-09-07 13:32:36 +02:00
.gitignore initial version for github 2016-07-19 12:45:18 +02:00
CODE_OF_CONDUCT.md Compliance bulk update. Add CODE_OF_CONDUCT.md 2024-10-16 17:19:16 +02:00
CONTRIBUTING.md Compliance bulk update. Add CONTRIBUTING.md 2024-10-16 17:19:14 +02:00
LICENSE Update LICENSE 2023-04-24 18:01:39 +02:00
pom.xml initial version for github 2016-07-19 12:45:18 +02:00
README.md Update README.md 2023-08-02 11:29:29 +02:00

Sample Spring boot OAuth2 service provider for UAA

Demo app that authenticates its users against an OAuth2 provider (i.e. UAA) using the authorization code grant type, meaning this app will store the session (and JWT) in its backend.

It uses Spring boot and Spring security (based on https://github.com/spring-guides/tut-spring-boot-oauth2).

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

How it works

When the app is accessed by an unauthenticated user, it redirects the user to the OAuth2 provider (standard OAuth2 Authorization code flow). When the user logs in on the OAuth2 provider, he is redirected back to the app. The app completes the OAuth2 flow and receives the user's info (using the Userinfo endpoint), i.e. its attributes like firstname, lastname.

It was tested with UAA acting as OAuth2 provider, but should work fine with other providers (i.e. Facebook, Github etc.).

Configure, deploy and test

The app expects a VCAP_SERVICES env variable containing a service with a tag oauth2 or a credentials with a key authorizationEndpointUrl, 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 & push the app
mvn clean package
cf push oauth2-simple --random-route --no-start -p target/oauth2-simple-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='{"authorizationEndpoint": "<uaa-url>/oauth/authorize", "tokenEndpoint": "<uaa-url>/oauth/token", "userInfoEndpoint": "<uaa-url>/userinfo", "logoutEndpoint": "<uaa-url>/logout.do", "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 oauth2-simple OAUTH2-CLIENT
cf start oauth2-simple

Now access the app in your browser, which redirects you to the UAA/OAuth2 provider you specified in the configuration. After login, the user attributes passed to the app will be shown:

{
  "details":
  {
    "user_id":"e814ddc8-1b28-422f-b34a-1aa0c97beb31",
    "user_name":"jdoe",
    "given_name":"John",
    "family_name":"Doe",
    "email":"john.doe@example.com",
    "name":"John Doe"
  },
  "authorities":[{"authority":"ROLE_USER"}],
  "authenticated":true,
  "principal":"e814ddc8-1b28-422f-b34a-1aa0c97beb31",
  "credentials":"N/A",
  "name":"e814ddc8-1b28-422f-b34a-1aa0c97beb31"
}

Sample overview

Authorization code

Implicit flow & Client Credentials

WARNING: PKCE's secure implementation renders the implicit flow obsolete, as it is vulnerable and must not be used anymore.