您可以使用部署描述符使用容器管理的身份验证。这不需要您期望使用简单的登录表单(带有输入和密码字段并提交到URL)的额外代码j_security_check@H_419_2@。这是一个基本示例:
<form action="j_security_check" method="post">
<input type="text" name="j_username">
<input type="password" name="j_password">
<input type="submit">
</form>
@H_419_2@
假设您在名为的文件夹中有私人页面,/private@H_419_2@并且上面的登录页面位于中
/private/login.jsp@H_419_2@,则将以下条目添加到webapp的
web.xml@H_419_2@:
<security-constraint>
<web-resource-collection>
<web-resource-name>Private</web-resource-name>
<url-pattern>/private/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>friends</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Private</realm-name>
<form-login-config>
<form-login-page>/private/login.jsp</form-login-page>
<form-error-page>/private/error.jsp</form-error-page>
</form-login-config>
</login-config>
@H_419_2@
然后,在你使用该servletcontainer需要配置所谓的 境界 了Private@H_419_2@。由于不清楚您使用的是哪个servlet容器,因此以下是针对Tomcat 8.0的文档:Realm Configuration HOW-TO。您可以对其进行配置,以针对XML文件或数据库甚至是自定义位置来验证用户名/密码组合。