Level: Beginner
Technologies: Jakarta EE
Summary: Servlet application protected with Elytron OIDC and Keycloak Authorization Services
Target Product: Keycloak, WildFly
This quickstart demonstrates how to enable fine-grained authorization to a Jakarta Servlet application in order to protect specific resources and build a dynamic menu based on the permissions obtained from a Keycloak Server.
For this application, users can be regular users, premium users or administrators, where:
- Regular users have very limited access.
- They are allowed to access the main page (src/main/webapp/index.jsp) and the page that generates a dynamic menu (src/main/webapp/protected/dynamicMenu.jsp)
- Premium users have access to the premium area
- They are allowed to access a specific set of pages for premium users (src/main/webapp/protected/premium
- Administrators have access to the administration area
- They are allowed to access a specific set of pages for administrators (src/main/webapp/protected/admin)
The dynamic menu is built based on the permissions obtained from the server and using the AuthorizationContext
object to
determine the resources and scopes the user is allowed to access.
You'll also learn how to use the AuthorizationContext
object to obtain permissions granted by the server and perform additional checks in the application.
To compile and run this quickstart you will need:
- JDK 17
- Apache Maven 3.8.6
- Wildfly 28+
- Keycloak 21+
- Docker 20+
To start a Keycloak Server you can use OpenJDK on Bare Metal, Docker, Openshift or any other option described in Keycloak Getting Started guides. For example when using Docker just run the following command in the root directory of this quickstart:
docker run --name keycloak \
-e KEYCLOAK_ADMIN=admin \
-e KEYCLOAK_ADMIN_PASSWORD=admin \
--network=host \
quay.io/keycloak/keycloak:{KC_VERSION} \
start-dev \
--http-port=8180
where KC_VERSION
should be set to 21.0.0 or higher.
You should be able to access your Keycloak Server at http://localhost:8180.
Log in as the admin user to access the Keycloak Administration Console. Username should be admin
and password admin
.
Import the realm configuration file to create a new realm called quickstart
.
For more details, see the Keycloak documentation about how to create a new realm.
In order to deploy the example application, you need a Wildfly Server up and running. For more details, see the Wildfly documentation about how to install the server.
Make sure the server is accessible from localhost
and listening on port 8080
.
-
Open a terminal and navigate to the root directory of this quickstart.
-
The following shows the command to deploy the quickstart:
mvn -Djakarta clean wildfly:deploy
You can access the application with the following URL: http://localhost:8080/jakarta-servlet-authz-client.
Try to authenticate with any of these users:
Username | Password | Roles |
---|---|---|
alice | alice | user |
jdoe | jdoe | user, user_premium |
admin | admin | admin |
If you want to play around, try the following steps:
- Add
user_premium
toalice
and see if she is now able to access premium resources as well if the dynamic menu changes and displayDo user premium thing
- Grant to
alice
the necessary permission to access administrative resources.- Open the Keycloak admin console and make sure the
quickstart
realm is selected - Click on
Clients
on the left-side menu and selectjakarta-servlet-authz-client
client - Click on
Authorization
tab and then click thePolicies
tab - On the
Policies
tab, create a newUser-based Policy
by selectingalice
user. Name this policy asOnly Alice Policy
- Click on
Permissions
tab and selectAdministrative Resource Permission
- Add
Only Alice Policy
policy to theApply Policy
field - Change
Decision Strategy
fromUnanimous
toAffirmative
. ClickSave
- Add
- Logout and log in again as
alice
user and try to access http://localhost:8080/jakarta-servlet-authz-client/protected/admin/onlyAdmin.jsp
- Open the Keycloak admin console and make sure the
- Revoke
alice
administrative permissions by changing theOnly Alice Policy
logic- Click on
Policies
tab and selectOnly Alice Policy
- Change
Logic
field fromPositive
toNegative
. ClickSave
- Change
- Logout and log in again as
alice
user and try to access http://localhost:8080/jakarta-servlet-authz-client/protected/admin/onlyAdmin.jsp
- Click on
For more information, please consult the Authorization Services documentation.
-
Open a terminal and navigate to the root directory of this quickstart.
-
The following shows the command to deploy the quickstart:
mvn -Djakarta clean wildfly:undeploy
Make sure Keycloak is running.
You don't need Wildfly running because a temporary server is started during test execution.
-
Open a terminal and navigate to the root directory of this quickstart.
-
Run the following command to build and run tests:
mvn -Djakarta clean verify