2012-02-24 73 views
4

我有以下web.xml文件我保持歡迎頁面進入安全檢查,以便它將重定向到登錄頁面,但歡迎頁面顯示時沒有用戶登錄。這是正確的辦法? enter image description here歡迎頁面顯示時沒有被重定向到登錄頁面

<welcome-file-list> 
     <welcome-file>/GISPages/welcome.xhtml</welcome-file> 
    </welcome-file-list> 

    <resource-ref> 
     <res-ref-name>jdbc/Gis_WebApp</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
    </resource-ref> 


    <security-constraint> 
     <web-resource-collection> 
      <web-resource-name>Protected Pages</web-resource-name> 
      <url-pattern>/GISPages/*</url-pattern> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>registereduser</role-name> 
      <role-name>admin</role-name> 
     </auth-constraint> 
    </security-constraint> 

    <login-config> 
     <auth-method>FORM</auth-method> 
     <realm-name>Live</realm-name> 
     <form-login-config> 
      <form-login-page>/login.xhtml</form-login-page> 
      <form-error-page>/noauth.xhtml</form-error-page> 
     </form-login-config> 
    </login-config> 

    <security-role> 
     <role-name>registereduser</role-name> 
    </security-role> 

    <security-role> 
     <role-name>admin</role-name> 
    </security-role> 
+0

在'web-resource-collection'標籤下,你有'url-pattern'的重複條目。這可能是問題的原因。文件的其餘部分在哪裏? – 2012-02-24 12:14:25

+0

@maple_shaft刪除了這些,但仍然是同樣的問題。以及我已經添加了圖像文件。 – kinkajou 2012-02-24 13:57:46

+2

只是比較它與我的配置。唯一不同的是,login.xhtml和error.xhtml也位於受保護的文件夾中,而不是外部。只是一個猜測。 – 2012-02-24 14:29:04

回答

1

安全限制保護的URL模式,但在這種情況下,由於歡迎文件設置默認網址將變爲類似於http://:端口/ webcontext /和welcome.xhtml將顯示。儘管根據定義的URL模式,受保護的URL應該具有像http://:port/webcontext/GISPages/welcome.xhtml 這樣的URL。由於URL模式與應用程序服務器不匹配呈現頁面內容。

這工作對我來說唯一的辦法是檢查UserPrincipal在預渲染事件

<f:event type="preRenderComponent" 
listener="#{bean.forwardToLoginIfNotLoggedIn}" /> 

,並重定向到login.xhtml如果UserPrincipal返回null。

打開舊線程的道歉。我最近面臨類似的問題,因此認爲這可能對一些人有用。