| Sign In/My Account | View Cart |
Web FORM-Based Authentication
Pages: 1, 2
Related Reading:![]() Java Security, 2nd Edition |
|||||
We will go through the simple steps required in setting up the standards-based FORM-based authentication.
web.xml to use FORM-based authenticationLet's tell the container to use FORM-based authentication in our web.xml file.
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/LoginForm.html</form-login-page>
<form-error-page>/LoginError.html</form-error-page>
</form-login-config>
</login-config>
First, we specify "FORM" as the auth-method (instead of BASIC, DIGEST, or CLIENT-CERT), and then we tell the system that the Web page LoginForm.html has the <FORM> which will authenticate a user. If we try to access a page under /secure we will first have to fill out the form in LoginForm.html and authenticate. If our authentication fails (we do not log in correctly as the system user), then we will be sent to LoginError.html.
Now we build out login form. We have to follow a couple of conventions that are defined in the Servlet API specification:
<form>'s action field must be j_security_checkj_username, and j_password that hold the username and password to authenticate withSo, our LoginForm.html will simply look like:
<form method="POST" action="j_security_check">
Username: <input type="text" name="j_username"><br />
Password: <input type="password" name="j_password"><br />
<br />
<input type="submit" value="Login">
<input type="reset" value="Reset">
</form>
Let's say a browser tries to access something under /secure in our deployed Web application. The container will do the following:
LoginForm.html.LoginError.html is sent back to the browser.LoginError.html will.And that is it! Using FORM-based authentication is easy. You configure the web.xml to point to the correct login form and error page, and then make sure that the form follows the conventions of using j_security_check, j_username, and j_password.
Lastly, we can declaratively control the level of security in the transport mechanism using the following tag in web.xml:
<user-data-constraint>
<description>SSL not required</description>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
There are three possible values for the <transport-guarantee>:
| Transport | Description |
NONE |
No encryption is required (http is fine) |
CONFIDENTIAL |
The data must be encrypted, so that other parties can not observe the contents (e.g. enforce SSL) |
INTEGRAL |
The data must be transported so that the data cannot be changed in transit. Most servers use SSL for this value too, although in theory you could use some hashing algorithm, as encryption is not required |
We have shown that you can configure many security options for your Web-based applications, adding support for a standard way to do FORM-based authentication that the Web container takes care of for you. Please download the sample Web application and test it out by trying to access /logintest/secure/ (assuming that you deploy the Web application as "logintest").
Dion Almaer is a Principal Technologist for The Middleware Company, and Chief Architect of TheServerSide.Com J2EE Community.
Return to ONJava.com.