1.在Tomat服务器中的Servers中的tomcat-users.xml文件中的tomcat-users节点下进行用户名和密码的配置
2.在web工程中的web.xml文件中配置一些必要结点
注释:
(1)首页为index.jsp
(2)login-config:定义认证机制
auth-method:指定认证机制(BASIC,FORM,DIGEST,CLIENT-CERT)
form-login-config:定义登录页和错误页
(3)web-resource-collection:指定安全约束应用的资源集合
auth-constraint:指定可访问受保护资源的角色
(4)security-role:指定用于安全约束中的安全角色的声明
<?xml version="1.0" encoding="UTF-8"?>
-<web-app version="2.5" id="WebApp_ID" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>09-03</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>
-<login-config>
<auth-method>FORM</auth-method>
-<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
-<security-constraint>
-<web-resource-collection>
<web-resource-name>form</web-resource-name>
<url-pattern>/form.jsp</url-pattern>
</web-resource-collection>
-<auth-constraint>
<role-name>tomcat</role-name>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
-<security-role>
<role-name>tomcat</role-name>
</security-role>
-<security-role>
<role-name>admin</role-name>
</security-role>
</web-app>