Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork70
The goal of this project is to create a simple Spring Boot REST API, called simple-service, and secure it with Keycloak. Furthermore, the API users will be loaded into Keycloak from OpenLDAP server.
ivangfr/springboot-keycloak-openldap
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
The goal of this project is to create a simpleSpring Boot REST API, calledsimple-service, and secure it withKeycloak. Furthermore, the API users will be loaded intoKeycloak fromOpenLDAP server.
Note: In the
springboot-react-keycloakrepository, we have implemented amovies-appusingKeycloak(withPKCE). This application consists of two services: the backend that was implemented usingSpring Bootand the frontend implemented withReactJS.
Onivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
- [Medium]Implementing and Securing a Simple Spring Boot REST API with Keycloak
- [Medium]Implementing and Securing a Simple Spring Boot UI (Thymeleaf + RBAC) with Keycloak
- [Medium]Implementing and Securing a Spring Boot GraphQL API with Keycloak
- [Medium]Setting Up OpenLDAP With Keycloak For User Federation
- [Medium]Integrating GitHub as a Social Identity Provider in Keycloak
- [Medium]Integrating Google as a Social Identity Provider in Keycloak
- [Medium]Building a Single Spring Boot App with Keycloak or Okta as IdP: Introduction
- [Medium]Implementing a Full Stack Web App using Spring-Boot and React
- [Medium]Using Keycloak to secure a Full Stack Web App implemented with Spring-Boot and React
Spring BootWeb Java application that exposes the following endpoints:GET /api/public: it's a not secured endpoint, everybody can access it;GET /api/private: it's a secured endpoint, only accessible by users that provide aJWTaccess token issued byKeycloakand the token must contain the roleUSER.
Open a terminal and inside the
springboot-keycloak-openldaproot folder run:docker compose up -d
Just wait for the Docker containers to start running. The
KeycloakDocker container usually takes longer. You can check its progress by running this command:docker logs -f keycloak
Press
Ctrl+Cto exitOnce you see the following log,
Keycloakhas started:INFO [io.quarkus] (main) Keycloak 26.4.1 on JVM (powered by Quarkus 3.27.0) started in 55.566s. Listening on: http://0.0.0.0:8080. Management interface listening on http://0.0.0.0:9000.
TheLDIF file that we will use,springboot-keycloak-openldap/ldap/ldap-mycompany-com.ldif, contains a predefined structure formycompany.com. Basically, it has 2 groups (developers andadmin) and 4 users (Bill Gates,Steve Jobs,Mark Cuban andIvan Franchin). Additionally, it is defined thatBill Gates,Steve Jobs andMark Cuban belong todevelopers group andIvan Franchin belongs toadmin group.
Bill Gates > username: bgates, password: 123Steve Jobs > username: sjobs, password: 123Mark Cuban > username: mcuban, password: 123Ivan Franchin > username: ifranchin, password: 123There are two ways to import those users: by running a script or by usingphpLDAPadmin.
In a terminal and inside the
springboot-keycloak-openldaproot folder run:./import-openldap-users.sh
The command below can be used to check the imported users:
ldapsearch -x -D"cn=admin,dc=mycompany,dc=com" \ -w admin -H ldap://localhost:389 \ -b"ou=users,dc=mycompany,dc=com" \ -s sub"(uid=*)"
Accesshttps://localhost:6443
Login with the credentials:
Login DN: cn=admin,dc=mycompany,dc=comPassword: adminImport the file
springboot-keycloak-openldap/ldap/ldap-mycompany-com.ldif.You should see a tree like the one shown in the picture below:
There are two ways: running a script or usingKeycloak website.
In a terminal, make sure you are inside the
springboot-keycloak-openldaproot folder.Run the script below to configure
Keycloakforsimple-serviceapplication:./init-keycloak.sh
It creates
company-servicesrealm,simple-serviceclient,USERclient role,ldapfederation and the usersbgatesandsjobswith the roleUSERassigned.Copy
SIMPLE_SERVICE_CLIENT_SECRETvalue that is shown at the end of the script. It will be needed whenever we callKeycloakto get aJWTaccess token to accesssimple-service.
Please have a look at thisMedium article,Setting Up OpenLDAP With Keycloak For User Federation
Open a new terminal and make sure you are in the
springboot-keycloak-openldaproot folder.Start the application by running the following command:
./mvnw clean spring-boot:run --projects simple-service -Dspring-boot.run.jvmArguments="-Dserver.port=9080"
Open a new terminal.
Call the endpoint
GET /api/public:curl -i http://localhost:9080/api/public
It should return:
HTTP/1.1 200It is public.Try to call the endpoint
GET /api/privatewithout authentication:curl -i http://localhost:9080/api/private
It should return:
HTTP/1.1 401Create an environment variable that contains the
Client Secretgenerated byKeycloaktosimple-serviceatConfigure Keycloak step:SIMPLE_SERVICE_CLIENT_SECRET=...
Run the command below to get an access token for
bgatesuser:BGATES_ACCESS_TOKEN=$(curl -s -X POST \"http://localhost:8080/realms/company-services/protocol/openid-connect/token" \ -H"Content-Type: application/x-www-form-urlencoded" \ -d"username=bgates" \ -d"password=123" \ -d"grant_type=password" \ -d"client_secret=$SIMPLE_SERVICE_CLIENT_SECRET" \ -d"client_id=simple-service"| jq -r .access_token)echo$BGATES_ACCESS_TOKEN
Note: Injwt.io, you can decode and verify the
JWTaccess tokenCall the endpoint
GET /api/private:curl -i http://localhost:9080/api/private -H"Authorization: Bearer$BGATES_ACCESS_TOKEN"It should return:
HTTP/1.1 200bgates, it is private.The access token default expiration period is
5 minutes. So, wait for this time and, using the same access token, try to call the private endpoint.It should return:
HTTP/1.1 401WWW-Authenticate: Bearer realm="company-services", error="invalid_token", error_description="Token is not active"
Click
GET /api/publicto open it. Then, clickTry it outbutton and, finally, clickExecutebutton.It should return:
Code: 200Response Body: It is public.Now click
GET /api/privatesecured endpoint. Let's try it without authentication. Then, clickTry it outbutton and, finally, clickExecutebutton.It should return:
Code: 401Details: Error: response status is 401In order to access the private endpoint, you need an access token. So, open a terminal.
Create an environment variable that contains the
Client Secretgenerated byKeycloaktosimple-serviceatConfigure Keycloak step:SIMPLE_SERVICE_CLIENT_SECRET=...
Run the following commands:
BGATES_ACCESS_TOKEN=$(curl -s -X POST \"http://localhost:8080/realms/company-services/protocol/openid-connect/token" \ -H"Content-Type: application/x-www-form-urlencoded" \ -d"username=bgates" \ -d"password=123" \ -d"grant_type=password" \ -d"client_secret=$SIMPLE_SERVICE_CLIENT_SECRET" \ -d"client_id=simple-service"| jq -r .access_token)echo$BGATES_ACCESS_TOKEN
Copy the token generated and go back to
Swagger.Click
Authorizebutton and paste the access token in theValuefield. Then, clickAuthorizebutton and, to finalize, clickClose.Go to
GET /api/privateand call this endpoint again, now with authentication.It should return:
Code: 200Response Body: bgates, it is private.
You can get an access token forsimple-service usingclient_id andclient_secret
- Accesshttp://localhost:8080;
- Click the dropdown button that contains
Keycloakand selectcompany-services; - On the left menu, click
Clients; - Select
simple-serviceclient; - In
Settingstab:- Go to
Capability configand checkService accounts rolescheckbox; - Click
Savebutton;
- Go to
- In
Service account rolestab:- Click
service-account-simple-servicelink present in the info message;"To manage detail and group mappings, click on the username service-account-simple-service"
- In
Role mappingtab:- Click
Assign rolebutton; - Make sure the
Filter by clientsis selected in the first dropdown button; - In
Search by role name, typesimple-serviceand pressEnter; - Select
[simple-service] USERname and clickAssignbutton; - Now,
service-account-simple-servicehas the roleUSERof thesimple-serviceassigned.
- Click
- Click
Open a terminal.
Create an environment variable that contains the
Client Secretgenerated byKeycloaktosimple-serviceatConfigure Keycloak step.SIMPLE_SERVICE_CLIENT_SECRET=...
Run the following command:
CLIENT_ACCESS_TOKEN=$(curl -s -X POST \"http://localhost:8080/realms/company-services/protocol/openid-connect/token" \ -H"Content-Type: application/x-www-form-urlencoded" \ -d"grant_type=client_credentials" \ -d"client_secret=$SIMPLE_SERVICE_CLIENT_SECRET" \ -d"client_id=simple-service"| jq -r .access_token)echo$CLIENT_ACCESS_TOKEN
Try to call the endpoint
GET /api/private:curl -i http://localhost:9080/api/private -H"Authorization: Bearer$CLIENT_ACCESS_TOKEN"It should return:
HTTP/1.1 200service-account-simple-service, it is private.
In a terminal, make sure you are in the
springboot-keycloak-openldaproot folder.Build Docker Image:
- JVM
./build-docker-images.sh
- Native
./build-docker-images.sh native
Environment Variable Description KEYCLOAK_HOSTSpecify host of the Keycloakto use (defaultlocalhost)KEYCLOAK_PORTSpecify port of the Keycloakto use (default8080)- JVM
Run Docker Container:
docker run --rm --name simple-service \ -p 9080:8080 \ -e KEYCLOAK_HOST=keycloak \ --network=springboot-keycloak-openldap_default \ ivanfranchin/simple-service:1.0.0
Open a new terminal.
Create an environment variable that contains the
Client Secretgenerated byKeycloaktosimple-serviceatConfigure Keycloak step.SIMPLE_SERVICE_CLIENT_SECRET=...
Run the commands below to get an access token for
bgatesuser:BGATES_TOKEN=$( docker run -t --rm -e CLIENT_SECRET=$SIMPLE_SERVICE_CLIENT_SECRET --network springboot-keycloak-openldap_default alpine/curl:latest sh -c' curl -s -X POST http://keycloak:8080/realms/company-services/protocol/openid-connect/token \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=bgates" \ -d "password=123" \ -d "grant_type=password" \ -d "client_secret=$CLIENT_SECRET" \ -d "client_id=simple-service"')BGATES_ACCESS_TOKEN=$(echo$BGATES_TOKEN| jq -r .access_token)
Call the endpoint
GET /api/private:curl -i http://localhost:9080/api/private -H"Authorization: Bearer$BGATES_ACCESS_TOKEN"It should return:
HTTP/1.1 200bgates, it is private.
- To stop the
simple-serviceapplication, go to the terminal where it is running and pressCtrl+C; - To stop and remove Docker Compose containers, network, and volumes, go to a terminal and, inside the
springboot-keycloak-openldaproot folder, run the following command:docker compose down -v
To remove the Docker image create by this project, go to a terminal and, inside thespringboot-keycloak-openldap root folder, run the following script:
./remove-docker-images.sh
About
The goal of this project is to create a simple Spring Boot REST API, called simple-service, and secure it with Keycloak. Furthermore, the API users will be loaded into Keycloak from OpenLDAP server.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.


