Chapter 9. OpenID Connect in JBoss EAP
Use the JBoss EAP native OpenID Connect (OIDC) client to secure your applications through an external OpenID provider. OIDC is an identity layer that enables clients, such as JBoss EAP, to verify a user’s identity based on OpenID provider authentication. For example, you can secure your JBoss EAP applications using Red Hat Single Sign-On as the OpenID provider.
9.1. OpenID Connect configuration in JBoss EAP Copy linkLink copied to clipboard!
When you secure your applications using an OpenID provider, you do not need to configure any security domain resources locally. The elytron-oidc-client
subsystem provides a native OpenID Connect (OIDC) client in JBoss EAP to connect with OpenID providers. JBoss EAP automatically creates a virtual security domain for your application, based on your OpenID provider configurations.
It is recommended to use the OIDC client with Red Hat Single Sign-On. You can use other OpenID providers if they can be configured to use access tokens that are JSON Web Tokens (JWTs) and can be configured to use the RS256, RS384, RS512, ES256, ES384, or ES512 signature algorithm.
To enable the use of OIDC, you can configure either the elytron-oidc-client
subsystem or an application itself. JBoss EAP activates the OIDC authentication as follows:
-
When you deploy an application to JBoss EAP, the
elytron-oidc-client
subsystem scans the deployment to detect if the OIDC authentication mechanism is required. -
If the subsystem detects OIDC configuration for the deployment in either the
elytron-oidc-client
subsystem or the application deployment descriptor, JBoss EAP enables the OIDC authentication mechanism for the application. -
If the subsystem detects OIDC configuration in both places, the configuration in the
elytron-oidc-client
subsystemsecure-deployment
attribute takes precedence over the configuration in the application deployment descriptor.
The keycloak-client-oidc
layer to secure your applications with Red Hat Single Sign-On is deprecated in JBoss EAP XP 4.0.0. Use the native OIDC client provided by the elytron-oidc-client
subsystem instead.
Deployment configuration
To secure an application with OIDC by using a deployment descriptor, update the application’s deployment configuration as follows:
Create a file called
oidc.json
in theWEB-INF
directory with the OIDC configuration information.Example
oidc.json
contentsCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Set the
auth-method
property toOIDC
in the application deployment descriptorweb.xml
file.
Example deployment descriptor update
<login-config> <auth-method>OIDC</auth-method> </login-config>
<login-config>
<auth-method>OIDC</auth-method>
</login-config>
Subsystem configuration
You can secure applications with OIDC by configuring the elytron-oidc-client
subsystem in the following ways:
- Create a single configuration for multiple deployments if you use the same OpenID provider for each application.
- Create a different configuration for each deployment if you use different OpenID providers for different applications.
Example XML configuration for a single deployment:
To secure multiple applications using the same OpenID provider, configure the provider
separately, as shown in the example:
9.2. Enabling the elytron-oidc-client subsystem Copy linkLink copied to clipboard!
The elytron-oidc-client
subsystem is provided in the standalone-microprofile.xml
configuration file. To use it, you must start your server with the bin/standalone.sh -c standalone-microprofile.xml
command. You can include the elytron-oidc-client
subsystem in the standalone.xml
configuration by enabling it using the management CLI.
Prerequisites
- You have installed JBoss EAP XP.
Procedure
Add the
elytron-oidc-client
extension using the management CLI./extension=org.wildfly.extension.elytron-oidc-client:add
/extension=org.wildfly.extension.elytron-oidc-client:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enable the
elytron-oidc-client
subsystem using the management CLI./subsystem=elytron-oidc-client:add
/subsystem=elytron-oidc-client:add
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Reload JBoss EAP.
reload
reload
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can now use the elytron-oidc-client
subsystem by starting the server normally, with the command bin/standalone.sh
9.3. Securing applications using OpenID Connect with Red Hat Single Sign-On Copy linkLink copied to clipboard!
You can use OpenID Connect (OIDC) to delegate authentication to an external OpenID provider. The elytron-oidc-client
subsystem provides a native OIDC client in JBoss EAP to connect with external OpenID providers.
To create an application secured with OpenID Connect using Red Hat Single Sign-On, follow these procedures:
9.3.1. Configuring Red Hat Single Sign-On as an OpenID provider Copy linkLink copied to clipboard!
Red Hat Single Sign-On is an identity and access management provider for securing web applications with single sign-on (SSO). It supports OpenID Connect (an extension to OAuth 2.0).
Prerequisites
- You have installed the Red Hat Single Sign-On server. For more information, see Installing the Red Hat Single Sign-On server in the Red Hat Single Sign-On Getting Started Guide.
- You have created a user in your Red Hat Single Sign-On server instance. For more information, see Creating a user in the Red Hat Single Sign-On Getting Started Guide.
Procedure
Start the Red Hat Single Sign-On server at a port other than 8080 because JBoss EAP default port is 8080.
Syntax
RH_SSO_HOME/bin/standalone.sh -Djboss.socket.binding.port-offset=<offset-number>
$ RH_SSO_HOME/bin/standalone.sh -Djboss.socket.binding.port-offset=<offset-number>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
/home/servers/rh-sso-7.4/bin/standalone.sh -Djboss.socket.binding.port-offset=100
$ /home/servers/rh-sso-7.4/bin/standalone.sh -Djboss.socket.binding.port-offset=100
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to the Admin Console at
http://localhost:<port>/auth/
. For example,http://localhost:8180/auth/
. - To create a realm, in the Admin Console, hover over Master, and click Add realm.
-
Enter a name for the realm. For example,
example_realm
. Ensure that Enabled is ON and click Create. - Click Users, then click Add user to add a user to the realm.
-
Enter a user name. For example,
jane_doe
. Ensure that User Enabled is ON and click Save. - Click Credentials to add a password to the user.
-
Set a password for the user. For example,
janedoep@$$
. Toggle Temporary to OFF and click Set Password. In the confirmation prompt, click Set password. - Click Clients, then click Create to configure a client connection.
-
Enter a client ID. For example,
my_jbeap
. Ensure that Client Protocol is set toopenid-connect
, and click Save. Click Installation, then select
Keycloak OIDC JSON
as the Format Option to see the connection parameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow When configuring your JBoss EAP application to use Red Hat Single Sign-On as the identity provider, you use the parameters as follows:
"provider-url" : "http://localhost:8180/auth/realms/example_realm", "ssl-required": "external", "client-id": "my_jbeap", "public-client": true, "confidential-port": 0
"provider-url" : "http://localhost:8180/auth/realms/example_realm", "ssl-required": "external", "client-id": "my_jbeap", "public-client": true, "confidential-port": 0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Click Clients, click Edit next to my_jbeap to edit the client settings.
In Valid Redirect URIs, enter the URL where the page should redirect after authentication is successful.
For this example, set this value to
http://localhost:8080/simple-oidc-example/secured/*
9.3.2. Configuring a Maven project for creating a secure application Copy linkLink copied to clipboard!
Create a Maven project with the required dependencies and the directory structure for creating a secure application.
Prerequisites
- You have installed Maven. For more information, see Downloading Apache Maven.
- You have configured your Maven repository for the latest release. For more information, see Maven and the JBoss EAP microprofile maven repository.
Procedure
Set up a Maven project using the
mvn
command. The command creates the directory structure for the project and thepom.xml
configuration file.Syntax
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the application root directory:
Syntax
cd <name-of-your-application>
$ cd <name-of-your-application>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd simple-oidc-example
$ cd simple-oidc-example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the generated
pom.xml
file as follows:Set the following properties:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following dependencies:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following build configuration to use
mvn widlfy:deploy
to deploy the application:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
In the application root directory, enter the following command:
mvn install
$ mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get an output similar to the following:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can now create your secure application.
9.3.3. Creating a secure application that uses OpenID Connect Copy linkLink copied to clipboard!
You can secure an application by either updating its deployment configuration or by configuring the elytron-oidc-client
subsystem. The following example demonstrates creating a servlet that prints a logged-in user’s Principal. For an existing application, only those steps that are related to updating the deployment configuration or the elytron-oidc-client
subsystem are required.
In this example, the value of the Principal comes from the ID token from the OpenID provider. By default, the Principal is the value of the "sub"
claim from the token. You can specify which claim value from the ID token to use as the Principal in one of the following:
-
The
elytron-oidc-client
subsystem attributeprincipal-attribute
. -
The
oidc.json
file.
<application_root> in the procedure denotes the pom.xml
file directory. The pom.xml
file contains your application’s Maven configuration.
Prerequisites
- You have created a Maven project. For more information, see Configuring Maven project for creating a secure application .
- You have configured Red Hat Single Sign-On as the OpenID provider. For more information, see Configuring Red Hat Single Sign-On as an OpenID provider.
-
You have enabled the
elytron-oidc-client
subsystem. For more information, see Enabling the elytron-oidc-client subsystem
Procedure
Create a directory to store the Java files.
Syntax
mkdir -p <application_root>/src/main/java/com/example/oidc
$ mkdir -p <application_root>/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
mkdir -p simple-oidc-example/src/main/java/com/example/oidc
$ mkdir -p simple-oidc-example/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the new directory.
Syntax
cd <application_root>/src/main/java/com/example/oidc
$ cd <application_root>/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd simple-oidc-example/src/main/java/com/example/oidc
$ cd simple-oidc-example/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a servlet "SecuredServlet.java" with the following content:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add security rules for access to your application in the deployment descriptor
web.xml
file located in theWEB-INF
directory of the application.Copy to Clipboard Copied! Toggle word wrap Toggle overflow To secure the application with OpenID Connect, either update the deployment configuration or configure the
elytron-oidc-client
subsystem.NoteIf you configure OpenID Connect in both the deployment configuration and the
elytron-oidc-client
subsystem, the configuration in theelytron-oidc-client
subsystemsecure-deployment
attribute takes precedence over the configuration in the application deployment descriptor.Updating the deployment configuration:
Create a file
oidc.json
in theWEB-INF
directory, like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the deployment descriptor
web.xml
file with the following text to declare that this application uses OIDC:<login-config> <auth-method>OIDC</auth-method> </login-config>
<login-config> <auth-method>OIDC</auth-method> </login-config>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Configuring the
elytron-oidc-client
subsystem:To secure your application, use the following management CLI command:
/subsystem=elytron-oidc-client/secure-deployment=simple-oidc-example.war/:add(client-id=my_jbeap,provider-url=http://localhost:8180/auth/realms/example_realm,public-client=true,ssl-required=external)
/subsystem=elytron-oidc-client/secure-deployment=simple-oidc-example.war/:add(client-id=my_jbeap,provider-url=http://localhost:8180/auth/realms/example_realm,public-client=true,ssl-required=external)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
In the application root directory, compile your application with the following command:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application.
mvn wildfly:deploy
$ mvn wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
In a browser, navigate to
http://localhost:8080/simple-oidc-example/secured
. Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can now log in to the application using the credentials you configured in the Red Hat Single Sign-On as the OpenID provider.
9.3.4. Restricting access to applications based on user roles Copy linkLink copied to clipboard!
You can restrict access to all, or parts, of your application based on user roles. For example, you can let users with the "public" role have access to the parts of your application that aren’t sensitive, and give users with the "admin" role access to those parts that are.
Prerequisites
- You have secured your application using OpenID Connect. For more information, see Creating a secure application that uses OpenID Connect.
Procedure
Update the deployment descriptor
web.xml
file with the following text:Syntax
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Allow only those users with the role
example_role
to access your application.
In the application root directory, recompile your application with the following command:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application.
mvn wildfly:deploy
$ mvn wildfly:deploy
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
In a browser, navigate to
http://localhost:8080/simple-oidc-example/secured
. Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Forbidden
Forbidden
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Because you have not assigned the required role to the user "jane_doe," jane_doe can’t log in to your application. Only the users with the required role can log in.
To assign users the required roles, see Creating and assigning roles to users in Red Hat Single Sign-On.
9.3.5. Creating and assigning user roles in Red Hat Single Sign-On Copy linkLink copied to clipboard!
Red Hat Single Sign-On is an identity and access management provider for securing your web applications with single sign-on (SSO). You can define users and assign roles in Red Hat Single Sign-On.
Prerequisites
- You have configured Red Hat Single Sign-On. For more information, see Configuring Red Hat Single Sign-On as an OpenID provider.
Procedure
-
Log in to the admin console at
http://localhost:<port>/auth/
. For example,http://localhost:8180/auth/
. - Click the realm you use to connect with JBoss EAP. For example, example_realm.
-
Click Clients, then click the
client-name
you configured for JBoss EAP. For example, my_jbeap. - Click Roles, then Add Role.
- Enter a role name, such as example_role, then click Save. This is the role name you configure in JBoss EAP for authorization.
- Click Users, then View all users.
- Click an ID to assign the role you created. For example, click the ID for jane_doe.
-
Click Role Mappings. In the Client Roles field, select the
client-name
you configured for JBoss EAP. For example, my_jbeap. - In Available Roles, select a role to assign. For example, example_role. Click Add selected.
Verification
- In a browser, navigate to the application URL.
Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Users with the required role can log in to your application.
9.4. Developing JBoss EAP bootable jar application with OpenID Connect Copy linkLink copied to clipboard!
You can use OpenID Connect (OIDC) to delegate authentication to an external OpenID provider. The elytron-oidc-client
galleon layer provides a native OIDC client in JBoss EAP bootable jar applications to connect with external OpenID providers.
To create an application secured with OpenID Connect using Red Hat Single Sign-On, follow these procedures:
9.4.1. Configuring Red Hat Single Sign-On as an OpenID provider Copy linkLink copied to clipboard!
Red Hat Single Sign-On is an identity and access management provider for securing web applications with single sign-on (SSO). It supports OpenID Connect (an extension to OAuth 2.0).
Prerequisites
- You have installed the Red Hat Single Sign-On server. For more information, see Installing the Red Hat Single Sign-On server in the Red Hat Single Sign-On Getting Started Guide.
- You have created a user in your Red Hat Single Sign-On server instance. For more information, see Creating a user in the Red Hat Single Sign-On Getting Started Guide.
Procedure
Start the Red Hat Single Sign-On server at a port other than 8080 because JBoss EAP default port is 8080.
Syntax
RH_SSO_HOME/bin/standalone.sh -Djboss.socket.binding.port-offset=<offset-number>
$ RH_SSO_HOME/bin/standalone.sh -Djboss.socket.binding.port-offset=<offset-number>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
/home/servers/rh-sso-7.4/bin/standalone.sh -Djboss.socket.binding.port-offset=100
$ /home/servers/rh-sso-7.4/bin/standalone.sh -Djboss.socket.binding.port-offset=100
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Log in to the Admin Console at
http://localhost:<port>/auth/
. For example,http://localhost:8180/auth/
. - To create a realm, in the Admin Console, hover over Master, and click Add realm.
-
Enter a name for the realm. For example,
example_realm
. Ensure that Enabled is ON and click Create. - Click Users, then click Add user to add a user to the realm.
-
Enter a user name. For example,
jane_doe
. Ensure that User Enabled is ON and click Save. - Click Credentials to add a password to the user.
-
Set a password for the user. For example,
janedoep@$$
. Toggle Temporary to OFF and click Set Password. In the confirmation prompt, click Set password. - Click Clients, then click Create to configure a client connection.
-
Enter a client ID. For example,
my_jbeap
. Ensure that Client Protocol is set toopenid-connect
, and click Save. Click Installation, then select
Keycloak OIDC JSON
as the Format Option to see the connection parameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow When configuring your JBoss EAP application to use Red Hat Single Sign-On as the identity provider, you use the parameters as follows:
"provider-url" : "http://localhost:8180/auth/realms/example_realm", "ssl-required": "external", "client-id": "my_jbeap", "public-client": true, "confidential-port": 0
"provider-url" : "http://localhost:8180/auth/realms/example_realm", "ssl-required": "external", "client-id": "my_jbeap", "public-client": true, "confidential-port": 0
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Click Clients, click Edit next to my_jbeap to edit the client settings.
In Valid Redirect URIs, enter the URL where the page should redirect after authentication is successful.
For this example, set this value to
http://localhost:8080/simple-oidc-layer-example/secured/*
9.4.2. Configuring a Maven project for a bootable jar OIDC application Copy linkLink copied to clipboard!
Create a Maven project with the required dependencies and the directory structure for creating a bootable jar application that uses OpenID Connect. The elytron-oidc-client
galleon layer provides a native OpenID Connect (OIDC) client to connect with OpenID providers.
Prerequisites
- You have installed Maven. For more information, see Downloading Apache Maven.
- You have configured your Maven repository for the latest release. For more information, see Maven and the JBoss EAP microprofile Maven repository.
Procedure
Set up a Maven project using the
mvn
command. The command creates the directory structure for the project and thepom.xml
configuration file.Syntax
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the application root directory.
Syntax
cd <name-of-your-application>
$ cd <name-of-your-application>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd simple-oidc-layer-example
$ cd simple-oidc-layer-example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the generated
pom.xml
file as follows:Set the following repositories:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following plugin repositories:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following properties:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following dependencies:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the following build configuration in the
<build>
element of thepom.xml
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- JBoss EAP Maven plug-in to build the application as a bootable JAR
- 2
- The
elytron-oidc-client
layer provides a native OpenID Connect (OIDC) client to connect with external OpenID providers. - 3
- Register the application in the
simple-oidc-layer-example
resource path. The servlet is then available at the URLhttp://server-url/application_name/servlet_path
, for example:http://localhost:8080/simple-oidc-layer-example/secured
. By default, the application WAR file is registered under the root-context path, likehttp://server-url/servlet_path
, for example:http://localhost:8080/secured
.
Set the application name, for example "simple-oidc-layer-example" in the
<build>
element of thepom.xml
file.<finalName>simple-oidc-layer-example</finalName>
<finalName>simple-oidc-layer-example</finalName>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
In the application root directory, enter the following command:
mvn install
$ mvn install
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get an output similar to the following:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can now create a bootable jar application that uses OpenID Connect
9.4.3. Creating a bootable jar application that uses OpenID Connect Copy linkLink copied to clipboard!
The following example demonstrates creating a servlet that prints a logged-in user’s Principal. For an existing application, only those steps that are related to updating the deployment configuration are required.
In this example, the value of the Principal comes from the ID token from the OpenID provider. By default, the Principal is the value of the "sub"
claim from the token. You can specify which claim value from the ID token to use as the Principal in one of the following:
-
The
elytron-oidc-client
subsystem attributeprincipal-attribute
. -
The
oidc.json
file.
<application_root> in the procedure denotes the pom.xml
file directory. The pom.xml
file contains your application’s Maven configuration.
Prerequisites
- You have created a Maven project. For more information, see Configuring Maven project for creating a secure application .
- You have configured Red Hat Single Sign-On as the OpenID provider. For more information, see Configuring Red Hat Single Sign-On as an OpenID provider.
Procedure
Create a directory to store the Java files.
Syntax
mkdir -p <application_root>/src/main/java/com/example/oidc
$ mkdir -p <application_root>/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
mkdir -p simple-oidc-layer-example/src/main/java/com/example/oidc
$ mkdir -p simple-oidc-layer-example/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the new directory.
Syntax
cd <application_root>/src/main/java/com/example/oidc
$ cd <application_root>/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd simple-oidc-layer-example/src/main/java/com/example/oidc
$ cd simple-oidc-layer-example/src/main/java/com/example/oidc
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a servlet "SecuredServlet.java" with the following content:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add security rules for access to your application in the deployment descriptor
web.xml
file located in theWEB-INF
directory of the application.Copy to Clipboard Copied! Toggle word wrap Toggle overflow To secure the application with OpenID Connect, either update the deployment configuration or configure the
elytron-oidc-client
subsystem.NoteIf you configure OpenID Connect in both the deployment configuration and the
elytron-oidc-client
subsystem, the configuration in theelytron-oidc-client
subsystemsecure-deployment
attribute takes precedence over the configuration in the application deployment descriptor.Updating the deployment configuration:
Create a file
oidc.json
in theWEB-INF
directory, like this:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Update the deployment descriptor
web.xml
file with the following text to declare that this application uses OIDC:<login-config> <auth-method>OIDC</auth-method> </login-config>
<login-config> <auth-method>OIDC</auth-method> </login-config>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Configuring the
elytron-oidc-client
subsystem:Create a directory to store a CLI script in the application root directory:
Syntax
mkdir <application_root>/<cli_script_directory>
$ mkdir <application_root>/<cli_script_directory>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
mkdir simple-oidc-layer-example/scripts/
$ mkdir simple-oidc-layer-example/scripts/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can create the directory at any place that Maven can access, inside the application root directory.
Create a CLI script, such as
configure-oidc.cli
, with the following content:/subsystem=elytron-oidc-client/secure-deployment=simple-oidc-layer-example.war:add(client-id=my_jbeap,provider-url=http://localhost:8180/auth/realms/example_realm,public-client=true,ssl-required=external)
/subsystem=elytron-oidc-client/secure-deployment=simple-oidc-layer-example.war:add(client-id=my_jbeap,provider-url=http://localhost:8180/auth/realms/example_realm,public-client=true,ssl-required=external)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The subsystem command defines the
simple-oidc-layer-example.war
resource as the deployment to secure inelytron-oidc-client
subsystem.In the project
pom.xml
file, add the following configuration extract to the existing plug-in<configuration>
element:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
In the application root directory, compile your application with the following command:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the bootable jar application using the following command:
Syntax
java -jar <application_root>/target/simple-oidc-layer-example-bootable.jar
$ java -jar <application_root>/target/simple-oidc-layer-example-bootable.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
java -jar simple-oidc-layer-example/target/simple-oidc-layer-example-bootable.jar
$ java -jar simple-oidc-layer-example/target/simple-oidc-layer-example-bootable.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This starts JBoss EAP and deploys the application.
Verification
-
In a browser, navigate to
http://localhost:8080/simple-oidc-layer-example/secured
. Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can now log in to the application using the credentials you configured in the Red Hat Single Sign-On as the OpenID provider.
9.4.4. Restricting access based on user roles in bootable jar OIDC applications Copy linkLink copied to clipboard!
You can restrict access to all, or parts, of your application based on user roles. For example, you can let users with the "public" role have access to the parts of your application that aren’t sensitive, and give users with the "admin" role access to those parts that are.
Prerequisites
- You have secured your application using OpenID Connect. For more information, see Creating a bootable jar application that uses OpenID Connect.
Procedure
Update the deployment descriptor
web.xml
file with the following text:Syntax
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Allow only those users with the role
example_role
to access your application.
In the application root directory, recompile your application with the following command:
mvn package
$ mvn package
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the application.
java -jar simple-oidc-layer-example/target/simple-oidc-layer-example-bootable.jar
$ java -jar simple-oidc-layer-example/target/simple-oidc-layer-example-bootable.jar
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This starts JBoss EAP and deploys the application.
Verification
-
In a browser, navigate to
\localhost:8080/simple-oidc-layer-example/secured
. Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Forbidden
Forbidden
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Because you have not assigned the required role to the user "jane_doe," jane_doe can’t log in to your application. Only the users with the required role can log in.
To assign users the required roles, see Creating and assigning roles to users in Red Hat Single Sign-On.
9.4.5. Creating and assigning user roles in Red Hat Single Sign-On Copy linkLink copied to clipboard!
Red Hat Single Sign-On is an identity and access management provider for securing your web applications with single sign-on (SSO). You can define users and assign roles in Red Hat Single Sign-On.
Prerequisites
- You have configured Red Hat Single Sign-On. For more information, see Configuring Red Hat Single Sign-On as an OpenID provider.
Procedure
-
Log in to the admin console at
http://localhost:<port>/auth/
. For example,http://localhost:8180/auth/
. - Click the realm you use to connect with JBoss EAP. For example, example_realm.
-
Click Clients, then click the
client-name
you configured for JBoss EAP. For example, my_jbeap. - Click Roles, then Add Role.
- Enter a role name, such as example_role, then click Save. This is the role name you configure in JBoss EAP for authorization.
- Click Users, then View all users.
- Click an ID to assign the role you created. For example, click the ID for jane_doe.
-
Click Role Mappings. In the Client Roles field, select the
client-name
you configured for JBoss EAP. For example, my_jbeap. - In Available Roles, select a role to assign. For example, example_role. Click Add selected.
Verification
- In a browser, navigate to the application URL.
Log in with your credentials. For example:
username: jane_doe password: janedoep@$$
username: jane_doe password: janedoep@$$
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the following output:
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Secured Servlet Current Principal '5cb0c4ca-0477-44c3-bdef-04db04d7e39d'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Users with the required role can log in to your application.