2012-04-17 97 views
2

的web.xml歡迎文件忽略安全約束

<context-param> 
      <param-name>javax.faces.PROJECT_STAGE</param-name> 
      <param-value>Development</param-value> 
     </context-param> 

     <welcome-file-list> 
      <welcome-file>/secured/secure.xhtml</welcome-file> 
     </welcome-file-list> 

     <servlet> 
      <servlet-name>Faces Servlet</servlet-name> 
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
      <load-on-startup>1</load-on-startup> 
     </servlet> 

     <servlet-mapping> 
      <servlet-name>Faces Servlet</servlet-name> 
      <url-pattern>*.xhtml</url-pattern> 
     </servlet-mapping> 

     <context-param> 
      <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name> 
      <param-value>true</param-value> 
     </context-param> 
    <security-constraint> 
     <web-resource-collection> 
      <web-resource-name>Restricted</web-resource-name> 
      <url-pattern>/secured/*</url-pattern> 
      <http-method>GET</http-method> 
      <http-method>POST</http-method> 
     </web-resource-collection> 
     <auth-constraint> 
      <role-name>ADMIN</role-name> 
     </auth-constraint> 
     </security-constraint> 
<login-config> 
    <auth-method>FORM</auth-method> 
    <realm-name>jdbc-realm</realm-name> 
    <form-login-config> 
     <form-login-page>/public/login.xhtml</form-login-page> 
     <form-error-page>/public/error.xhtml</form-error-page> 
    </form-login-config> 
    </login-config> 

我想我的web應用重定向未授權的用戶登錄頁面。有趣的事情,我有這個工作,但我做了一些愚蠢的變化,現在訪問localhost:8080我總是看到secure.xhtml即使沒有登錄。localhost:8080/secured/secure.xhtml重定向罰款。

回答

7

您完全沒有正確使用<welcome-file>。它應該表示無論請求的文件夾(根文件//public/或等)被請求時何時需要提供的文件的唯一文件名。

歡迎文件由內部轉發服務,由RequestDispatcher#forward()執行。內部轉發做不是觸發安全約束。您需要發送重定向。

<welcome-file>更改爲更理智的默認值,例如, index.xhtml

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

並在webapp的根目錄中創建一個,如/index.xhtml。如果您需要/index.xhtml請求重定向到/secured/secure.xhtml,則大致有2種方式:

  1. 上的/index.xhtml URL模式映射一個Filter並調用response.sendRedirect("secured/secure.xhtml")doFilter()方法內。例如。

    @WebFilter("/index.xhtml") 
    public class IndexFilter implements Filter { 
    
        @Override 
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
         HttpServletResponse response = (HttpServletResponse) res; 
         response.sendRedirect("secured/secure.xhtml")); 
        } 
    
        // ... 
    } 
    
  2. 把一個<f:event type="preRenderView">中調用後臺bean的方法,而這又做了externalContext.redirect("secured/secure.xhtml")/index.xhtml。例如。

    <f:event type="preRenderView" listener="#{indexBean.redirect}" /> 
    

    @ManagedBean 
    @ApplicationScoped 
    public class IndexBean { 
    
        public void redirect() throws IOException { 
         FacesContext.getCurrentInstance().getExternalContext().redirect("secured/secure.xhtml"); 
        } 
    
    } 
    
+0

哇感謝!但是,檢查用戶是否已登錄?我應該在同一個過濾器中做這個嗎?將容器從容器中取出可以嗎? – 2012-04-17 14:50:45

+2

''已經做到了。你不需要自己做。唯一的一點是你需要執行重定向而不是前進。重定向會創建一個全新的請求,然後再通過新的URL觸發安全約束。對於轉發來說已經太晚了,因爲安全約束*已經*已經根據'/'的初始URL進行了測試,這是根據您的配置公開訪問而沒有限制。 – BalusC 2012-04-17 14:51:11

+0

我有一個過濾器的問題。如果我沒有調用'security-constraint'過濾器,如果我註釋掉'constraint',它就開始工作。也許是因爲它們在/ secured/*網頁上重疊?服務器 - glassfish 3.1。如果你有時間,請幫我一把。 – 2012-04-17 17:15:24