过滤器filter使用之案例二

来源:互联网 发布:十六进制转十进制算法 编辑:程序博客网 时间:2024/06/06 05:54

案例二:使用filter过滤器控制访问权限


本案例主要内容如下:

在电商管理系统中,客户没有登录,则不能访问购物车页面,同理没有登陆的客户,也不可以将商品添加到购物车。
实现的方式有多种,以filter过滤器最为方便


1. 自定义LoginFilter类,实现Filter接口
package cn.com.mp.filter;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.annotation.WebFilter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;@WebFilter("/LoginFilter")public class LoginFilter implements Filter {    public LoginFilter() {    }    public void destroy() {    }    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)            throws IOException, ServletException {        HttpServletRequest req = (HttpServletRequest) request;        HttpServletResponse res = (HttpServletResponse) response;        HttpSession session = req.getSession();        // 要过滤的uri        System.out.println("过滤uri===" + req.getRequestURI());        //name是登陆的时候session.setAttribute("name")        if (session == null || session.getAttribute("name") == null) {//重定向到未登录提示页面           res.sendRedirect(req.getContextPath() + "/jsp/front/notLoginTip.jsp");            return;        }        // 继续访问其他资源        chain.doFilter(req, res);    }    public void init(FilterConfig fConfig) throws ServletException {    }}
2. web.xml中配置LoginFilter,这一步相当重要,若配置有错,则可能filter过滤无效或者过滤太严格
<filter>    <filter-name>LoginFilter</filter-name>    <filter-class>cn.com.mp.filter.LoginFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>LoginFilter</filter-name>    <!-- 要过滤的路径或者要控制的路径 cart.jsp为购物车页面 --->    <url-pattern>/jsp/front/cart.jsp</url-pattern>  </filter-mapping>  <filter-mapping>    <filter-name>LoginFilter</filter-name>      <!-- 要过滤的路径或者要控制的路径 AddCartServlet为添加到购物车的路径--->    <url-pattern>/AddCartServlet</url-pattern>  </filter-mapping>

Filter过滤的路径可以同时配置多个,根据项目的不同,配置自己所需要控制的路径即可。

3. notLoginTip.jsp 未登录提示页面
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>未登录提示</title><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()            + path + "/";%><script type="text/javascript">    alert("亲,您还没有登陆,请登陆后访问!");    window.location.href="<%=path%>"+"/jsp/front/login.jsp";</script></head><body></body></html>
4. 购物车页面及代码

这里写图片描述


代码:

<a href="checkout.jsp"                        style="font-size: 16px; color: white; margin-left: 10px; text-decoration: none;">购物车</a>
5. 添加购物车图片及代码

这里写图片描述


<a href="../../AddCartServlet" class="add-cart item_add">添加至购物车</a>

总结:

合理的使用filter过滤可以为我们访问权限控制,得到和方便的解决,当然也可以使用shiro框架完成,这个在后续的博客中后写到。以上便是笔者使用filter过滤器控制权限访问的案例,如有不正确之处,还请多多指教。
0 0
原创粉丝点击