關鍵字:   使用Fiter來控制頁面的權限    

轉載:http://www.javaeye.com/topic/95432

眾所周知,如果沒有對頁面進行權限控制,用戶只要輸入URL就能進入任何頁面。

     下面就演示一下最基本的使用Fiter來控制頁面的權限。

     1.寫一個FILTER,用來判斷用戶是否有權限進入指定頁面。

java 代碼
  1. import java.io.IOException;   
  2.   
  3. import javax.servlet.Filter;   
  4. import javax.servlet.FilterChain;   
  5. import javax.servlet.FilterConfig;   
  6. import javax.servlet.ServletException;   
  7. import javax.servlet.ServletRequest;   
  8. import javax.servlet.ServletResponse;   
  9. import javax.servlet.http.HttpServlet;   
  10. import javax.servlet.http.HttpServletRequest;   
  11. import javax.servlet.http.HttpServletResponse;   
  12. import javax.servlet.http.HttpSession;   
  13.   
  14. import org.apache.log4j.Logger;   
  15.   
  16. import com.kiral.action.UserAction;   
  17. import com.kiral.model.User;   
  18.   
  19. /*******************************************************************************  
  20.  * 在過濾器中實現權限控制類,用來檢驗用戶是否有權限進入當前頁面  
  21.  *   
  22.  * @作者:kiral  
  23.  * @日期:2006-6-24  
  24.  * @版本: 1.0  
  25.  ******************************************************************************/  
  26. public class FilterServlet extends HttpServlet implements Filter {   
  27.     private static final long serialVersionUID = 5162189625393315379L;   
  28.   
  29.     private static Logger LOG = Logger.getLogger(FilterServlet.class);   
  30.   
  31.     /**  
  32.      * 配置允許的角色  
  33.      */  
  34.     private String allowRole = null;   
  35.   
  36.     /**  
  37.      * 重定向的URL  
  38.      */  
  39.     private String redirectURl = null;   
  40.   
  41.     public void init(FilterConfig filterConfig) throws ServletException {   
  42.         // 得到允許的角色,這個參數是由web.xml里的allowRole所指定   
  43.         allowRole = filterConfig.getInitParameter("allowRole");   
  44.         // 指定要重定向的頁面   
  45.         redirectURl = "/locker/index.html";   
  46.     }   
  47.   
  48.     /**  
  49.      * 在過濾器中實現權限控制  
  50.      */  
  51.     public void doFilter(ServletRequest sRequest, ServletResponse sResponse,   
  52.             FilterChain filterChain) throws IOException, ServletException {   
  53.         HttpServletRequest request = (HttpServletRequest) sRequest;   
  54.         HttpServletResponse response = (HttpServletResponse) sResponse;   
  55.         HttpSession session = request.getSession();   
  56.   
  57.         // 如果回話中的用戶為空,頁面重新定向到登陸頁面   
  58.         if (session.getAttribute(UserAction.CURRENT_USER) == null) {   
  59.             response.sendRedirect(redirectURl);   
  60.         }   
  61.         // 會話中存在用戶,則驗證用戶是否存在當前頁面的權限   
  62.         else {   
  63.             User user = (User) session.getAttribute(UserAction.CURRENT_USER);   
  64.             try {   
  65.                 // 如果用戶沒有當前頁的權限,頁面重新定向到登陸頁面   
  66.                 if ("0".equals(allowRole) || user.hasPower(allowRole)) {   
  67.                     filterChain.doFilter(sRequest, sResponse);   
  68.                 } else {   
  69.                     // 過濾器經過過濾后,過濾鏈繼續傳遞請求和響應   
  70.                     response.sendRedirect(redirectURl);   
  71.                 }   
  72.             } catch (Throwable e) {   
  73.                 LOG.error("權限過濾時候出現錯誤", e);   
  74.                 throw new RuntimeException("權限過濾時候出現錯誤", e);   
  75.             }   
  76.         }   
  77.     }   
  78.   
  79.     public void destroy() {   
  80.     }   
  81.   
  82. }  

 

在web.xml中配置 要過濾的頁面和能進入當前頁面的角色

xml 代碼
  1. <!--權限控制過濾器配置-->  
  2.     <filter>  
  3.         <filter-name>UserAdminfilter-name>  
  4.         <!-- 指定進行權限過濾的類-->  
  5.         <filter-class>com.emap.web.FilterServletfilter-class>  
  6.         <!-- 初始化參數,這些參數對應類里變量-->  
  7.         <init-param>  
  8.             <!-- 配置允許進入的角色,這里你可以自己定義任何參數-->  
  9.             <param-name>allowRoleparam-name>  
  10.             <param-value>1param-value>  
  11.         init-param>  
  12.     filter>  
  13.     <filter-mapping>  
  14.         <filter-name>UserAdminfilter-name>  
  15.         <url-pattern>/jsp/security/*url-pattern>  
  16.     filter-mapping>  

上面配置的意思是說,當用戶進入/jsp/security文件夾下的頁面的時候,程序會進入FilterServlet 里的doFilter方法里,進行權限判斷。

 其他的頁面權限控制:

  1.你可以在filter里判斷用戶是否登錄,然后需要特殊權限能訪問的頁面,在頁面里進行判斷。

  2.推薦使用開源框架ACEGI來進行權限控制