2012-07-03 19 views
2

首先是一些細節:我在web.xml中配置了安全性如下所示 view plaincopy to clipboardprint?HTML資源在servlet中沒有受到約束

<login-config> 
    <auth-method>FORM</auth-method> 
    <form-login-config> 
     <form-login-page>/formLoginPage.html</form-login-page> 
     <form-error-page>/formErrorPage.html</form-error-page> 
    </form-login-config> 
</login-config> 
<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>MyBeerApp</web-resource-name> 
     <url-pattern>/web/form.html</url-pattern> 
     <url-pattern>/SelectBeer.do</url-pattern>   
     <http-method>POST</http-method> 
     <http-method>GET</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>member</role-name> 
    </auth-constraint> 
    <user-data-constraint> 
     <!--transport-guarantee>CONFIDENTIAL</transport-guarantee--> 
    </user-data-constraint> 
</security-constraint> 
<security-role> 
    <role-name>admin</role-name> 
</security-role> 
<security-role> 
    <role-name>member</role-name> 
</security-role> 
<security-role> 
    <role-name>guest</role-name> 
</security-role> 

在Tomcat的user.xml如下: 視圖plaincopy到clipboardprint?

<role rolename="member"/> 
    <role rolename="guest"/> 
    <user username="vgarg2" password="tomcat" roles="member,guest" /> 
    <user username="vgarg3" password="tomcat" roles="guest" /> 

文件是位置如下: 視圖plaincopy到clipboardprint? form.html的

<TOMCAT_HOME>\Beer-v1\index.html 
<TOMCAT_HOME>\Beer-v1\web\form.html 
<TOMCAT_HOME>\Beer-v1\WEB-INF\web.xml 
<TOMCAT_HOME>\Beer-v1\WEB-INF\classes\... 

內容:

視圖plaincopy到clipboardprint?

<html> 
<body> 
<h1 align="center">Beer Selection Page</h1> 
<form method="POST" action="../SelectBeer.do"> 
Select Beer Characteristics 
Color : <select name="color1" size="1"> 
      <option value="light1">Light</option> 
      <option value="amber1">Amber</option> 
      <option value="brown1">Brown</option> 
      <option value="dark1">Dark</option> 
     </select> 
    <br/> 
Can sizes: 
<input type="checkbox" name="sizes" value="12oz">12 oz</input>  
<input type="checkbox" name="sizes" value="24oz">24 oz</input> 
<input type="checkbox" name="sizes" value="36oz">36 oz</input> 
    <br/> 
    <center> 
     <input type="submit"/> 
    </center> 
</form> 
</body> 
<html> 

現在我的問題是,要求將 「HTTP://本地主機:8080 /啤酒-V1 /網絡/ form.html」 是沒有得到約束。 當我將/web/form.html的請求提交到「http:// localhost:8080/Beer-v1/SelectBeer.do」時,它會檢查身份驗證並要求輸入身份/密碼信息。

如果我禁用安全約束,應用程序工作正常。

這裏怎麼回事?

回答

1

我認爲這裏的問題是http://localhost:8080/Beer-v1/web/form.html的響應來自緩存。

如果您需要對靜態頁面進行身份驗證,則應該發送緩存控件標頭,告訴緩存實體不要緩存響應。

  • 的Cache-Control:對於這個,你應該與響應一起發送下列頭無緩存,無店鋪
  • 雜注:無緩存

注:沒有應用上面的標題,如果使用F5刷新頁面,則應該看到身份驗證將起作用。

我建議將所有需要進行身份驗證的頁面作爲JSP的。

+0

Bingo Ramesh :)它的工作。 但是現在我有另一個問題,因爲請求直接轉到「/web/form.html」,並且沒有servlet參與,我怎樣才能設置響應中提到的頭文件? – Vineet

+0

檢查服務器是否支持向靜態頁面添加標題。否則,爲靜態頁面編寫一個自定義過濾器。 –