Simple form-based authentication for JSF 2.3 with Java EE 8 Security API

Last Updated:  October 12, 2021 | Published: August 13, 2018

Securing your web application can be cumbersome. I recently tried to secure a JSF 2.3 application with the latest Java EE 8 Security API (JSR-375) and it was quite simple. In this blog post, I'll show you the required configuration steps for securing your JSF application with a form-based authentication mechanism. In the example, I'll use an in-memory user store of two users, add PrimeFaces for enhancing the UI and deploy it to Payara 5.182.

JSF project setup

The pom.xml is straightforward for this application:

The new Security API is part of Java EE 8 and the definitions are mostly located in the javax.security package. The default reference implementation is Soteria and is already part of the most recent application servers (Payara, Wildfly, OpenLiberty, TomEE …). With this new API, we now get a common security workflow definition and with the new interfaces and annotations, we can configure the authentication and authorization of our Java EE applications.

Using the Java EE Security API with an in-memory IndentityStore

One of the central interfaces is the IdentityStore which is responsible for validating the incoming credentials. In this example, I'm using a simple in-memory IdentityStore which has only two valid users:

For a more realistic example, you can use the new @DatabaseIdentityStoreDefinition or @LdapIdentityStoreDefinition annotation to configure a database or a LDAP system for user validation. Our IdentityStore will just check if the incoming mail and password match with one of the two dummy users and return a CredentialValidationResult which contains the principal's name and the associated roles.

The web application has two pages: the login.xhtml file at the root level which is visible for everybody and a secured page at /app/index.html. To secure a path in your JSF application, you have to update your web.xml. In our example, I am securing the path to /app/* to be visible only for authenticated users with the ADMIN or USER role:

To register a custom login page, you have to inform the authentication mechanism about the name and location of your login page. This can be done programmatically with a simple configuration class:

The property useForwardToLogin is set to false to use a redirect instead of a forward.

Creating the JSF view for form authentication

The login.xhtml file is a simple JSF page containing a form with two input fields:

The backing bean for the login page is responsible for validating the incoming credentials and redirects to the secured area if the credentials are valid. To use our custom IdentityStore the backing beans injects the SecurityContext and passes the email and password to our in-memory authentication. With the bean validation annotations we get a pre-validation (e.g. check the password against custom password rules) of the user input out of the box:

Entering valid credentials will redirect the user to the secured index.html which will display the user's name and a button to logout:

The backing bean for the logout is invalidating the session of the user and will redirect to the login page:

For using this on WildFly 14+ (Java EE 8 compliant), you have to add the following jboss-web.xml in src/main/webapp/WEB-INF and reference to the jaspitest security Domain which is defined in the legacy security subsystem:

To try this on your machine, have a look at my GitHub repository for the full codebase and a step-by-step tutorial to get this running on your machine in the README.md of the project. There is quite more to discover with JSF 2.3 and Java EE in general, so stay tuned for the next blog posts. To start with JSF 2.3 I can recommend The Definitive Guide to JSF in Java EE 8 from Bauke Scholtz (@OmniFaces) & Arjan Tijms (@arjan_tijms).

Have fun securing your applications,

Phil

>