<!--
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
-->
还有这个
我们这里假设让用户通过表单的方式通过验证,需要2 个 html 文件: login.html和 loginerr.html。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login to My Web Application</h1>
<p>
If you have been issued a username and password, key them in here now!
</p>
<form method="POST" action="j_security_check">
Username : <input type="text" size="15" maxlength="25" name="j_username"><br><br>
Password : <input type="password" size="15" maxlength="25" name="j_password"><br><br>
<input value="Login" type="submit"> <input value="Clear" type="reset">
</form>
</body>
</html>
还有错误页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Authentication Error!</title>
</head>
<body>
<h1>Authentication Error!</h1>
<p>
Oops! You either keyed in the wrong username or password.
</p>
<a href="javascript:history.back(1)">Try again ?</a>
</body>
</html>
我自己登陆时使用的是
账号:bjensen 《=在openDJ里面的用户名 uid
密码:123456 《= 在openDJ服务里面输入的
对了,还有一点非常重要。
就是在你的项目的web.xml里面的最后加入以下代码:
<security-constraint>
<web-resource-collection>
<web-resource-name>balancer</web-resource-name>
<description> accessible by authenticated users of the tomcat role</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
</web-resource-collection>
<auth-constraint>
<description>These roles are allowed access</description>
<role-name> tomcat </role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>YourWebApp Protected Area</realm-name>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/autherr.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description>Only 'tomcat' role is allowed to access this web
application</description>
<role-name> tomcat </role-name>
</security-role>
知道web.xml的人都知道 web.xml的配置格式是:
<web-app>
各种<context-param>
各种<filter>和<filter-mapping>
各种<listener>
各种<servlet>和<servlet-mapping>
<session-config>
<welcome-list>
各种<error-page>
<tag-lib>
<resource-ref>
<security-constrain>
<login-config>
<security-role>
</web-app>
这样才能正确解析。
说到这里,启动你的服务试试吧~~!!