czetsuya / Spring-Keycloak-with-REST-API

Securing a Spring REST API with Keycloak with bearer-only=true.
12 stars 13 forks source link
jwt keycloak rest-api spring-boot spring-boot-2 spring-rest spring-security-oauth2

License: GPL v3

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:

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:

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