Note
|
This repository contains the guide documentation source. To view the guide in published form, view it on the Open Liberty website. |
Learn how to secure a web application through authentication and authorization.
You’ll learn how to secure a web application by performing authentication and authorization using Jakarta EE Security. Authentication confirms the identity of the user by verifying a user’s credentials while authorization determines whether a user has access to restricted resources.
Jakarta EE Security provides capability to configure the basic authentication, form authentication, or custom form authentication mechanism by using annotations in servlets. It also provides the SecurityContext API for programmatic security checks in application code.
You’ll implement form authentication for a simple web front end. You’ll also learn to specify security constraints for a servlet and use the SecurityContext API to determine the role of a logged-in user.
The finished application is secured with form authentication.
Navigate your browser to this URL to access the application: https://localhost:9080
The application automatically switches from an HTTP connection to a secure HTTPS connection and forwards you to a login page. If the browser gives you a certificate warning, it’s because the Open Liberty instance created a self-signed SSL certificate by default. You can follow your browser’s provided instructions to accept the certificate and continue.
Sign in to the application with one of the following user credentials from the user registry, which are provided to you:
Username |
Password |
Role |
Group |
alice |
alicepwd |
user |
Employee |
bob |
bobpwd |
admin, user |
Manager, Employee |
carl |
carlpwd |
admin, user |
TeamLead, Employee |
dave |
davepwd |
N/A |
PartTime |
Notice that when you sign in as Bob or Carl, the browser redirects to the admin
page and you can view their names and roles. When you sign in as Alice, you can only view Alice’s name. When you sign in as Dave, you are blocked and see an Error 403: Authorization failed
message because Dave doesn’t have a role that is supported by the application.
For this application, users are asked to log in with a form when they access the application. Users are authenticated and depending on their roles, they are redirected to the pages that they are authorized to access. If authentication or authorization fails, users are sent to an error page. The application supports two roles, admin
and user
.
Navigate to the start
directory to begin.
Create theHomeServlet
class.src/main/java/io/openliberty/guides/ui/HomeServlet.java
HomeServlet.java
link:finish/src/main/java/io/openliberty/guides/ui/HomeServlet.java[role=include]
The HomeServlet
servlet is the entry point of the application. To enable form authentication for the HomeServlet
class, define the @FormAuthenticationMechanismDefinition
annotation and set its loginToContinue
attribute with a @LoginToContinue
annotation. This @FormAuthenticationMechanismDefinition
annotation defines welcome.html
as the login page and error.html
as the error page.
The welcome.html
page implements the login form, and the error.html
page implements the error page. Both pages are provided for you under the src/main/webapp
directory. The login form in the welcome.html
page uses the j_security_check
action, which is defined by Jakarta EE and available by default.
Authorization determines whether a user can access a resource. To restrict access to authenticated users with user
and admin
roles, define the @ServletSecurity
annotation with the @HttpConstraint
annotation and set the rolesAllowed
attribute to these two roles.
The transportGuarantee
attribute defines the constraint on the traffic between the client and the application. Set it to CONFIDENTIAL
to enforce that all user data must be encrypted, which is why an HTTP connection from a browser switches to HTTPS.
The SecurityContext interface provides programmatic access to the Jakarta EE Security API. Inject a SecurityContext instance into the HomeServlet
class. The doGet()
method uses the isCallerInRole()
method from the SecurityContext API to check a user’s role and then forwards the response to the appropriate page.
The src/main/webapp/WEB-INF/web.xml
file contains the rest of the security declaration for the application.
web.xml
link:finish/src/main/webapp/WEB-INF/web.xml[role=include]
The security-role
elements define the roles that are supported by the application, which are user
and admin
. The security-constraint
elements specify that JSF resources like the user.jsf
and admin.jsf
pages can be accessed only by users with user
and admin
roles.
User registries store user account information, such as username and password, for use by applications to perform security-related operations. Typically, Open Liberty would be configured to use an external registry like a Lightweight Directory Access Protocol (LDAP) registry. Applications would access information in the registry for authentication and authorization by using APIs like the Jakarta EE Security API.
Open Liberty provides an easy-to-use basic user registry for developers, which you will configure.
Create theuserRegistry
configuration file.src/main/liberty/config/userRegistry.xml
userRegistry.xml
link:finish/src/main/liberty/config/userRegistry.xml[role=include]
The registry has four users, bob
, alice
, carl
, and dave
. It also has four groups: Manager
, TeamLead
, Employee
, and PartTime
. Each user belongs to one or more groups.
It is not recommended to store passwords in plain text. The passwords in the userRegistry.xml
file are encoded by using the Liberty securityUtility
command with XOR encoding.
server.xml
link:finish/src/main/liberty/config/server.xml[role=include]
Use the include
element to add the basic user registry configuration to your Liberty configuration. Open Liberty includes configuration information from the specified XML file in its configuration.
The server.xml
configuration file contains the security configuration of the Liberty under the application-bnd
element. Use the security-role
and group
elements to map the groups in the userRegistry.xml
file to the appropriate user roles supported by the application for proper user authorization. The Manager
and TeamLead
groups are mapped to the admin
role while the Employee
group is mapped to the user
role.
HomeServlet.java
link:finish/src/main/java/io/openliberty/guides/ui/HomeServlet.java[role=include]
Point your browser to the https://localhost:9080 URL.
As you can see, the browser gets automatically redirected from an HTTP connection to an HTTPS connection because the transport guarantee is defined in the HomeServlet
class.
You will see a login form because form authentication is implemented and configured. Sign in to the application by using one of the credentials from the following table. The credentials are defined in the configured user registry.
Username |
Password |
Role |
Group |
alice |
alicepwd |
user |
Employee |
bob |
bobpwd |
admin, user |
Manager, Employee |
carl |
carlpwd |
admin, user |
TeamLead, Employee |
dave |
davepwd |
N/A |
PartTime |
Notice that when you sign in as Bob or Carl, the browser redirects to the admin
page and you can view their names and roles. When you sign in as Alice, you can only view Alice’s name. When you sign in as Dave, you are blocked and see an Error 403: Authorization failed
message because Dave doesn’t have a role that is supported by the application.
Write the SecurityIT
class to test the authentication and authorization of the application.
Create theSecurityIT
class.src/test/java/it/io/openliberty/guides/security/SecurityIT.java
SecurityIT.java
link:finish/src/test/java/it/io/openliberty/guides/security/SecurityIT.java[role=include]
The testAuthenticationFail()
method tests an invalid user authentication while the testAuthorizationFail()
method tests unauthorized access to the application.
The testAuthorizationForAdmin()
and testAuthorizationForUser()
methods verify that users with admin
or user
roles are properly authenticated and can access authorized resource.
You see the following output:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running it.io.openliberty.guides.security.SecurityIT
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.78 sec - in it.io.openliberty.guides.security.SecurityIT
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
You learned how to use Jakarta EE Security in Open Liberty to authenticate and authorize users to secure your web application.
Next, you can try the related MicroProfile JWT guide. It demonstrates technologies to secure backend services.