Tomcat 的表单验证要求用户自己实现登录页面,并且提供用户名和密码。
当 web.xml 文件中的 <auth-method> 元素设置为 FORM 时,表明应用使用的是表单验证。当用户请求 Web 应用程序受保护的资源时,表单验证会跳转至配置的登录页面。当登录失败时,表单验证会跳转至错误页面。
可以使用 IDEA、Eclipse 等工具创建一个 Web 项目(当然也可以手动创建),项目的的结构如下图:

其中:
index.jsp 为 Web 应用的主页面,需要用户登录授权后才能访问。
login.jsp 为 Web 应用的登录页面。
error.jsp 为 Web 应用的错误页面。
web.xml 为 Web 应用的配置文件。
login.jsp 文件内容如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form method="post" action="j_security_check">
<p>
用户名:<input type="text" name="j_username" />
</p>
<p>
密 码:<input type="password" name="j_password" />
</p>
<p>
<button type="submit">登录</button>
</p>
</form>
</body>
</html>注意:登录表单的 method 必须为 post,action 必须是 j_security_check,用户名的 name 必须为 j_username,密码的 name 必须为 j_password。
index.jsp 文件内容如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>DEMO</title>
</head>
<body>
<h1>FORM 表单验证</h1>
</body>
</html>error.jsp 文件内容如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>错误</title>
</head>
<body>
<p>用户名/密码不正确,登录失败!</p>
</body>
</html>web.xml 配置文件内容如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 安全限制配置 -->
<security-constraint>
<display-name>default</display-name>
<!-- 需要限制访问的资源子集 -->
<web-resource-collection>
<web-resource-name>all files</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<!-- 指定可以访问该资源集合的用户角色 -->
<auth-constraint>
<role-name>manager-gui</role-name>
</auth-constraint>
<!-- 用来显示怎样保护在客户端和Web容器之间传递的数据 -->
<!-- NONE 不需要传输保证 -->
<!-- INTEGRAL 服务器和客户端之间的数据必须以某种方式发送,而且在传送中不能改变 -->
<!-- CONFIDENTIAL 传输的数据必须是加密的数据 -->
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- 表单方式验证 -->
<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-role>
<role-name>manager-gui</role-name>
</security-role>
</web-app>其中,将 <auth-method> 配置为 FORM 表示表单验证,<form-login-page> 配置登录页面路径,<form-error-page> 配置错误页面路径。
打开 tomcat 的 tomcat-users.xml 配置文件,添加一个名为 tomcat 的用户,用户密码使用明文。如下:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="aaaaaa"
roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui"/>
</tomcat-users>将该 Web 应用打包成 war 包的方式去部署运行 Web 应用,或者直接通过 IDEA 内置 Tomcat 功能运行该 Web 应用。
运行成功后,使用浏览器访问项目 index.jsp 页面。如下图:

输入用户名和密码点击“登录”按钮进行登录,如果密码验证通过,则跳转到主页。如下图:

通过浏览器调试工具,可以看到请求 j_security_check 接口的内容为 “j_username=tomcat&j_password=aaaaaa”。