monasharma1991 / Spring-Keycloak-with-REST-API

Securing a Spring REST API with Keycloak with bearer-only=true.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

patreon License: GPL v3

*Help me keep this project updated by supporting me on Patreon.

Spring Project Secured with Keycloak

A demo project created to demonstrate how a Spring project can be secured using a Keycloak server via bearer token.

SpringDoc URLs:

  • springdoc.api-docs=/api-docs
  • springdoc.swagger-ui.path=/swagger-ui.html

Requirements:

*In case you will be using Keycloak version greater than 7.0.0, the Keycloak team has introduced the notion of feature and uploading the json file has become one of them. Sadly, it is disabled by default. To enable realm, upload run Keycloak with the given parameter:

standalone.bat -Djboss.socket.binding.port-offset=1 -Dkeycloak.profile.feature.upload_scripts=enabled

Here's the documentation: https://www.keycloak.org/docs/latest/server_installation/, look at the profiles section.

Running Keycloak in Docker

To run Keycloak as a docker container and enable the realm configuration upload, the keycloak.profile.feature.upload_scripts must be set.

docker run --name=keycloak10 -d -p 8080:8080 -e JAVA_OPTS="-Dkeycloak.profile.feature.scripts=enabled -Dkeycloak.profile.feature.upload_scripts=enabled" -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=kerri jboss/keycloak

Running Keycloak as Standalone

Import the realm file inside the config folder. If you're not familiar with the installation process, visit the blog I have written in the reference section below.

When you import the realm it will also create the users below.

Users

Role=User, kerri / kerri Role=Admin, admin / admin

Note:

  • Make sure that you have the same Keycloak client credentials value for the 2 project ands Keycloak server.

If keycloak.json file is to be used instead of application.yml, set the following system variable and make sure that you have the file keycloak.json in src/main/resources.

keycloak.configurationFile = classpath:keycloak.json

References

Authors

  • Edward P. Legaspi - Java Architect - czetsuya

About

Securing a Spring REST API with Keycloak with bearer-only=true.


Languages

Language:Java 100.0%