This project demonstrates how a third-party application can communicate and manage Keycloak resources via API.
- Using Spring STS IDE, create a new project using the Spring Starter Project template.
- Make sure to add the following dependencies: keycloak-spring-boot-starter and spring-boot-starter-security.
- We need to extend the class KeycloakWebSecurityConfigurerAdapter as specified in the keycloak securing apps documentation below. See the code below.
- Due to some Keycloak issues, we need to extend the class KeycloakSpringBootConfigResolver.
- I created a utility class that will help us initialize a Keycloak class that we can use to communicate and manage a Keycloak instance - KeycloakAdminClientUtils.
- We then need a service to get or manage information from Keycloak depending on the user's role. For instance, the user I'm using has a manage-realm role, this means that I can call almost all the API provided by Keycloak. In my example, I'm returning the user's role as well as its profile. See class KeycloakAdminClientService.
- I create a REST controller class to use the service in #6 for demo. See KeycloakController.
- Don't forget to specify the Keycloak configuration in application.properties.
- Download and run keycloak.
- Create a new realm and users by importing the file config/balambgarden-realm.json.
- If you are using an IDE make sure to set the environment variable keycloak.secret=332e78cb-0487-46a8-949d-7c2a09cd380c. This is use when calling the getProfile API.
- Now we're ready to run the tests inside the postman collection.
You must have docker installed on your local machine.
- Make sure to change the value of keycloak.auth-server-url in application.properties to where you will be running docker-compose.
- Build the application. Whether by using mvn in command line or in your IDE.
- Open your terminal.
- Navigate to the root folder of the project.
- Enter: "docker-compose up --build" without the " and press enter.
- Now we're ready to run the tests inside the postman collection.
If will take a while during the first time as it will download Keycloak.
docker run -d --name=keycloak10 -p 8081:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=kerri jboss/keycloak:10.0.1
- Install Postman
- Import the postman environment settings deployment/czetsuya-course.postman_environment.json and make sure to update the value of keycloak_url and api_url to where you deploy keycloak and running this project.
- Import the collection deployment/keycloak-admin-api.postman_collection.json.
- Run the Login test first so that the access_token will be initialized.
- https://www.czetsuyatech.com/2020/03/spring-rest-api-keycloak-admin.html
- https://czetsuya-tech.blogspot.com/2019/10/docker-and-kubernetes.html
- https://hub.docker.com/r/jboss/keycloak/
- https://spring.io/guides/gs/spring-boot-docker/
- https://www.keycloak.org/documentation
- https://www.keycloak.org/docs-api/10.0/rest-api/index.html