4

Tomcat的7成基于认证

@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令牌我用其中:

  1. 用户名:tomcat的从此开始:tomcat的
  2. 用户名:both passwort:tomcat

我仍然失败/login-failed.jsp。

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

您对此有何看法?

配置:Tomcat的7.0.23,Eclipse的靛蓝

+0

你可以从server.xml发布你的** **配置吗? – pd40 2012-07-08 10:16:15

+1

@ pd40我相应地更新了我的答案 – arthur 2012-07-08 10:23:27

+0

仔细检查'conf/tomcat-users.xml'中的注释:所有内容都被注释掉以防止在默认配置中进行任何类型的访问。 – 2012-07-09 00:55:13

回答

2

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

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

如果我我不知道对不对,但是当Tomcat运行Eclipse之外的Web应用程序安全仅支持..

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

你可以考虑碰撞了logging帮助诊断AUTH问题。

+0

我加了但注意到真的改变了。 – arthur 2012-07-08 10:42:41

+0

您的login.jsp是否在表单字段中发布了j_username&j_password? – pd40 2012-07-08 10:51:23

+0

是的...登录页面也在上面的问题中得到了.. – arthur 2012-07-08 10:55:43

1

这是为时已晚,我回答,但也许有人来到这里会发现这个有用。

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

+0

不要太晚Prasad。 – Alex 2013-10-15 10:45:12

+0

您可以在Eclipse自动创建的“Servers”项目中更新您的tomcat conf。例如更新tomcat-users.xml并重新启动Tomcat。它应该工作正常。 – Kloe2378231 2014-11-06 10:51:18

0

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