@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public HelloServlet() {
// TODO Auto-generated constructor stub
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.print("hello my Friend: " + request.getRemoteUser());
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("This is the Test Servlet");
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = (String) headerNames.nextElement();
out.print("<br/>Header Name: <em>" + headerName);
String headerValue = request.getHeader(headerName);
out.print("</em>, Header Value: <em>" + headerValue);
out.println("</em>");
}
}
....
}
在web.xml中聲明的tomcat的安全策略:在CONF/tomcat的用戶
<security-constraint>
<web-resource-collection>
<web-resource-name>my application</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>tomcat</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login-failed.jsp</form-error-page>
</form-login-config>
</login-config>
和Tomcat的角色定義。 XML
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
在 「server.xml中」 的境界是:
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
,我嘗試使用url localhost/jsfWorkgroup/HelloServlet訪問Servlet「HelloServlet」。
像預期的,我(重新)定向到登錄頁面:
<form method="POST" action="j_security_check">
<table>
<tr>
<td colspan="2">Login to the Tomcat-Demo application:</td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="j_username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password"/ ></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Go" /></td>
</tr>
</table>
</form>
無論ID令牌我用其中:
- 用戶名:tomcat的從此開始:tomcat的
- 用戶名:both passwort:tomcat
我仍然失敗/login-failed.jsp。
這裏是我對此的看法:tomcat會將我重定向到登錄頁面,但不會讀取conf/tomcat-users.xml來驗證我的登錄(即使重新啓動幾次)。
您對此有何看法?
配置:Tomcat的7.0.23,Eclipse的靛藍
你可以從server.xml發佈你的** **配置嗎? –
pd40
2012-07-08 10:16:15
@ pd40我相應地更新了我的答案 – arthur 2012-07-08 10:23:27
仔細檢查'conf/tomcat-users.xml'中的註釋:所有內容都被註釋掉以防止在默認配置中進行任何類型的訪問。 – 2012-07-09 00:55:13