javawebday56(粗粒度权限控制通过过滤器 请求编码问题 )


粗粒度权限控制(拦截是否登录、拦截用户名admin权限)
RBAC(Role-Based Access Control)->基于角色的权限控制

index.jsp

<h1>欢迎游客登录</h1>
<a href="<c:url value='/index.jsp'/>" >游客入口</a>
<a href="<c:url value='/users/u.jsp'/>" >会员入口</a>
<a href="<c:url value='/admin/a.jsp'/>" >管理员入口</a>
  </body>

login.jsp

  <body>
 <h1>登录</h1>
 ${msg }
 <form action ="<c:url value='/LoginServlet' />" method="post">
    用户名<input type="text" name="username"/>
    <input type="submit" value="登录"/>
 </form> 
  </body>

LoginServlet

public class LoginServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        /*
         * 1、获取用户名
         * 2、判断用户名是否包含my
         *  3、如果包含就是管理员
         *  4、如果不包含,就是普通会员
         * 5、要把登录的用户名称保存到session中
         * 6、转发到index.jsp
         */
        String username =request.getParameter("username");
        if(username.contains("my")){
            request.getSession().setAttribute("admin", username);
        } else{
            request.getSession().setAttribute("username", username);
        }
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }

}

UserFilter

public class UserFilter implements Filter {

    public UserFilter() {
        super();
    }

    private FilterConfig filterConfig;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        /*
         * 1、得到session
         * 2、判断session域中是否存在admin,如果存在,放行
         * 3、判断session域中是否存在username,如果存在,放行,如果不存在打回到Login.jsp,并返回
         */
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String name = (String) request.getSession().getAttribute("admin");
        if(name!=null){
            filterChain.doFilter(req, res);
            return;
        }
        name = (String) request.getSession().getAttribute("username");
        if(name !=null){
            filterChain.doFilter(req, res);
        } else{
            request.setAttribute("msg", "非会员用户 先注册");
            request.getRequestDispatcher("/login.jsp").forward(req,res);
        }
    }

    public void destroy() {

    }
}

web.xml

<filter>
    <filter-name>UserFilter</filter-name>
    <filter-class>my.filter.UserFilter</filter-class>
</filter>
    <filter-mapping>
        <filter-name>UserFilter</filter-name>
        <url-pattern>/users/*</url-pattern>
</filter-mapping>   
<filter>
    <filter-name>AdminFilter</filter-name>
    <filter-class>my.filter.AdminFilter</filter-class>
</filter>
    <filter-mapping>
        <filter-name>AdminFilter</filter-name>
        <url-pattern>/admin/*</url-pattern>
</filter-mapping>  

EncodingFilter

public class EncodingFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        //处理post请求问题
        req.setCharacterEncoding("utf-8");

        HttpServletRequest request = (HttpServletRequest) req;
        //处理GET请求的编码问题
//      String username = req.getParameter("username");
//      System.out.println(new String(username.getBytes("iso-8859-1"),("UTF-8")));
//      username = new String(username.getBytes("iso-8859-1"),("UTF-8"));
//      req.setAttribute("username", username);
        /*
         * 调包request
         * 1、写一个request的装饰类
         * 2、在放行时,使用我们自己的request
         */
        if (request.getMethod().equals("GET")) {
            EncodingRequest er = new EncodingRequest(request);
            filterChain.doFilter(er, res);
        } else if (request.getMethod().equals("POST")) {
            filterChain.doFilter(req, res);
        }
    }

    public void destroy() {

    }
}

EncodingRequest

/**
 * 装饰request
 * @author Administrator
 *继承的完成了装饰者模式 完成了一切拜托你 即完成重写 我们只需要覆盖自己所需要的
 */
public class EncodingRequest extends HttpServletRequestWrapper{

    private HttpServletRequest req;

    public EncodingRequest(HttpServletRequest request) {
        super(request);
        this.req = request;
    }
    @Override
    public String getParameter(String name) {
        String value = req.getParameter(name);
        //处理编码问题
//      try {
//          value = new String(value.getBytes("iso-8859-1"),"utf-8");
//      } catch (UnsupportedEncodingException e) {
//          throw new RuntimeException(e);
//      }
        return value;
    }
}

AServlet

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        response.getWriter().println(username);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        response.getWriter().println(username);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值