Authorization Services
Configuration
Before proceeding with the next sections, we need to configure the realm, roles and users in our Red Hat build of Keycloak instance.
-
Open a browser window and log in to the Red Hat build of Keycloak administration web console.
-
Create the
demo
realm. Click on themaster
realm dropdown menu, clickCreate Realm
.
-
Click
Create
.
-
Create the
staff
group. Click onGroups
.
-
Then click
Create group
.
-
Set
staff
as the group name, and clickCreate
.
-
Create the child group named
Personal staff
. Click on the kebab button of thestaff
group and clickCreate child group
.
-
Set
Personal staff
as the name, and clickCreate
.
-
Create the role
vet
. Click onRealm roles
, thenCreate role
.
-
Set the Role name, and click
Save
.
-
Repeat the same steps for creating a role named
assistant
.
-
Create the user
angel
. Click onUsers
, thenAdd user
.
-
Set the username, and click
Create
.
-
Open the
Credentials
tab and set the password for the user. Make sure to setTemporary
toOFF
before setting the password.
-
Open the
Role Mappings
tab and assign thevet
role to the user.
-
Open the
Groups
tab and assign the userangel
to thePersonal staff
group.
-
Repeat the same steps for creating a user named
elisabeth
but assign theassistant
role. Also, remember to incude the user in thePersonal staff
group.
Securing Web Applications
The sample application that we will use in this section is the Quarkus Petclinic project
.
In this tutorial, we will work with a version that has been modified for the tutorial.
The repository for this modified version is:
To get started, clone the repository and switch to the rhbk-base-22
branch:
git clone -b rhbk-base-22 https://github.com/atarazana/quarkus-petclinic.git
Test the application:
./mvnw clean quarkus:dev
-
Open a browser window and visit the application URL.
Before securing the application, let’s configure a new client in our Red Hat build of Keycloak realm.
-
Open a browser window and log in to the Red Hat build of Keycloak administration web console.
-
Select our
demo
realm and clickClients
. ClickCreate client
.
-
Set
quarkus-petclinic
asClient ID
. ClickNext
untilSave
.
-
On the
quarkus-petclinic
client configuration page:-
Enable
Client authentication
. -
Enable
Authorization
. -
Set
Valid Redirect URIs
to include the root context of your application. -
Click
Save
.
-
At this point, we have the demo
realm with the quarkus-petclinic
client; additionally, we created two roles and two users: the user angel
that has the vet
role and the user elisabeth
that has the role assistant
.
-
The authorization granularity that we want to configure based on our application is the following:
-
Any user that belongs to the
demo
realm, likeelisabeth
, can browse the application except theVETERINARIANS
resource. -
Only the users with the role
vet
can view theVETERINARIANS
resource (which has the/vets.html
context). -
Any other user access outside the realm will be rejected.
-
Let’s configure our client authorization.
-
Open a browser window and log in to the Red Hat build of Keycloak administration web console. Browse to our
quarkus-petclinic
client and click theAuthorization
and thenPolicies
tabs.
-
Click
Create policy
, selectGroup
.
-
Set
Default Group Policy
as theName
. Add thestaff
group to theGroups
list. Make sure that theLogic
is set toPositive
andExtend to Children
is checked (to enable the checkbox, you may need to clickSave
first). Finally, clickSave
.
-
Come back, select the
Resources
tab and clickCreate Permission
for theDefault Resource
.
-
Set
Default Resource Permission
asName
and selectDefault Resource
asResources
. Add the recently createdDefault Group Policy
policy. ClickSave
.
-
Come back again, select the
Resources
tab and create a new resource by clicking onCreate resource
.
-
Set
Vets Resource
asName
andDisplay name
, and/vets.html
as URI. ClickSave
.
-
Browse to
Authorization
andPolicies
tabs. Click onCreate Policy
.
-
Select
Role
-
Set
Vet Role Policy
as the Name,vet
asRealm Roles
, and check required. ClickSave
.
-
Browse to the
Authorization
andResources
tabs. -
Click on
Create Permission
for theVets Resource
.
-
Set
Vets Resource Permission
asName
. And apply theVet Role Policy
. ClickSave
.
At this point, the Red Hat build of Keycloak client is properly configured.
Let’s configure the application side.
-
Open a browser window and log in to the Red Hat build of Keycloak administration web console. Browse to our
quarkus-petclinic
client and click theCredentials
tab. Write down theSecret
value, we will need it soon.
-
Export the client secret
export KEYCLOAK_CLIENT_SECRET=<the secret>
-
Add the following dependencies to the
pom.xml
file:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-keycloak-authorization</artifactId>
</dependency>
-
Add the following configuration to the
application.properties
file of the application, as shown below:
quarkus.oidc.auth-server-url=http://${KEYCLOAK_HOST:localhost:8080}/realms/demo
quarkus.oidc.client-id=quarkus-petclinic
quarkus.oidc.credentials.secret=${KEYCLOAK_CLIENT_SECRET:secret}
quarkus.oidc.tls.verification=none
quarkus.oidc.roles.source=accesstoken
quarkus.oidc.application-type=web-app
quarkus.oidc.webapp.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc.webapp.client-id=${quarkus.oidc.client-id}
quarkus.oidc.webapp.credentials.secret=${quarkus.oidc.credentials.secret}
quarkus.oidc.webapp.roles.source=${quarkus.oidc.roles.source}
quarkus.keycloak.policy-enforcer.enable=true
-
Redeploy and test the application accesses:
-
Open a new incognito browser session.
-
Browse to the context root of the Quarkus Petclinic application. The request will be redirected to the Red Hat Single Sign-On login page. Here, we checked that the anonymous access to the application is forbidden.
-
Log in as
elisabeth
. The request will be redirected to the application after a successful login. -
Browse through the application, try to access the VETERINARIANS section. The access should be denied, as
elisabeth
has theassistant
role and the access is restricted to thevet
role. An error will be shown on the application page. -
Close the browser and open a new one incognito session.
-
Visit the context root of the application again.
-
Login as
angel
. -
Browse through the application, try to access the VETERINARIANS section. The access should be granted, as
angel
has thevet
role.
-
A complete OIDC working Petclinic Quarkus application is available at rhbk-oidc-22 branch.
|