Use a database security scheme to secure the Example application.
| Resources | |
|---|---|
| Files: |
/home/student/AD248/labs/security-dbrealm
|
| Application URL: | http://127.0.0.1:8080/example |
Outcomes
You should be able to configure an application to verify the username and password by using a database backed security module.
Before beginning the guided exercise, run the following command to prepare the environment:
[student@workstation ~]$ lab start security-dbrealm
Instructions
Start the standalone instance of JBoss EAP by running the following command, which uses /home/student/AD248/labs/security-dbrealm/standalone as the base directory:
[student@workstation ~]$cd /opt/jboss-eap-7.4/bin[student@workstation bin]$./standalone.sh \-Djboss.server.base.dir=/home/student/AD248/labs/security-dbrealm/standalone/
Connect to the security data source.
In this step, you configure the non-XA data source that connects to the database storing the credentials used by the example application. The data source is used later by the security subsystem to fetch the username and the password.
The database is a MariaDB database running on localhost and it is named bksecurity.
The credentials to access the database are bkadmin/redhat123.
Use java:/jboss/datasource/bksecurity-ds as the JNDI name.
On the workstation machine, open the management console by pointing a web browser to localhost:9990.
Use admin as the username, and redhat123 as the password.
Go to the page.
Navigate to the data sources subsystem by clicking and then .
In the third column click .
Then, in the fourth column, click the arrow next to the plus icon, and click .
On the first step of the Add Datasource wizard, select and click .
Use the following parameters to complete the form:
| Field | Value |
|---|---|
Name
|
bksecurity-ds
|
JNDI Name
|
java:/jboss/datasources/bksecurity-ds
|
Click .
On the third step of the Add Datasource wizard, the mariadb driver is already selected.
Click .
Use the following information for the database connection:
| Field | Value |
|---|---|
Connection URL
|
jdbc:mariadb://localhost:3306/bksecurity
|
User Name
|
bkadmin
|
Password
|
redhat123
|
Security Domain
| Leave empty |
Click .
On step 5 of the 'Add Datasource' wizard, click . Then click to review all the data, and close the wizard by clicking , and .
Configure the security domain.
Create a security domain that uses the bksecurity-ds data source.
The security domain must have a default cache type.
Go back to the column.
In the second column, click . Then, click the plus icon button in the column.
Enter bksecurity as the security domain name, and set the to default.
Click .
Start the management CLI in a new terminal and connect to the standalone server.
[student@workstation ~]$cd /opt/jboss-eap-7.4/bin[student@workstation bin]$./jboss-cli.sh --connect
Use the following command to create an authentication login module.
[standalone@localhost:9990]/subsystem=security/security-domain\=bksecurity/authentication\=classic:add
The output indicates that a server reload is required. You can safely ignore that message, because you reload the server in the next steps.
Create a login module that connects to the data source. It should query the table's users and roles to identify the credentials. The password is encrypted with a SHA-256 algorithm and the hash uses a base64 encoding.
Use the following values when creating the login module:
| Field | Value |
|---|---|
Name
|
database
|
Code
|
Database
|
Flag
|
required
|
dsJndiName
|
java:jboss/datasources/bksecurity-ds
|
principalsQuery
|
select password from users where username=?
|
rolesQuery
|
select role, "Roles" from roles where username=?
|
hashAlgorithm
|
SHA-256
|
hashEncoding
|
base64
|
Use the following management CLI command to create the login module:
[standalone@localhost:9990]/subsystem=security/security-domain=bksecurity/\ authentication=classic/login-module=database:add( \code=Database, \flag=required, \module-options=[ \("dsJndiName"=>"java:jboss/datasources/bksecurity-ds"), \("principalsQuery"=>"select password from users where username=?"), \("rolesQuery"=>"select role, 'Roles' from roles where username=?"), \("hashAlgorithm"=>"SHA-256"), \("hashEncoding"=>"base64") \])
Reload the server to allow the changes to take place:
[standalone@localhost:9990] :reloadVerify that your settings are correct by running the following CLI command:
[standalone@localhost:9990]/subsystem=security/security-domain=bksecurity\/authentication=classic/login-module=database:read-resource
The response is similar to the following output:
{
"outcome" => "success",
"result" => {
"code" => "Database",
"flag" => "required",
"module" => undefined,
"module-options" => {
("rolesQuery" => "select role, "Roles" from roles where username=?"),
("principalsQuery" => "select password from users where username=?"),
("dsJndiName" => "java:jboss/datasources/bksecurity-ds"),
("hashAlgorithm" => "SHA-256"),
("hashEncoding" => "base64"),
}
}
}If there is any mistake in the module-options, you can update each module option by using the map-put command in the management CLI.
For example, if there is an error with the dsJndiName value, then the following command can update the module option:
[standalone@localhost:9990]/subsystem=security/security-domain\=bksecurity/authentication=classic/login-module=database\:map-put(name=module-options,key="dsJndiName",\value="java:/jboss/datasources/bksecurity-ds")
Configure the application security.
To secure an application deployed on JBoss EAP, the first step is to modify the application so that it requires credentials to access it.
By using a text editor, open the /home/student/AD248/labs/security-dbrealm/example/src/main/webapp/WEB-INF/web.xml file.
After the <welcome-file-list> section, add the following XML snippet, which defines a security constraint on all URLs to the application and requires a user to be authenticated.
<security-constraint>
<web-resource-collection>
<web-resource-name>All resources</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>*</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>bksecurity</realm-name>
</login-config>Save your changes to the web.xml file.
Configure the security domain in the application.
Using a text editor, open the jboss-web.xml file in the /home/student/AD248/labs/security-dbrealm/example/src/main/webapp/WEB-INF directory.
Within the <jboss-web> tag, enter the following <security-domain> tag:
<security-domain>bksecurity</security-domain>
bksecurity references the security domain that was created previously.
Save your changes to the jboss-web.xml file.
Package the application.
Open a new terminal and enter the following command to create a WAR file of the example application:
[student@workstation ~]$cd /home/student/AD248/labs/security-dbrealm/\example/src/main/webapp[student@workstation webapp]$jar -cvf example.war .
Deploy the application.
Using the management console or management CLI, deploy the example.war file located at /home/student/AD248/labs/security-dbrealm/example/src/main/webapp/ on the standalone server.
[standalone@localhost:9990 /] deploy \
/home/student/AD248/labs/security-dbrealm/example/src/main/webapp/example.warInspect the server log in the terminal console, and verify that there are no errors on the deployment.
Verify the security settings.
Navigate to http://127.0.0.1:8080/example/.
You should be prompted to log in.
Enter admin as the username and admin as the password.
You should see the "Welcome to JBoss EAP 7" page after you are logged in successfully.
If the authentication fails with those credentials, verify that the security domain was set up correctly with the correct values for each module option.
Undeploy the example application from the standalone server by using the management CLI:
[standalone@localhost:9990] undeploy example.warExit the management CLI and stop the running instance of JBoss EAP.