This lab implements security using Spring Security, JWT, and OAauth2.
- Clone the repository
- Start the Docker dependencies by running
docker-compose up - Log into Keycloak at
http://localhost:9999/adminwith the credentialsadmin/admin - In Keycloak, create a new realm called
security-lab - Create a new client called
jwt-client - Enable Client Authentication and Authorization for the client
- IMPORTANT: Regenerate the client secret. Navigate to the
Credentialstab for the client and click onRegenerateon theSecretfield. - Create a new user called
java4userwith the email verified enabled. - Set the password for the user to
password1234, with temporary disabled. - Optionally, navigate to Realm Settings -> Tokens and set the Access Token Lifespan to 30 minutes.
- Log into Keycloak at
http://localhost:9999/adminwith the credentialsadmin/admin - In Keycloak, navigate to the
security-labrealm - Navigate to the
jwt-clientclient - Click on the
Credentialstab - Copy the
Secretvalue - Run the following command to retrieve the access token:
curl -X POST 'http://localhost:9999/realms/security-lab/protocol/openid-connect/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'grant_type=password' \ --data-urlencode 'client_id=jwt-client' \ --data-urlencode 'client_secret=<client secret>' \ --data-urlencode 'username=java4user' \ --data-urlencode 'password=password1234' \ --data-urlencode 'scope=<space separated list of scopes>'
- Copy the
access_tokenvalue - Use the
access_tokenas a Bearer token to make requests to the application
In order to restrict access to the different operations, we need to create client scopes for each operation.
- Log into Keycloak at
http://localhost:9999/adminwith the credentialsadmin/admin - In Keycloak, navigate to the
security-labrealm - Navigate to the
Client Scopestab - Create a new client scope called
<model>:read. Set the scope to "Optional" and "Include in Token Scope" to true. - Follow the same steps to create a new client scope for the "update", "create", and "delete" operations.
1Run the application by executing ./gradlew bootRun
2The application will be available at http://localhost:8080
List your scopes here: