Skip to content

thainguyen101b/keycloak-java-example

Repository files navigation

keycloak examples

See config file at /config folder

Keycloak basic authz service with spring boot

Step 1:

# Start keycloak container
docker compose up -d keycloak-rest-authz-resource-server

Step 2:

# Start spring boot
./mvnw spring-boot:run -pl rest-authz-resource-server

There are two users:

Username Password Role
alice alice user
jdoe jdoe user, user_premium
endpoint Permission
/ user
/protected/premium user_premium

Test 1

GET http://localhost:8080
Authorization: Bearer ${alice_token}

The result should be : Hello alice!

Test 2

GET http://localhost:8080/protected/premium
Authorization: Bearer ${alice_token}

The result should be : 403

Test 3

GET http://localhost:8080/protected/premium
Authorization: Bearer ${jdoe_token}

The result should be : Hello jdoe!

More details authz service with bank-rest-authz-resource-server

Step 1:

# Start keycloak container
docker compose up -d keycloak-bank-rest-authz-resource-server

Step 2:

# Start spring boot
./mvnw spring-boot:run -pl bank-rest-authz-resource-server

Step 3:

Please visit Keycloak admin console http://localhost:8180 with username|password admin|admin and then create two users with following info.

Username Password Role
bob bob bank_teller
alice alice account_owner

This application is configured by policy-enforcer.json find more details in keycloak policy-enforcer

Test

Endpoint Methods Users Result
/accounts/* GET bob/alice permit
/accounts/* POST,PUT bob deny
/accounts/* POST,PUT alice permit

Keycloak javascript adapter example

This example using:

  • react with vite
  • keycloak-js library

You can find more details in keycloak javascript-adapter

Step 1

# Start keycloak container
docker compose up -d keycloak-javascript-adapter

Step 2

run these commands

cd keycloak-javascript-adapter
npm i 
npm run dev 

About

Keycloak examples (Authorization Service, OAuth2 Resource Server, keycloak.js)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published