Develop form login pages

 

Overview

A Web client or browser can authenticate a user to a Web server using one of the following mechanisms:

HTTP basic authentication The HTTP basic authentication transmits a user password from the Web client to the Web server in simple base64 encoding.
HTTPS client authentication Requires a user (Web client) to possess a public key certificate. The Web client sends the certificate to a Web server that requests the client certificates. This is a strong authentication mechanism.

Form-based Authentication A developer controls the look and feel of the login screens using this authentication mechanism. User password is sent from the browser to the Web server in plain text.

Both HTTP basic authentication and form-based authentication are not very secure unless the HTTPS protocol is used.

The Web application deployment descriptor (web.xml) contains information about which authentication mechanism to use.

When form-based authentication is used, the deployment descriptor also contains entries for login and error pages. A login page can be either an HTML page or a JSP page. This login page displays on the Web client side when a secured resource (servlet, JSP file, HTML page) is accessed from the application. On authentication failure, an error page displays. You can write login and error pages to suit the application needs and control the look and feel of these pages. During assembly of the application, an assembler can set the authentication mechanism for the application and set the login and error pages in the deployment descriptor.

Form login uses the servlet sendRedirect() method, which is used twice during form login, initially displaying the form login page in the Web browser, and then later redirecting the Web browser back to the originally requested protected page.

The sendRedirect(String URL) method tells the Web browser to use the HTTP GET (not the HTTP POST) request to get the page specified in the URL. If HTTP POST is the first request to a protected servlet or JSP file, and no previous authentication or login occurred, then HTTP POST is not delivered to the requested page. However, HTTP GET is delivered because form login uses the sendRedirect() method, which behaves as an HTTP GET request that tries to display a requested page after a login occurs.

Using HTTP POST, you might experience a scenario where an unprotected HTML form collects data from users and then posts this data to protected servlets or JSP files for processing, but the users are not logged in for the resource. To avoid this scenario, structure your Web application or permissions so that users are forced to use a form login page before the application performs any HTTP POST actions to protected servlets or JSP files.

See the Example: Form login article for sample form login pages.

  1. Create a form login page with the required look and feel including the required elements to perform form-based authentication.

  2. Create an error page. You can program error pages to retry authentication or display an appropriate error message.

  3. Place the login page and error page in the *.war file relative to the top directory. For example, if the login page is configured as /login.html in the deployment descriptor, place it in the top directory of the WAR file. An assembler can also perform this step using the assembly tool.

  4. Create a form logout page and insert it to the application only if required.

 

What to do next

After developing login and error pages, add them to the Web application. Use the assembly tool to configure an authentication mechanism and insert the developed login page and error page in the deployment descriptor of the application.


Related concepts
Web component security
Related reference
Example: Form login
Security: Resources for learning