Level: Beginner Technologies: SpringBoot Summary: SpringBoot REST Service Protected Using Keycloak Authorization Services Target Product: Keycloak Source: https://github.com/keycloak/Keycloak-quickstarts
The app-authz-rest-employee quickstart demonstrates how to protect a SpringBoot REST service using Keycloak Authorization Services.
This quickstart provides a RESTful API exposing a single endpoint to obtain information about employees. Employees can only obtain information about themselves, but access is not granted if trying to obtain information about other employee.
However, users granted with a role people-manager are also allowed to access information about any employee.
The endpoint is available at:
http://localhost:8080/api/{employee}
Where {employee} should be replaced by the employee's username, such as /api/alice.
You can use two distinct users to access this application:
| Username | Password | Roles |
|---|---|---|
| alice | alice | user |
| jdoe | jdoe | user, people-manager |
The quickstart demonstrates how to use Keycloak Authorization Services to:
- Pass information about the execution environment to policies in order to evaluate permissions for a resource
- Use a JavaScript-based policy to define constraints based on the execution environment
- Mix different policies and define how permissions should be evaluated
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.1.1 or later.
Prior to running the quickstart you need to create a realm in Keycloak with all the necessary configuration to deploy and run the quickstart.
The following steps show how to create the realm required for this quickstart:
- Open the Keycloak Admin Console
- In the top left corner dropdown menu that is titled
Master, clickAdd Realm. If you are logged in to the master realm this dropdown menu lists all the realms created. - For this quickstart we are not going to manually create the realm, but import all configuration from a JSON file. Click on
Select Fileand import the config/quickstart-realm. - Click
Create
The steps above will result on a new spring-boot-quickstart realm.
Make sure your Keycloak server is running on http://localhost:8180/. For that, you can start the server using the command below:
cd {KEYCLOAK_HOME}/bin
./standalone.sh -Djboss.socket.binding.port-offset=100
If your server is up and running, perform the following steps to start the application:
-
Open a terminal and navigate to the root directory of this quickstart.
-
The following shows the command to run the application:
mvn spring-boot:run
First thing, your client needs to obtain an OAuth2 access token from a Keycloak server for user alice. You can use the same command to obtain tokens
on behalf of user jdoe, just make sure to change both username and password request parameters.
export access_token=$(\
curl -X POST http://localhost:8180/auth/realms/spring-boot-quickstart/protocol/openid-connect/token \
-H 'Authorization: Basic YXBwLWF1dGh6LXJlc3QtZW1wbG95ZWU6c2VjcmV0' \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'username=alice&password=alice&grant_type=password' | jq --raw-output '.access_token' \
)The most simple way to invoke resources protected by a policy enforcer is sending an OAuth2 Access Token. If you successfully obtained an OAuth2 Access Token in the previous section, you can access resources in this application as follows:
curl -v -X GET \
http://localhost:8080/api/alice \
-H "Authorization: Bearer "$access_tokenUser alice should be able to access information about herself and you should get a response as follows:
{
"name": "alice"
}
User alice can not access information about jdoe and you should get an access denied.
curl -v -X GET \
http://localhost:8080/api/jdoe \
-H "Authorization: Bearer "$access_tokenUser jdoe is granted with role people-manager and you should be able to access information about any employee,
including alice. For that, make sure to obtain an access token for user jdoe and then send a request to:
curl -v -X GET \
http://localhost:8080/api/alice \
-H "Authorization: Bearer "$access_token
- Try to remove the policy
Only People Manager Policyfrom the permissionEmployee Permissionand youll see thatjdoeis no longer granted with access to query information aboutalice`. - Try to change
Employee Policy, a JavaScript-based policy, to change behavior.
- Make sure you have an Keycloak server running with an admin user in the
masterrealm or use the provided docker image - Be sure to set the
TestHelper.keycloakBaseUrlin thecreateArchivemethod (default URL is localhost:8180/auth). - Set accordingly the correct url for the
keycloak.auth-server-urlin the test application.properties. - Run
mvn test -Pspring-boot