2012-03-27 44 views
2

在我的web應用程序,我有3個主要部分 1.客戶 2.供應商 3.管理員Java會話過濾器

我使用Java會話過濾器來檢查用戶會話,並允許訪問的特定部分網站。 因此客戶只能訪問客戶部分,供應商可以訪問供應商部分,管理員可以訪問管理部分。

客戶的會話過濾器已經實現並且工作正常。它檢查客戶身份驗證並訪問客戶子文件夾,從而我有幾個jsp。

如果我想過濾器來檢查供應商和管理員部分身份驗證,並允許他們根據他們的用戶級別訪問。

我需要創建2個更多的過濾器 - 管理員和供應商嗎?

目前這裏是我的客戶實行:

public class SessionFilter implements Filter { 



     private FilterConfig config; 

     /** Creates new SessionFilter */ 
     public SessionFilter() { 
     } 

     public void init(FilterConfig filterConfig) throws ServletException { 

     System.out.println("Instance created of " + getClass().getName()); 
     this.config = filterConfig; 
     } 

     public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain chain) throws java.io.IOException, ServletException { 

     HttpSession session = ((HttpServletRequest) request).getSession(); 
     ServletContext context = config.getServletContext(); 
     /* 
     * use the ServletContext.log method to log filter messages 
     */ 
     context.log("doFilter called in: " + config.getFilterName() + " on " 
      + (new java.util.Date())); 

     // log the session ID 
     context.log("session ID: " + session.getId()); 

     // Find out whether the logged-in session attribute is set 
     Object u= session.getAttribute("users"); 
     if (u != null){ 
      chain.doFilter(request, response); 
     } 
     else{ 
      //request.getRequestDispatcher("../index.jsp").forward(request, response); 
      ((HttpServletResponse) response).sendRedirect(((HttpServletResponse) response).encodeRedirectURL("../index.jsp?error=userpriv")); 
     } 


     } 


     public void destroy() { 

     } 
    } 

這裏是我的web.xml

<filter> 
    <filter-name>SessionFilter</filter-name> 
    <filter-class>controller.SessionFilter</filter-class> 
    <init-param> 
     <param-name>avoid-urls</param-name> 
     <param-value>index.jsp</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>SessionFilter</filter-name> 
    <url-pattern>/users/*</url-pattern> 
</filter-mapping> 

回答

3

你爲什麼不使用Servlet身份驗證。您只需使用<security-constraint>標記定義角色並映射到網址即可。

這裏是一個說明如何定義樣本:

安全性約束對於客戶

​​

安全性約束對於供應商

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>User Restriction</web-resource-name> 
     <url-pattern>/suppliers/*</url-pattern> 
    </web-resource-collection> 

    <auth-constraint> 
     <role-name>supplier</role-name> 
    </auth-constraint> 
</security-constraint> 

安全性約束對於管理員

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>User Restriction</web-resource-name> 
     <url-pattern>/admin/*</url-pattern> 
    </web-resource-collection> 

    <auth-constraint> 
     <role-name>administrator</role-name> 
    </auth-constraint> 
</security-constraint> 
+0

能否請您闡述一下我是如何實現servlet身份驗證或任何有用的鏈接。 是否與檢查每個頁面上的用戶會話相同? – Parvesh 2012-03-27 06:04:52

+0

我想你的意思是這樣的: http://www.informit.com/articles/article.aspx?p=24253 我試過使用基於表格 - 使用jdbc .. realms。無法配置它,這就是爲什麼使用會話篩選器 – Parvesh 2012-03-27 06:09:54

+0

是的,您可以在容器中配置基於數據庫的Realm(這是特定容器)並配置上面定義的安全約束。 – 2012-03-27 06:28:26

0

我相信你一定能找到實現在同一個過濾器的一切辦法,但如果你想跟着「單一職責原則」最好是有一類每個角色。也許將來你將不得不爲每個用戶做特定的處理,所以最好有專門的過濾器。

如果你真的需要有隻有一個過濾器,你可以按照這個線程:

Writing an authorization filter for my web app(JSF 2.0)