Chapter 2. Building and running JBoss EAP applications on OpenShift Container Platform
You can follow the source-to-image (S2I) process to build and run a Java application on the JBoss EAP for OpenShift image.
2.1. Prerequisites Copy linkLink copied to clipboard!
- You have an OpenShift instance installed and operational.
2.2. Preparing OpenShift to deploy an application Copy linkLink copied to clipboard!
As a JBoss EAP application developer, you can deploy your applications on OpenShift. In the following example, note that the kitchensink
quickstart demonstrates a Jakarta EE web-enabled database application using Jakarta Server Faces, Jakarta Contexts and Dependency Injection, Jakarta Enterprise Beans, Jakarta Persistence, and Jakarta Bean Validation. See the JBoss EAP 8.1 kitchensink
quickstart for more information. Deploy your application by following the procedures below.
Procedure
-
Log in to your OpenShift instance using the
oc login
command. Create a project in OpenShift.
Create a project using the following command. With a project, you can organize and manage content separately from other groups.
oc new-project <project_name>
$ oc new-project <project_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, for the
kitchensink
quickstart, create a project namedeap-demo
using the following command:oc new-project eap-demo
$ oc new-project eap-demo
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Create a keystore and a secret.
NoteYou must create a keystore and a secret if you use any HTTPS-enabled features in your OpenShift project.
Use the Java
keytool
command to generate a keystore:WarningThe following commands generate a self-signed certificate, but for production environments, use your own SSL certificate from a verified certificate authority (CA) for SSL-encrypted connections (HTTPS).
keytool -genkey -keyalg RSA -alias <alias_name> -keystore <keystore_filename.jks> -validity 360 -keysize 2048
$ keytool -genkey -keyalg RSA -alias <alias_name> -keystore <keystore_filename.jks> -validity 360 -keysize 2048
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, for the
kitchensink
quickstart, use the following command to generate a keystore:keytool -genkey -keyalg RSA -alias eapdemo-selfsigned -keystore keystore.jks -validity 360 -keysize 2048
$ keytool -genkey -keyalg RSA -alias eapdemo-selfsigned -keystore keystore.jks -validity 360 -keysize 2048
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to create a secret from your new keystore:
oc create secret generic <secret_name> --from-file=<keystore_filename.jks>
$ oc create secret generic <secret_name> --from-file=<keystore_filename.jks>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, for the
kitchensink
quickstart, use the following command to create a secret:oc create secret generic eap-app-secret --from-file=keystore.jks
$ oc create secret generic eap-app-secret --from-file=keystore.jks
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.3. Building application images using source-to-image in OpenShift Copy linkLink copied to clipboard!
Follow the source-to-image (S2I) workflow to build reproducible container images for a JBoss EAP application. These generated container images include the application deployment and ready-to-run JBoss EAP servers.
The S2I workflow takes source code from a Git repository and injects it into a container that’s based on the language and framework you want to use. After the S2I workflow is completed, the src
code is compiled, the application is packaged and is deployed to the JBoss EAP server.
For more information, see Legacy server provisioning for JBoss EAP S2I.
In JBoss EAP, you can use S2I images only if you develop your application using Jakarta EE 10.
Prerequisites
- You have an active Red Hat customer account.
- You have a Registry Service Account. Follow the instructions on the Red Hat Customer Portal to create an authentication token using a registry service account.
- You have downloaded the OpenShift secret YAML file, which you can use to pull images from Red Hat Ecosystem Catalog. For more information, see OpenShift Secret.
-
You used the
oc login
command to log in to OpenShift. - You have installed Helm. For more information, see Installing Helm.
You have installed the repository for the JBoss EAP Helm charts by entering this command in the management CLI:
helm repo add jboss-eap https://jbossas.github.io/eap-charts/
$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
Create a file named
helm.yaml
using the following YAML content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to deploy your JBoss EAP application on OpenShift.
helm install helloworld -f helm.yaml jboss-eap/eap8
$ helm install helloworld -f helm.yaml jboss-eap/eap8
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Access the application using
curl
.curl https://$(oc get route helloworld --template='{{ .spec.host }}')/HelloWorld
$ curl https://$(oc get route helloworld --template='{{ .spec.host }}')/HelloWorld
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You get the output
Hello World!
confirming that the application is deployed.
2.4. Deploying a third-party application on OpenShift Copy linkLink copied to clipboard!
You can create application images for OpenShift deployments by using compiled WAR files or EAR archives. Use a Dockerfile to deploy these archives onto JBoss EAP server, along with an updated and comprehensive runtime stack that includes the operating system, Java, and JBoss EAP components.
Red Hat do not provide pre-built JBoss EAP server images.
2.4.1. Provisioning JBoss EAP servers with the default configuration Copy linkLink copied to clipboard!
You can install and configure a JBoss EAP server with its default configuration on OpenShift by using the builder image. For seamless deployment, follow the procedure to provision the server, transfer the application files, and make any necessary customization.
Prerequisites
You have access to the supported Red Hat JBoss Enterprise Application Platform container images. For example:
-
registry.redhat.io/jboss-eap-8/eap81-openjdk21-builder-openshift-rhel9
-
registry.redhat.io/jboss-eap-8/eap81-openjdk21-runtime-openshift-rhel9
-
- You have podman installed on your system. Use the latest podman version available on supported RHEL. For more information, see Red Hat JBoss Enterprise Application Platform 8.1 Supported Configurations.
Procedure
Copy the following Dockerfile contents as provided:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- You can specify the
MAVEN_MIRROR_URL
environment variable, which is used by the JBoss EAP Maven plugin internally within the image. For more information, see Artifact repository mirrors. - 2
- You do not need to update this Dockerfile for any of the minor releases. Specify the JBoss EAP version in the
GALLEON_PROVISION_CHANNELS
environment variable if you want to use a specific version. For more information, see Environment variables. - 3
- Modify the copied Dockerfile to include your WAR file in the container. For example:
COPY --chown=jboss:root <my-app.war> $JBOSS_HOME/standalone/deployments
COPY --chown=jboss:root <my-app.war> $JBOSS_HOME/standalone/deployments
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace <myapp.war> with the path to the Web archive you want to add to the image.
Build the application image using podman:
podman build -t my-app .
$ podman build -t my-app .
Copy to Clipboard Copied! Toggle word wrap Toggle overflow After the command is executed, the
my-app
container image is ready to be deployed on OpenShift.Upload your container image to one of the following options:
- Your internal registry that is accessible from OpenShift.
- The OpenShift registry by pushing the image directly from the machine where it was built. For more information, see How to push a container image into the image registry in RHOCP 4.
- When deploying your image from the registry, use deployment strategies such as Helm charts, Operator, or Deployment. Select your preferred method and use either the full image URL or ImageStreams based on your requirements. For more information, see Using Helm charts to build and deploy JBoss EAP applications on OpenShift.
2.5. Using OpenID Connect to secure JBoss EAP applications on OpenShift Copy linkLink copied to clipboard!
Use the JBoss EAP native OpenID Connect (OIDC) client to delegate authentication using an external OpenID provider. OIDC is an identity layer that enables clients, such as JBoss EAP, to verify a user’s identity based on the authentication performed by an OpenID provider.
The elytron-oidc-client
subsystem and elytron-oidc-client
Galleon layer provides a native 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.
You can configure the elytron-oidc-client
subsystem in three different ways:
-
Adding an
oidc.json
into your deployment. -
Running a CLI script to configure the
elytron-oidc-client
subsystem. -
Defining environment variables to configure an
elytron-oidc-client
subsystem on start of JBoss EAP server on OpenShift.
This procedure explains how you can configure an elytron-oidc-client
subsystem using the environment variables to secure application with OIDC.
2.5.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.
Use the OIDC client with Red Hat build of Keycloak. 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.
2.5.2. Creating an application secured with OpenID Connect Copy linkLink copied to clipboard!
For creating a web-application, create a Maven project with the required dependencies and the directory structure. Create a web application containing a servlet that returns the user name obtained from the logged-in user’s principal and attributes. If there is no logged-in user, the servlet returns the text "NO AUTHENTICATED USER".
Prerequisites
- You have installed Maven. For more information, see Downloading Apache Maven.
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-webapp-example
$ cd simple-webapp-example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace the content of the generated
pom.xml
file with the following text:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note-
<version.eap.plugin>2.0.0.Final-redhat-00009</version.eap.plugin>
is an example version of JBoss EAP Maven plugin. See the Red Hat Maven repository for more information on JBoss EAP Maven plugin releases: https://maven.repository.redhat.com/earlyaccess/all/org/jboss/eap/plugins/eap-maven-plugin/.
-
Create a directory to store the Java files.
Syntax
mkdir -p src/main/java/<path_based_on_artifactID>
$ mkdir -p src/main/java/<path_based_on_artifactID>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
mkdir -p src/main/java/com/example/app
$ mkdir -p src/main/java/com/example/app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the new directory.
Syntax
cd src/main/java/<path_based_on_artifactID>
$ cd src/main/java/<path_based_on_artifactID>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd src/main/java/com/example/app
$ cd src/main/java/com/example/app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file
SecuredServlet.java
with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the application’s
web.xml
to protect the application resources.Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this example, only the users with the role
Users
can access the application.
2.5.3. Deploying the application on OpenShift Copy linkLink copied to clipboard!
As a JBoss EAP application developer, you can deploy your applications on OpenShift that uses the OpenID Connect subsystem and integrate it with a Red Hat build of Keycloak server. Deploy your application by following the procedures below.
Prerequisites
You have configured the Red Hat build of Keycloak server in your OpenShift with the following configuration. For more information, see Red Hat build of Keycloak Operator.
- Create a realm called JBossEAP.
- Create a user called demo.
- Set a password for the user called demo. Toggle Temporary to OFF and click Set Password. In the confirmation prompt, click Set password.
- Create a role called Users.
- Assign the role Users to the user demo.
- In the Client Roles field, select the realm-management you configured for JBoss EAP.
- Assign the role create-client to the client realm-management.
Procedure
- Deploy your application code to Git Repository.
Create a secret containing the OIDC configuration.
Create a file named
oidc-secret.yaml
using the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to create a secret:
oc apply -f oidc-secret.yaml
$ oc apply -f oidc-secret.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a file named
helm.yaml
using the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the example application using JBoss EAP Helm charts:
helm install eap-oidc-test-app -f helm.yaml jboss-eap/eap8
$ helm install eap-oidc-test-app -f helm.yaml jboss-eap/eap8
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the environment variables to the
oidc-secret.yaml
file to configure the OIDC provider URL and application hostname.yaml stringData: ... OIDC_HOSTNAME_HTTPS: <host of the application> OIDC_PROVIDER_URL: https://<host of the SSO provider>/realms/JBossEAP
yaml stringData: ... OIDC_HOSTNAME_HTTPS: <host of the application> OIDC_PROVIDER_URL: https://<host of the SSO provider>/realms/JBossEAP
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The value for
OIDC_HOSTNAME_HTTPS
corresponds to the following output:echo $(oc get route eap-oidc-test-app --template='{{ .spec.host }}')
echo $(oc get route eap-oidc-test-app --template='{{ .spec.host }}')
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The value for
OIDC_PROVIDER_URL
corresponds to the following output:echo https://$(oc get route sso --template='{{ .spec.host }}')/realms/JBossEAP
echo https://$(oc get route sso --template='{{ .spec.host }}')/realms/JBossEAP
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A route discovery attempt is made if
OIDC_HOSTNAME_HTTP(S)
is not set. To enable route discovery, the OpenShift user must be able to list theroute
resources. For example, to create and associate therouteview
role with theview
user, use the followingoc
command:oc create role <role-name> --verb=list --resource=route oc adm policy add-role-to-user <role-name> <user-name> --role-namespace=<your namespace>
$ oc create role <role-name> --verb=list --resource=route $ oc adm policy add-role-to-user <role-name> <user-name> --role-namespace=<your namespace>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Update the secret with
oc apply -f oidc-secret.yaml
. Deploy the application again to ensure OpenShift uses the new environment variables:
oc rollout restart deploy eap-oidc-test-app
$ oc rollout restart deploy eap-oidc-test-app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
In your browser, navigate to
https://<eap-oidc-test-app route>/
.You will be redirected to Red Hat build of Keycloak login page.
- Access the secured servlet.
Log in with the following credentials:
username: demo password: demo
username: demo password: demo
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A page appears that contains the Principal ID.
2.5.4. Environment variable based configuration Copy linkLink copied to clipboard!
Use these environment variables to configure JBoss EAP OIDC support on OpenShift image.
Environment variable | Legacy SSO environment variable | Description | Required | Default Value |
---|---|---|---|---|
OIDC_PROVIDER_NAME |
NONE. When |
You must set to | Yes | |
OIDC_PROVIDER_URL |
| The URL of the provider. | Yes | |
OIDC_USER_NAME | SSO_USERNAME | Dynamic client registration requires the username to receive a token. | Yes | |
OIDC_USER_PASSWORD | SSO_PASSWORD | Dynamic client registration requires the user password to receive a token. | Yes | |
OIDC_SECURE_DEPLOYMENT_SECRET | SSO_SECRET | It is known to both the secure-deployment subsystem and the authentication server client. | No | |
OIDC_SECURE_DEPLOYMENT_PRINCIPAL_ATTRIBUTE | SSO_PRINCIPAL_ATTRIBUTE | Configure the value of the principal name. | No |
Defaults to Typical value: preferred_username. |
OIDC_SECURE_DEPLOYMENT_ENABLE_CORS | SSO_ENABLE_CORS | Enable CORS for Single Sign-On applications. | No |
Defaults to |
OIDC_SECURE_DEPLOYMENT_BEARER_ONLY | SSO_BEARER_ONLY | Deployment that accepts only bearer token and does not support logging. | No |
Defaults to |
OIDC_PROVIDER_SSL_REQUIRED | NONE | Defaults to external, such as private and local address, but does not support https. | No | External |
OIDC_PROVIDER_TRUSTSTORE | SSO_TRUSTSTORE |
Specify the realm | No | |
OIDC_PROVIDER_TRUSTSTORE_DIR | SSO_TRUSTSTORE_DIR |
Directory to find the realm | No | |
OIDC_PROVIDER_TRUSTSTORE_PASSWORD | SSO_TRUSTSTORE_PASSWORD |
Specify the realm | No | |
OIDC_PROVIDER_TRUSTSTORE_CERTIFICATE_ALIAS | SSO_TRUSTSTORE_CERTIFICATE_ALIAS |
Specify the realm | No | |
OIDC_DISABLE_SSL_CERTIFICATE_VALIDATION | SSO_DISABLE_SSL_CERTIFICATE_VALIDATION | Disable certificate validation when interacting with the authentication server to register a client. | No | |
OIDC_HOSTNAME_HTTP | HOSTNAME_HTTP | Hostname used for unsecure routes. | No | Routes are discovered. |
OIDC_HOSTNAME_HTTPS | HOSTNAME_HTTPS | Hostname used for secured routes. | No | Secured routes are discovered. |
NONE | SSO_PUBLIC_KEY | Public key of the Single Sign-On realm. This option is not used, public key is automatically retrieved by the OIDC subsystem. | No | If set, a warning is displayed that this option is being ignored. |
2.6. Securing applications by using SAML Copy linkLink copied to clipboard!
The Security Assertion Markup Language (SAML) serves as a data format and protocol that enables the exchange of authentication and authorization information between two parties. These two parties typically include an identity provider and a service provider. This information takes the form of SAML tokens containing assertions. Identity providers issue these SAML tokens to subjects to enable these subjects to authenticate with service providers. Subjects can reuse SAML tokens with multiple service providers, which enables browser-based Single Sign-On in SAML v2.
You can secure web applications by using the Galleon layers that the Keycloak SAML adapter feature pack provides.
For information about the Keycloak SAML adapter feature pack, see Keycloak SAML adapter feature pack for securing applications by using SAML.
2.6.1. Keycloak SAML adapter feature pack for securing applications by using SAML Copy linkLink copied to clipboard!
Keycloak SAML adapter Galleon pack is a Galleon feature pack that includes the keycloak-saml
layer. Use the keycloak-saml
layer in the feature pack to install the necessary modules and configurations in JBoss EAP. These modules and configurations are required if you want to use Red Hat build of Keycloak as an identity provider for Single Sign-On (SSO) when using SAML. When using the keycloak-saml
SAML adapter Galleon layer for source-to-image (S2I), you can optionally use the SAML client feature that enables automatic registration with an Identity Service Provider (IDP), such as Red Hat build of Keycloak.
2.6.2. Configuring Red Hat build of Keycloak as SAML provider for OpenShift Copy linkLink copied to clipboard!
Red Hat build of Keycloak is an identity and access management provider for securing web applications with Single Sign-On (SSO). It supports OpenID Connect, which is an extension to OAuth 2.0, and SAML.
The following procedure outlines the essential steps needed to secure applications with SAML. For more information, see Red Hat build of Keycloak documentation.
Prerequisites
- You have administrator access to Red Hat build of Keycloak.
- Red Hat build of Keycloak is running. For more information, see Red Hat build of Keycloak Operator.
-
You used the
oc
login command to log in to OpenShift.
Procedure
- Create a Single Sign-On realm, users, and roles.
Generate the key and certificate by using the Java
keytool
command:keytool -genkeypair -alias saml-app -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -storepass password -dname "CN=saml-basic-auth,OU=EAP SAML Client,O=Red Hat EAP QE,L=MB,S=Milan,C=IT" -ext ku:c=dig,keyEncipherment -validity 365
keytool -genkeypair -alias saml-app -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -storepass password -dname "CN=saml-basic-auth,OU=EAP SAML Client,O=Red Hat EAP QE,L=MB,S=Milan,C=IT" -ext ku:c=dig,keyEncipherment -validity 365
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Import the keystore into a Java KeyStore (JKS) format:
keytool -importkeystore -deststorepass password -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass password
keytool -importkeystore -deststorepass password -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass password
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a secret in OpenShift for the keystore:
oc create secret generic saml-app-secret --from-file=keystore.jks=./keystore.jks --type=opaque
$ oc create secret generic saml-app-secret --from-file=keystore.jks=./keystore.jks --type=opaque
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThese steps are only necessary when using the automatic SAML client registration feature. When JBoss EAP registers a new SAML client into Red Hat build of Keycloak as the
client-admin
user, JBoss EAP must store the certificate of the new SAML client in the Red Hat build of Keycloak client configuration. This allows JBoss EAP to retain the private key while only storing the public certificate in Red Hat build of Keycloak, which establishes an authenticated client for communication with Red Hat build of Keycloak.
2.6.3. Creating an application secured with SAML Copy linkLink copied to clipboard!
You can enhance web application security by using the Security Assertion Markup Language (SAML). SAML provides effective user authentication and authorization, along with Single Sign-On (SSO) capabilities, making it a dependable choice for strengthening web applications.
Prerequisites
- You have installed Maven. For more information, see Downloading Apache Maven.
Procedure
Set up a Maven project by using the
mvn
command. This command creates both 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-webapp-example
$ cd simple-webapp-example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace the content of the generated
pom.xml
file with the following text:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note-
<version.eap.plugin>2.0.0.Final-redhat-00009</version.eap.plugin>
is an example version of JBoss EAP Maven plugin. See the Red Hat Maven repository for more information on JBoss EAP Maven plugin releases: https://maven.repository.redhat.com/earlyaccess/all/org/jboss/eap/plugins/eap-maven-plugin/.
-
Create a directory to store the Java files.
Syntax
mkdir -p src/main/java/<path_based_on_artifactID>
$ mkdir -p src/main/java/<path_based_on_artifactID>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
mkdir -p src/main/java/com/example/app
$ mkdir -p src/main/java/com/example/app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the new directory.
Syntax
cd src/main/java/<path_based_on_artifactID>
$ cd src/main/java/<path_based_on_artifactID>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example
cd src/main/java/com/example/app
$ cd src/main/java/com/example/app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file named
SecuredServlet.java
that contains the following settings:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the directory structure for the
web.xml
file:mkdir -p src/main/webapp/WEB-INF cd src/main/webapp/WEB-INF
mkdir -p src/main/webapp/WEB-INF cd src/main/webapp/WEB-INF
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the application’s
web.xml
file to protect the application resources.Example
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In this example, only users with the
user
role can access the application.
Verification
After creating the application, commit it to a remote Git repository.
-
Create a Git repository such as
https://github.com/your-username/simple-webapp-example
. For more information about remote repositories and Git, see Getting started with Git - About remote repositories. From the root folder of the application, run the following Git commands:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
These steps commit your application to the remote repository, making it accessible online.
2.6.4. Building and deploying a SAML-secured application on OpenShift Copy linkLink copied to clipboard!
You can build and deploy your application secured with SAML on OpenShift by using the JBoss EAP and Single Sign-On (SSO) Galleon layers.
Prerequisites
- You have installed Helm. For more information, see Installing Helm.
- You have created the SAML application project and made it accessible in a Git repository.
You have installed the repository for the JBoss EAP Helm charts by entering this command in the management CLI:
helm repo add jboss-eap https://jbossas.github.io/eap-charts/
$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Procedure
- Deploy your application code to the Git Repository.
Create an OpenShift secret containing the required environment variables:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Save the provided YAML content to a file, such as
saml-secret.yaml
. Apply the saved YAML file by using the following command:
oc apply -f saml-secret.yaml
oc apply -f saml-secret.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file named
helm.yaml
that contains the following settings:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteSpecify the web address in the HTTP format, such as
http://www.redhat.com
. If you are using a maven mirror, specify the web address as follows:build: uri: [WEB ADDRESS TO YOUR GIT REPOSITORY] env: - name: "MAVEN_MIRROR_URL" value: "http://..."
build: uri: [WEB ADDRESS TO YOUR GIT REPOSITORY] env: - name: "MAVEN_MIRROR_URL" value: "http://..."
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the example application by using JBoss EAP Helm charts:
helm install saml-app -f helm.yaml jboss-eap/eap8
$ helm install saml-app -f helm.yaml jboss-eap/eap8
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the environment variables to the
saml-secret.yaml
file to configure the Keycloak server URL and application route:stringData: ... HOSTNAME_HTTPS: <saml-app application route> SSO_URL: https://<host of the Keycloak server>
stringData: ... HOSTNAME_HTTPS: <saml-app application route> SSO_URL: https://<host of the Keycloak server>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<saml-app application route>
and<host of the Keycloak server>
with the appropriate values.The value for
HOSTNAME_HTTPS
corresponds to the following output:echo $(oc get route saml-app --template='{{ .spec.host }}')
echo $(oc get route saml-app --template='{{ .spec.host }}')
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The value for
SSO_URL
corresponds to the following output:echo https://$(oc get route sso --template='{{ .spec.host }}')
echo https://$(oc get route sso --template='{{ .spec.host }}')
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you cannot use this command, use
oc get routes
to list the available routes and select the route to your Red Hat build of Keycloak instance.-
Update the secret with
oc apply -f saml-secret.yaml
.
Verification
Deploy the application again to ensure that OpenShift uses the new environment variables:
oc rollout restart deploy saml-app
$ oc rollout restart deploy saml-app
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In a browser, navigate to the application URL. For example,
https://<saml-app route>/simple-webapp-example
.You are redirected to the Red Hat build of Keycloak login page.
To get the web address, use the following command to access the secured servlet:
echo https://$(oc get route saml-app --template='{{ .spec.host }}')/simple-webapp-example/secured
echo https://$(oc get route saml-app --template='{{ .spec.host }}')/simple-webapp-example/secured
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log in with the following credentials:
username: demo password: demo
username: demo password: demo
Copy to Clipboard Copied! Toggle word wrap Toggle overflow A page is displayed that contains the Principal ID.
Your application is now secured using SAML.
2.6.5. Creating a SSO realm, users, and roles Copy linkLink copied to clipboard!
You can configure a Single Sign-On (SSO) realm, define user roles, and manage access control in your Red Hat build of Keycloak environment. These actions enable you to enhance security and simplify user access management, ensuring a streamlined authentication experience. This is essential for optimizing your SSO setup and improving user authentication processes.
Prerequisites
- You have administrator access to Red Hat build of Keycloak.
- Red Hat build of Keycloak is running.
Procedure
-
Log in to the Red Hat build of Keycloak admin console using the URL:
https://<SSO route>/
. Create a realm in Red Hat build of Keycloak; for example,
saml-basic-auth
. You can subsequently use this realm to create the required users, roles, and a client.For more information, see Creating a realm.
Create a role within the
saml-basic-auth
realm. For example,user
.For more information, see Creating a realm role.
Create a user. For example,
demo
.For more information, see Creating users.
Create a password for the user. For example,
demo
.Ensure that the password is not temporary. For more information, see Setting a password for a user.
Assign the
user
role to thedemo
user for login access.For more information, see Assigning role mappings.
Create a user. For example,
client-admin
.To create the SAML client in the Keycloak server when the JBoss EAP server starts, you can use the
client-admin
user, which requires additional privileges. For more information, see Creating users.Create a password for the user. For example,
client-admin
.Ensure that the password is not temporary. For more information, see Setting a password for a user.
-
Select
realm-management
from theClient Roles
drop down list. Assign the roles
create-client
,manage-clients
, andmanage-realm
to theclient-admin
user.For more information, see Assigning role mappings.
2.6.6. Environment variables for configuring the SAML subsystem Copy linkLink copied to clipboard!
You can optimize the integration of the Keycloak server within your environment by understanding and using the following variables. This ensures a seamless and secure Keycloak setup for your application.
Environment variable | Description | Required |
---|---|---|
| Used as a prefix for the client name, derived from the deployment name. | Optional |
|
Custom | Optional |
|
Custom | Optional |
|
Choose between | Optional |
|
The password for a user with privileges to interact with the Keycloak realm and to create and register clients. For example, | True |
|
The SSO realm for associating application clients. For example, | Optional |
|
Alias of private key and certificate in the SAML client keystore. For example, | True |
|
Name of the keystore file. For example, | True |
|
Directory that contains the client keystore. For example, | True |
|
Keystore password. For example, | True |
|
Logout page. For example, | True |
|
Specify | Optional |
|
The name of the security domain used to secure undertow and | Optional |
| The truststore file name containing the server certificate. | Optional |
| Certificate alias within the truststore. | Optional |
| Directory that contains the truststore. | Optional |
|
The password for the truststore and certificate . For example, | Optional |
|
The URL for the SSO server. For example, | True |
|
The username of a user with privileges to interact with the Keycloak realm and to create and register clients. For example, | True |
2.6.7. Route discovery in JBoss EAP server Copy linkLink copied to clipboard!
You can optimize your server’s performance and simplify route configurations in your specified namespace by using the route discovery feature in the JBoss EAP server. This feature is essential for improving server efficiency to provide a smoother operational experience, particularly when the HOSTNAME_HTTPS
variable is unspecified.
If the HOSTNAME_HTTPS
variable is not set, the JBoss EAP server automatically attempts route discovery. To enable route discovery, you must create the required permissions:
oc create role routeview --verb=list --resource=route -n YOUR_NAME_SPACE oc policy add-role-to-user routeview system:serviceaccount:YOUR_NAME_SPACE:default --role-namespace=YOUR_NAME_SPACE -n YOUR_NAME_SPACE
oc create role routeview --verb=list --resource=route -n YOUR_NAME_SPACE
oc policy add-role-to-user routeview system:serviceaccount:YOUR_NAME_SPACE:default --role-namespace=YOUR_NAME_SPACE -n YOUR_NAME_SPACE