Giter Club home page Giter Club logo

quarkus-quickstart's Introduction

quarkus-keycloak-quickstart

This quickstart will show you :

  • How to obtain a Keycloak Access Token from a front-end application
  • How to Use that Access Token to call in secure way a first Quarkus Rest Application
  • How this first service can propagate this token to another Quarkus Rest Service, again in a secure way.

This quickstart consists of :

  • A simple Web App that performs the Keycloak Login and that call the quarkus-rest-username
  • The quarkus-rest-username , a Quarkus REST app, just returns the username of the authenticated user.
  • The quarkus-rest-caps, a Quarkus REST app, receives as path parameter a String and capitalize it.

The flow is pretty simple :

  1. User --login--> quarkus-front <-- returns token --> Keycloak Server
  2. quarkus-front -- calls --> quarkus-rest-username -- calls --> quarkus-rest-caps

Prerequisites

  • JDK8+
  • Maven
  • A running instance of Keycloak 5.0.0 running on port 8180
    • If you have Docker you can also run your Keycloak Server like this :
    docker run -d --name keycloak -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -p 8180:8180 -v `pwd`/quarkus-kc-quickstart.json:/config/quarkus-kc-quickstart.json -it jboss/keycloak:6.0.1 -b 0.0.0.0 -Djboss.http.port=8180 -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=/config/quarkus-kc-quickstart.json -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
    

Instructions

  • Import the Realm quarkus-kc-quickstart.json in your Keycloak Server (except if you are using the docker image from above)
  • Go to the quarkus-front folder and start the app : mvn package quarkus:dev
  • Go to the quarkus-rest-username folder and start the app : mvn package quarkus:dev
  • Go to the quarkus-rest-caps folder and start the app : mvn package quarkus:dev

Running the app

  • Go to http://localhost:8080 and press the login button
  • Login with username and password test/test
  • In the accordeon menu go to service call and press the button
  • Next to Service Call Result you should see the result TEST

Focus on how the smallrye-jwt extension is used

The service receives a JWT Access Token from the quarkus-front-end application. To handle the JWT, we use the smallrye-jwt extension :

 <dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-smallrye-jwt</artifactId>
 </dependency>

Then, it just need some configuration, notice the issuer and the publickey.location properties :

mp.jwt.verify.publickey.location=http://localhost:8180/auth/realms/quarkus-quickstart/protocol/openid-connect/certs
mp.jwt.verify.issuer=http://localhost:8180/auth/realms/quarkus-quickstart
quarkus.smallrye-jwt.auth-mechanism=MP-JWT
quarkus.smallrye-jwt.enabled=true

Then in your JAXRS resource, you just need to add an annotation to your endpoint method :

@GET
@RolesAllowed({"user"})
public String getUsername() {

Also notice how easy it is to inject any claim of your token :

 @Inject
 @Claim(standard = Claims.preferred_username)
 Optional<JsonString> preferred_username;

To call the quarkus-rest-caps service we use the quarkus-smallrye-rest-client extension, notice the annotation @RegisterClientHeaders

@Path("/caps")
@RegisterRestClient
@RegisterClientHeaders
public interface CapsService {

By doing this, we indicate that we want to propagate HTTP Headers from the JAXRS context. In our case, we want to propagate the Authorization header that contains our access token. In the properties we specify this header :

org.eclipse.microprofile.rest.client.propagateHeaders=Authorization

quarkus-quickstart's People

Contributors

sebastienblanc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.