4

给定一个 Servlet HelloServlet:

@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 安全策略:

<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>

和 conf/tomcat-users.xml 中的 tomcat-roles 定义

  <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-Token:

  1. 用户名:tomcat 密码:tomcat
  2. 用户名:两个密码:tomcat

我还是来了失败/login-failed.jsp。

这是我对此的看法:tomcat 将我重定向到登录页面,但不会读取 conf/tomcat-users.xml 来验证我的登录(即使在多次重新启动后)。

你怎么看待这件事 ?

配置:Tomcat 7.0.23,Eclipse-Indigo

4

5 回答 5

2

遵循@pd40 的提议,我尝试了示例/jsp/security/protected/ 示例,但不在 Eclipse IDE 中,Tomcat 通常与其他服务器(Glassfish、JBoss 等)一起嵌入,而是我启动了 tomcat 服务器作为独立的(在其 /bin 目录中).. 它在那里工作。

但是当它试图在 Eclipse 中的 Tomcat 中运行基于安全的 Web 应用程序时,它再次失败,即使使用上述配置也是如此。

我不知道我是否正确,但只有当tomcat在eclipse之外运行时才支持Web-Application Security ..

于 2012-07-08T16:30:43.267 回答
1

这对我来说太迟了,但也许来这里的人可能会觉得这很有用。

实际上,如果您遇到无法通过 eclipse 运行 tomcat 配置并在其外部运行的问题,那么只需从 eclipse 服务器选项卡中删除服务器并再次添加。这应该可以解决问题。

于 2013-05-21T04:24:00.930 回答
1

tomcat 示例 web.xml 包括以下部分<login-config>

<!-- Security roles referenced by this web application -->
<security-role>
  <role-name>role1</role-name>
</security-role>
<security-role>
  <role-name>tomcat</role-name>
</security-role>

您可能需要。


Tomcat 包含一个示例战争,其中包含使用tomcat-users.xml的身份验证,类似于您正在尝试的内容。如果部署了tomcat home/webapps/examples,请尝试访问http://localhost/examples/jsp/security/protected/。确保已删除围绕tomcat-users.xml的角色/用户部分的 XML 注释。它们默认被注释掉。

<!-- Un comment me 
<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"/>
-->

您可以考虑增加日志记录以帮助诊断身份验证问题。

于 2012-07-08T10:30:30.630 回答
1

通过在 web.xml 中定义安全页面,您可以限制对内容的访问:

<url-pattern>/*</url-pattern>

该通配符是指内容路径中的所有页面。因此,您获得了重定向到登录页面的不定式循环。

于 2018-09-20T08:36:35.557 回答
0

我发现如果您更改嵌入在 eclipse 中的 tomcat-users.xml 中的用户配置,您必须重新启动 eclipse 而不仅仅是服务器才能识别新用户。我猜 Eclipse 缓存了 tomact-user.xml 文件。

于 2013-10-15T10:45:34.240 回答