This sample demonstrates:
- Configuring a Spring Boot WebFlux application as a Resource Server
- Using and extending Spring Security to validate JWTs
- Protecting APIs to only allow authorized access
- Parse Auth0 Id_token with user metadata
- Java 8 or greater
- An Auth0 account
For complete instructions and additional information, please refer to the Spring 5 API Security Quickstart that this sample accompanies.
In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API, for example https://quickstarts/api. Leave the Signing Algorithm as RS256.
The project needs to be configured with your Auth0 domain and API Identifier.
To do this, first copy src/main/resources/application.yml.example into a new file in the same folder called src/main/resources/application.yml, and replace the values with your own Auth0 domain and API Identifier:
auth0:
audience: {API_IDENTIFIER}
spring:
security:
oauth2:
resourceserver:
jwt:
# Note the trailing slash is important!
issuer-uri: https://{DOMAIN}/You can run the application using Gradle or Docker.
Linux / MacOS:
./gradlew clean bootRunWindows:
gradle clean bootRunLinux / MacOS:
sh exec.shWindows:
./exec.ps1
Linux / macOS:
./gradlew clean testWindows:
gradlew.cmd clean testUsing a REST client such as Postman or cURL, issue a GET request to http://localhost:3010/api/public. You should receive the response:
{"message":"All good. You DO NOT need to be authenticated to call /api/public."}Next, issue a GET request to http://localhost:3010/api/private. You should receive a 401 Unauthorized response.
To test that your API is properly secured, you can obtain a test token in the Auth0 Dashboard:
- Go to the Machine to Machine Applications tab for the API you created above.
- Ensure that your API test application is marked as authorized.
- Click the Test tab, then COPY TOKEN.
Issue a GET request to the /api/private endpoint, this time passing the token you obtained above as an Authorization header set to Bearer YOUR-API-TOKEN-HERE. You should then see the response:
{"message":"All good. You can see this because you are Authenticated."}Finally, to test that the /api/private-scoped is properly protected by the read:messages scope, make a GET request to the /api/private-scoped endpoint using the same token as above. You should see a 403 Forbidden response, as this token does not possess the read:messages scope.
Back in the Auth0 Dashboard:
- Go to the Permissions tab for the API you created above.
- Add a permission of
read:messagesand provide a description. - Go to the Machine to Machine Applications tab.
- Expand your authorized test application, select the
read:messagesscope, then click UPDATE and then CONTINUE. - Click the Test tab, then COPY TOKEN.
Issue a GET request to /api/private-scoped, this time passing the token you obtained above (with the read:messages scope) as an Authorization header set to Bearer YOUR-API-TOKEN-HERE. You should see the response:
{"message":"All good. You can see this because you are Authenticated with a Token granted the 'read:messages' scope"}Auth0 helps you to:
- Add authentication with multiple authentication sources, either social like Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others, or enterprise identity systems like Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider.
- Add authentication through more traditional username/password databases.
- Add support for linking different user accounts with the same user.
- Support for generating signed Json Web Tokens to call your APIs and flow the user identity securely.
- Analytics of how, when and where users are logging in.
- Pull data from other sources and add it to the user profile, through JavaScript rules.
- Go to Auth0 and click Sign Up.
- Use Google, GitHub or Microsoft Account to login.
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
Ivan Trendafilov
This project is licensed under the MIT license. See the LICENSE file for more info.