论坛首页 Java企业应用论坛

使用Filter控制页面的权限

浏览 28626 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-29  

      众所周知,如果没有对页面进行权限控制,用户只要输入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来进行权限控制。

 

   发表时间:2007-07-23  
难道你的 当前用户是固定的吗?你写成了 UserAction.CURRENT_USER这种形式.
0 请登录后投票
   发表时间:2007-07-24  

fantasy 写道:

      众所周知,如果没有对页面进行权限控制,用户只要输入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来进行权限控制。

 





0 请登录后投票
   发表时间:2007-07-24  

拦截器似乎更适合处理权限。
0 请登录后投票
   发表时间:2007-07-25  
有一些问题:
1.因为继承了HttpServlet和Servlet API紧耦合。
2.要注意request dispatcher的情况,Servlet 2.3 规范中的过滤器只能过滤 Web 客户机和其所访问的指定 Web 资源之间的内容。2.4 规范消除了这个限制,通过增强filter和request dispatcher的配合。
0 请登录后投票
   发表时间:2007-07-25  
leeking 写道
难道你的 当前用户是固定的吗?你写成了 UserAction.CURRENT_USER这种形式.

UserAction.CURRENT_USER 这个是当前用户的键,根据这个找到当前用户的值。
0 请登录后投票
   发表时间:2007-07-25  
jomper 写道

拦截器似乎更适合处理权限。

恩 很对。我们目前就是使用拦截器控制的权限!
0 请登录后投票
   发表时间:2007-07-25  
如果权限做的复杂呢!这样的过滤器就不适用了吧
0 请登录后投票
   发表时间:2007-07-26  
我想知道你们说的拦截器是什么?
0 请登录后投票
   发表时间:2007-07-26  
spring MVC 里有个拦截器,蛮好用的.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics