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文件。