2012-04-12 112 views
3

我被@BalusC以下答案JSF 2.0: How to get the URL that is entered in the browser's address bar以限制用戶未登錄誰的網頁過濾器實現JSF - 爲受限制的頁面

篩選:

public class RestrictPageFilter implements Filter{ 
    FilterConfig fc; 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
     fc=filterConfig; 
    } 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest httpreq = (HttpServletRequest) request; 
     HttpServletResponse httpres = (HttpServletResponse) response; 
     if (httpreq.getUserPrincipal() == null) { 
      httpreq.getSession().setAttribute("from", httpreq.getRequestURI()); 
      httpres.sendRedirect("/pages/login.xhtml"); 
     } else { 
      chain.doFilter(request, response); 
     } 
    } 

    @Override 
    public void destroy() { 
     // TODO Auto-generated method stub 
    } 
} 

的web.xml:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Admin pages</web-resource-name> 
     <url-pattern>/admin/*</url-pattern> 
     <url-pattern>/restricted/*</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> 

    <security-constraint> 
    <web-resource-collection> 
     <web-resource-name>User pages</web-resource-name> 
     <url-pattern>/restricted/*</url-pattern> 
     <http-method>GET</http-method> 
      <http-method>POST</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>ADMIN</role-name> 
     <role-name>USER</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>/pages/login.xhtml</form-login-page> 
     <form-error-page>/pages/error.xhtml</form-error-page> 
    </form-login-config> 
    </login-config--> 

    <filter> 
     <filter-name>RestrictPageFilter</filter-name> 
     <filter-class>gov.denis.chanceryweb5.filter.RestrictPageFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>RestrictPageFilter</filter-name> 
     <url-pattern>/restricted/*</url-pattern> 
    </filter-mapping> 

與GlassFish的web.xml

<glassfish-web-app> 
<security-role-mapping> 
    <role-name>ADMIN</role-name> 
    <group-name>ADMIN</group-name> 
    </security-role-mapping> 
    <security-role-mapping> 
    <role-name>USER</role-name> 
    <group-name>USER</group-name> 
    </security-role-mapping> 

境界GlassFish中的GUI控制檯: enter image description here

當訪問我的web應用程序,瀏覽器我認爲這出於某種原因?爲什麼?

enter image description here

+1

指定不知道這是否與「認證」的事情,但你的過濾器不應該與* .xhtml所有頁面映射(它會在每一頁上被調用包括索引)。你應該只將它映射到我認爲索引不是的受限頁面上。 – Fallup 2012-04-12 15:55:21

+0

因此我應該將「login.xtml」移出我想限制的頁面? – Melissaa 2012-04-12 15:57:09

+1

登錄頁面應該超出安全限制。你的數據庫表是什麼樣的?您已經擁有了它們,並且正在嘗試將它適配到GlassFish中的JDBCRealm? – rbento 2012-04-12 15:59:40

回答

1

你看到用BASIC認證方法相關的對話框。

您目前已將web.xml文件的login-config元素註釋掉......以便配置未被應用。

GlassFish的3臺服務器有一個默認的登錄,配置,用於在用戶部署的應用程序指定安全性約束,但不指定登錄配置...

有效的登錄,配置爲您的應用程序實際上看起來像這樣

<login-config> 
    <auth-method>BASIC</auth-method> 
    <realm-name>file</realm-name> 
    </login-config> 

默認的登錄配置,那麼在glassfish3/glassfish/domains/<your domain name here>/config/default-web.xml

+0

感謝您的回答。我註釋了登錄配置,因爲回答問題我鏈接說我需要用過濾器來替換它。所以它錯了嗎? – Melissaa 2012-04-13 06:10:21

+0

@Melissaa:我從來不知道BalusC提供的答案是不幸的是,每臺服務器都允許在不受規範限制的區域「增加值」,這是TC和GF不同的地方,如果你希望GlassFish的行爲類似於Tomcat(在你可以從config/default-web.xml中刪除默認的login-config元素 – vkraemer 2012-04-13 16:25:27

+0

我註釋掉了默認的login-config,但它看起來像'request.login(name,pass)'不工作沒有web.xml中的login-config – Melissaa 2012-04-17 09:00:10