JSF Authentication Example
In our previous example we have learned how to create a sample JSF-HelloWorld application. This article will focus on creating a simple login application using the authentication mechanism and will demonstrate the following.
- How can we navigate from one page to another?
- Sending and retrieving data to and from a managed bean and using the same in another page.
Table Of Contents
1. Introduction
Authentication mechanism allows users to have secure access to the application by validating the username and password. We will have following components in our login application
- LoginBean.java – Managed bean
- index.xhtml – Contains the login form with JSF components
- success.xhtml – If the login is successful, it will show the logged in username
- failure.xhtml – If user credentials are invalid error page will be shown
- faces-config.xml – It’s the JSF configuration file where the success & failure navigation rule will be set
- web.xml – Web application configuration file
But before we create the sample authentication application let’s briefly understand the authentication mechanism in JSF.
The below example shows how to use form-based authentication with a Java Server Faces application. With a form-based application, we can customize the login & error pages that are shown to the client for authentication of the username and password. When a user submits his/her credentials, the server will check whether the user’s credentials are authorized or not. If the user is successfully authenticated, he will be shown the success page or else the error/failure page.
When using the form-based authentication, we specify a page that contains the form for obtaining the username and password. Here is the sample code for this page.
<h:outputLabel value="Username: " />
<h:inputText value="#{loginBean.userName}" />
<h:outputLabel value="Password: " />
<h:inputSecret value="#{loginBean.password}" />
<h:commandButton value="Login" action="#{loginBean.validateUserLogin}" />
The entered credentials will be authenticated by the server and if the user is authorized, he/she will be shown the success page with a welcome message else the failure page with an error message.
This example is based on the JSF method binding approach where the entered credentials will be authenticated against the managed-bean and the user will be shown the expected result based on the navigation rules defined in the JSF configuration.
1.1 JSF Navigation Rule
In simple language, page navigation means the flow of the application from one page to another page. Navigation in JSF defines the set of rules for choosing the next view to be displayed after a specified action is completed.
In JSF, navigation between pages is defined by a set of rules. These rules determine which next page to be displayed depending upon a user clicking a navigation component (such as a button or a hyperlink) or when the navigation is based on the form-based authentication. These navigation rules are defined in JSF configuration file namely faces-config.xml
1.1.1 How to Create Page Navigation Rules
Navigation rule definitions are stored in the JSF configuration file. The general syntax of a JSF navigation rule element in the faces-config.xml file is shown below:
<navigation-rule>
<from-view-id>page-or-pattern</from-view-id>
<navigation-case>
<from-outcome>outcome</from-outcome>
<to-view-id>destination-page</to-view-id>
</navigation-case>
<navigation-case>
...
</navigation-case>
</navigation-rule>
A navigation rule can consist of the following elements –
- navigation-rule: A mandatory element for navigation case elements.
- from-view-id: An optional element that contains either a complete page identifier or a page identifier prefix ending with the asterisk (*) character. If we use the wildcard character, the rule applies to all pages that match the wildcard pattern. To make a global rule that applies to all pages, leave this element blank.
- navigation-case: A mandatory element for each case in the navigation rule. Each case defines the different navigation paths from the same page. A navigation rule must have at least one navigation case.
- from-action: An optional element that limits the application of the rule only to outcomes from the specified action method.
- from-outcome: A mandatory element that contains an outcome value that is matched against values specified in the action attribute.
- to-view-id: A mandatory element that contains the complete page identifier of the page to which the navigation is routed when the rule is implemented.
A sample navigation from our example :
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-action>#{loginBean.validateUserLogin}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/success.xhtml</to-view-id>
</navigation-case>
</navigation-rule>This code specifies that view index.xhtml has two outputs – success and failure associated with a particular outcome. Below is the sample managed bean code in which this navigation case is listed:
public String validateUserLogin() {
String navResult = "";
System.out.println("Entered Username is= " + userName + ", password is= " + password);
if (userName.equalsIgnoreCase("javacodegeeks") && password.equals("access123")) {
navResult = "success";
} else {
navResult = "failure";
}
return navResult;
}1.1.2 Navigation Rule Flow
Here when the Login button is clicked in the index.xhtml, the request containing the form values is intercepted by the validateUserLogin() method of the LogicBean class.
Once the credentials are validated, JSF will resolve the success view name in the faces-config.xml and shows the corresponding result to the user. Similar execution happens in the case of invalid credentials and the user is shown the failure or the error page.
Now, open up the eclipse IDE and let’s start building the application!
2. JSF Authentication Example
2.1 Tools Used
Our preferred environment is Eclipse. We are using Eclipse Kepler SR2, JDK 8 (1.8.0_131) and Tomcat 7 application server. Having said that, we have tested the code against JDK 1.7 and it works well.
2.2 Project Structure
First, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

2.3 Project Creation
In this section, we will see how to create a Dynmaic Web Java project with Eclipse. In eclipse IDE, go to File -> New -> Dynamic web project

In the New Dynamic Project window fill in the below details and click next
- Enter the project name and project location
- Select Target runtime as Apache Tomcat v7.0 from dropdown
- Select Configuration as JavaServer Faces v.2.2 Project from dropdown (this is required to download the java server faces capabilities in your project)
Leave everything as default in this window as we will be making the required java file at a later stage. Simply click next and we will land up on the web-module window
In the Web Module window, leave the context_root and content_directory values as default (however, you can change the context_root but for this application let’s keep it as a default value). Simply, check Generate web.xml deployment descriptor checkbox and click next
In the JSF Capabilities windows, we will require downloading the dependencies (not available by default) so that our project is configured as a JSF module in Eclipse. Add the JSF capabilities to the web project by clicking on the download icon (encircled in fig. 5) and download the JSF 2.2 mojara implementation

A new pop-up window will open where it will auto lists down the JSF library. Select the JSF 2.2 library and click next (the library name and download destination will be auto populated)

Check the license checkbox and click finish. Eclipse will download the JSF 2.2 library and will display them on the JSF capabilities windows (i.e. fig5)

Now the JSF implementation libraries will be listed down on the capabilities page. Select the checkbox (JSF2.2 (Mojarra 2.2.0)) and leave everything else as default. Click Finish

Eclipse will create the project named JSF Authentication in the workspace and web.xml will be configured for accepting the JSF requests. It will have the following code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JSF Authentication</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
</web-app>Now let’s start building the application!
Download NOW!
















How does this prevent an attacker you from seeing other facelets?
This is JSF Navigation example, not JSF Authentication example.
You also can use a Filter. Check the following:
https://jcodepoint.com/jsf/jsf-autenticacion/