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

  • Hello,

    I am using Wildfly 14 which is Java EE 8 compliant.

    This project is not working, appears “Forbidden” instead of the login page.

    Any clues, please ?

  • Good evening!
    I’ve had this issue as well, and I believe it’s failing to inject FacesContext and ExternalContext. As soon as I switched to:
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    it’s working perfectly.

    Come to think of it, I’ve not been able to inject the FacesContext like the book suggests, I always revert to the variable declaration. I’ve had this with Payara and Wildfly as well, don’t know what’s causing it

  • For the login part. For some reason the @Inject FacesContext doesn’t work and I have to do the getCurrentInstance() way.

    Has happened with a couple of Payara versions and with a couple of Wildfly ones as well. Very strange.

    In fact, I can’t run your code. After I click submit, nothing happens. I’ll upload my version and if you have time maybe take a look at it over the weekend. It’s essentially the same as yours.

  • I don’t have any issues with injection of FacesContext or ExternalContent, but for some reason it seems the authentication status that is returned sometimes is SEND_CONTINUE and other times is SUCCESS. When SUCCESS is returned then the result is the welcome page… but when the status is SEND_CONTINUE then nothing happens and the result is the login page with no error message.

    • Add the following code [ .newAuthentication(true) ] will fix the problem with redirect on SEND_CONTINUE as explained in the book “The Definitive Guide to JSF in Java EE 8 Building Web Applications with JavaServer Faces” on page 428. Nor did I manage to figure out exactly what happened there but it works.

      Full method code:

      private AuthenticationStatus continueAuthentication() {
      return securityContext.authenticate(
      (HttpServletRequest) externalContext.getRequest(),
      (HttpServletResponse) externalContext.getResponse(),
      AuthenticationParameters.withParams()
      .newAuthentication(true)
      .credential(new UsernamePasswordCredential(username, password))
      );
      }

  • Thank you for that example, good work.

    Can you explain briefly how to integrate this application with the postgres database as DatabaseIdentityStoreDefinition ?

    • Thank you! To integrate this with a database as the identity store, you can remove the shown CustomInMemoryIdentityStore and add

      @DatabaseIdentityStoreDefinition(
      dataSourceLookup = "${'java:global/yourDatasource'}",
      callerQuery = "#{'select password from caller where name = ?'}",
      groupsQuery = "select group_name from caller_groups where caller_name = ?",
      priority = 10
      )

      • If we do not add there hashAlgorithm passwords in database should be stored as planetext ?

        If we add, for example “hashAlgorithm = PasswordHash.class” I suppose that passwords in databnase should be encrypted but in that case we have to implements methods of PasswordHash interface ?


        • @Retention(RUNTIME)
          @Target(TYPE)
          public @interface DatabaseIdentityStoreDefinition {
          String dataSourceLookup() default "java:comp/DefaultDataSource";
          Class<? extends PasswordHash> hashAlgorithm()
          default Pbkdf2PasswordHash.class;
          // ... and some more
          }

          The default hash algorithm, Pbkdf2PasswordHash, is an interface denoting a standard, built-in
          PasswordHash. All implementations of this specification MUST provide an implementation of the
          Pbkdf2PasswordHash interface, with configuration and behavior as described by the interface’s
          javadoc.

          This is from the Java EE Security API specification document you can find here (page 36).

  • Really thanks for this article! Very good written and easy to follow. I haven’t any problem with running it on my computer. I want to have a little contribution to it. I made a two user authentication where each one of them have access to some pages and I struggled on each page with redirection to the login page – if You have this problem use two asteriks instead of one in url-pattern e.g /adm/**
    regards!

    • what do you mean with hatch? You can for example query a database within the CustomInMemoryIdentityStore to make it more dynamic and just add new customers to this database table.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >