JSP - SecurityJavaServer Pages and servlets make several mechanisms available to Web developers to secure applications. Resources are protected declaratively by identifying them in the application deployment descriptor and assigning a role to them. Several levels of authentication are available, ranging from basic authentication using identifiers and passwords to sophisticated authentication using certificates. Role Based Authentication: The authentication mechanism in the servlet specification uses a technique called role-based security. The idea is that rather than restricting resources at the user level, you create roles and restrict the resources by role. You can define different roles in file tomcat-users.xml, which is located off of Tomcat's home directory in conf. An example of this file is shown below:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user username="role1" password="tomcat" roles="role1"/> <user username="both" password="tomcat" roles="tomcat,role1"/> <user username="admin" password="secret" roles="admin,manager"/> </tomcat-users> This file defines a simple mapping between user name, password, and role. Notice that a given user may have multiple roles, for example, user name="both" is in the "tomcat" role and the "role1" role. Once you identified and defined different roles, a role-based security restrictions can be placed on different Web Application resources by using the <security-constraint> element in web.xml file available in WEB-INF directory. Following is a sample entry in web.xml: <security-constraint> <web-resource-collection> <web-resource-name> SecuredBookSite </web-resource-name> <url-pattern>/secured/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <description> Let only managers use this app </description> <role-name>manager</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>manager</role-name> </security-role> <login-config> <auth-method>BASIC</auth-method> </login-config> ... </web-app> Above entries would mean:
Form Based Authentication: When you use the FORM authentication method, you must supply a login form to prompt the user for a username and password. Following is a simple code of login.jsp to create a form for the same purpose:
<html>
<body bgcolor="#ffffff"> <form method="POST" action="j_security_check"> <table border="0"> <tr> <td>Login</td> <td><input type="text" name="j_username"></td> </tr> <tr> <td>Password</td> <td> <input type="password" name="j_password"></td> </tr> </table> <input type="submit" value="Login!"> </center> </form> </body> </html> Here you have to make sure that the login form must contain form elements named j_username and j_password. The action in the |