2015-08-03 104 views
0

我正在製作一個學生管理系統,其中有三個用戶。
管理員,學生和超級管理員。
我正在通過過濾器處理授權。用戶登錄時根據其角色重定向,例如,如果用戶是學生,則將用戶重定向到學生頁面。
但問題是超級管理員 在FilterBean
過濾器在無限重定向循環中運行

import java.io.IOException; 
import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.FilterConfig; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRequest; 
import javax.servlet.ServletResponse; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

/** 
* 
* @author Sikandar 
*/ 
public class FilterBean implements Filter{ 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 

    } 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest req = (HttpServletRequest) request; 
     HttpServletResponse resp = (HttpServletResponse) response; 
     //HttpSession session = req.getSession(); 
     HttpSession session = req.getSession(); 
     String userName = (String) session.getAttribute("userName"); 
     String role = (String) session.getAttribute("role"); 
     System.out.println("role in Filter out side condition " +role); 
     String url = req.getRequestURI(); 
     if (session == null || userName == null) { 
      System.out.println("The role is if session == null "+role); 
      if (url.indexOf("admin.xhtml") >= 0 || url.indexOf("student.xhtml") >= 0 || url.indexOf("superadmin.xhtml") >= 0) { 
       resp.sendRedirect(req.getServletContext().getContextPath()+"/login.xhtml"); 
      } else { 
       chain.doFilter(request, response); 
      } 
     } else { 

      if(role.equals("admin") && (url.indexOf("login.xhtml") >= 0 || url.indexOf("superadmin.xhtml") >= 0 || url.indexOf("student.xhtml") >= 0)) { 
       resp.sendRedirect(req.getServletContext().getContextPath()+"/admin.xhtml"); 
      } else if (role.equals("super") && (url.indexOf("login.xhtml") >= 0 || url.indexOf("student.xhtml") >=0)) { 
       System.out.println("role == super "+url.indexOf("admin.xhtml")); 
       resp.sendRedirect(req.getServletContext().getContextPath()+"/superadmin.xhtml"); 
      } else if (role.equals("student") && (url.indexOf("login.xhtml") >= 0 || url.indexOf("admin.xhtml") >=0 || url.indexOf("superadmin.xhtml") >= 0)) { 
       resp.sendRedirect(req.getServletContext().getContextPath()+"/student.xhtml"); 
      } 
      else { 
       chain.doFilter(request, response); 
      } 
      } 
    } 

    @Override 
    public void destroy() { 

    } 

} 

else if (role.equals("super") && (url.indexOf("login.xhtml") >= 0 || url.indexOf("student.xhtml") >=0)) { 
       System.out.println("role == super "+url.indexOf("admin.xhtml")); 
       resp.sendRedirect(req.getServletContext().getContextPath()+"/superadmin.xhtml"); 

,如果我把url.indexOf("superadmin.xhtml");在這種情況下它說,有在頁面更重定向。

+0

爲什麼你需要檢查所有頁面並始終重定向到同一頁面?你的代碼,如果它的作品將超級管理員總是重定向到超級管理員頁面,無論他請求student.xhtml或login.xhtml –

+0

正確的方法是處理登錄頁面中的角色,當用戶輸入用戶名/密碼重定向到正確的結果。之後,製作一個篩選器,只檢查用戶是否登錄,如果他不是,則將其重定向到登錄頁面。 –

回答

0

url.indexOf("superadmin.xhtml");url.indexOf("admin.xhtml");

兩個包含admin.xhtml作爲後綴,這可能一個問題,嘗試重命名superadmin.xhtmladminsuper.xhtml