Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49383
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 340
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-10 15:23
文章分类
文章存档

2014年(1)

2013年(16)

我的朋友

分类: Java

2013-06-26 08:48:46

首先是过滤器代码。这里关于权限的字段为user_role.对用户注册时,已经将此属性的值付给session。

点击(此处)折叠或打开

  1. package filter;

  2. import java.io.IOException;

  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.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12.     
  13. public class MyFilter implements Filter {

  14.     protected FilterConfig filterConfig;
  15.     
  16.     public void destroy() {
  17.              
  18.     }
  19.     
  20.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  21.         // 获取URI地址
  22.          HttpServletRequest req = (HttpServletRequest) request;
  23.          HttpServletResponse res = (HttpServletResponse) response;
  24.          HttpSession session = req.getSession();
  25.          String uri = req.getRequestURI();
  26.            String ctx=req.getContextPath();
  27.            uri = uri.substring(ctx.length());
  28.         
  29.          String role=(String)session.getAttribute("user_role");
  30.         
  31.          if(uri.startsWith("/show")) {
  32.              if (role.equals("0")) {
  33.                    //request.setAttribute("message","您没有这个权限");
  34.                    request.getRequestDispatcher("/error1.jsp").forward(req,res);
  35.                    return;
  36.                }
  37.              else
  38.                  chain.doFilter(request,response);
  39.            }
  40.         
  41.         
  42.          if(uri.startsWith("/yhgl")) {
  43.              if (!role.equals("2")) {
  44.                    //request.setAttribute("message","您没有这个权限");
  45.                    request.getRequestDispatcher("/error.jsp").forward(req,res);
  46.                    return;
  47.                }
  48.              else
  49.                  chain.doFilter(request,response);
  50.            }
  51.          if(uri.startsWith("/xwgl")) {
  52.              if (!role.equals("2")) {
  53.                    //request.setAttribute("message","您没有这个权限");
  54.                    request.getRequestDispatcher("/error.jsp").forward(req,res);
  55.                    return;
  56.                }
  57.              else
  58.                  chain.doFilter(request,response);
  59.            }
  60.          if(uri.startsWith("/rcgl/write")){
  61.              if (role.equals("1")) {
  62.                    //request.setAttribute("message","您没有这个权限");
  63.                    request.getRequestDispatcher("/error1.jsp").forward(req,res);
  64.                    return;
  65.                }
  66.              else
  67.                  chain.doFilter(request,response);
  68.            }
  69.         
  70.         }
  71.         //下面还可以添加其他的用户权限,省去。
  72.     
  73.     

  74.     public void init(FilterConfig arg0) throws ServletException {
  75.              
  76.     }
  77.     
  78. }
web.xml

点击(此处)折叠或打开

  1. <filter>
  2.      <filter-name>MyFilter</filter-name>
  3.       <filter-class>filter.MyFilter</filter-class>
  4. </filter>
  5. <filter-mapping>
  6.       <filter-name>MyFilter</filter-name>
  7.       <url-pattern>/yhgl/*</url-pattern>
  8. </filter-mapping>
  9. <filter-mapping>
  10.       <filter-name>MyFilter</filter-name>
  11.       <url-pattern>/rcgl/write/*</url-pattern>
  12. </filter-mapping>
  13. <filter-mapping>
  14.       <filter-name>MyFilter</filter-name>
  15.       <url-pattern>/xwgl/*</url-pattern>
  16. </filter-mapping>
  17.     <filter-mapping>
  18.       <filter-name>MyFilter</filter-name>
  19.       <url-pattern>/show/*</url-pattern>
  20. </filter-mapping>
权限1、2、3分别是三个不同的权限。
error.jsp和error1.jsp分别是两个错误页面。

点击(此处)折叠或打开

  1. <table align="center" border="6" bgcolor="#ffffff" width="300px" height="130">
  2.   <tr align="center"><td><strong><font size="4" face="楷体">提示信息</font></strong></td></tr>
  3.   <tr align="center"><td><font size="4" face="楷体">抱歉,您没有权限操作此功能!</font></td></tr>
  4.   <tr><td align="right"><font size="2"><a href="indexsy.jsp">返回首页</a></font></td></tr>
  5.   </table>

error1.jsp为:

点击(此处)折叠或打开

  1. <table align="center" border="6" bgcolor="#ffffff" width="300px" height="130">
  2.   <tr align="center"><td><strong><font size="4" face="楷体">提示信息</font></strong></td></tr>
  3.   <tr align="center"><td><font size="4" face="楷体">抱歉,您没有权限操作此功能!</font></td></tr>

  4.   </table>
error带有一个返回首页的超链接。这完全取决于项目的需要。

阅读(525) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~