博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
过滤Filter推断用户是否登录
阅读量:4968 次
发布时间:2019-06-12

本文共 2634 字,大约阅读时间需要 8 分钟。

WEB.XML 

userSessionKey
USERSESSIONKEY
rediretPage
/u_member/Login.jsp
uncheckUrls
/u_member/insert.jsp,/u_member/Login.jsp

Filter 过滤器代码

package go.onlineShop.filter;import java.io.IOException;import java.util.Arrays;import java.util.List;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;/** * Servlet Filter implementation class IsLoginFilter * 推断是否登陆过滤器 */@WebFilter("/u_member")public class IsLoginFilter implements Filter {	//从xml中取值	String uncheckUrls =null;	String userSessionKey=null;	String rediretPage=null;    /**     * Default constructor.      */    public IsLoginFilter() {        // TODO Auto-generated constructor stub    }	/**	 * @see Filter#destroy()	 */	public void destroy() {		// TODO Auto-generated method stub	}	/**	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)	 */	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {		// TODO Auto-generated method stub		//1.得到訪问路径		HttpServletRequest request = (HttpServletRequest)req;		HttpServletResponse response = (HttpServletResponse)res;		String reqUrl = request.getRequestURL().toString();		String reqURI = request.getRequestURI();				String servletPath = request.getServletPath();			//	String[] uncheckUrl = uncheckUrls.split(",");		List
urls = Arrays.asList(uncheckUrls.split(",")); //2.假设是属于不须要过滤的地址就直接 放行 方法结束 if(urls.contains(servletPath)){ chain.doFilter(request, response); return; } //3.从session中获取sessionKey相应的值。若不存在 则跳转到登陆页面 Object user = request.getSession().getAttribute(userSessionKey); if(user==null){ request.getRequestDispatcher(rediretPage).forward(request, response); return; } //4若存在 则放行; chain.doFilter(request, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { rediretPage=fConfig.getServletContext().getInitParameter("rediretPage"); uncheckUrls=fConfig.getServletContext().getInitParameter("uncheckUrls"); userSessionKey=fConfig.getServletContext().getInitParameter("userSessionKey"); }}

Login页面



转载于:https://www.cnblogs.com/gcczhongduan/p/5041791.html

你可能感兴趣的文章
AsyncTask类
查看>>
115个Java面试题和答案——终极列表(上)
查看>>
C#编程(四十三)----------Lambda表达式
查看>>
没有dev-server.js文件,如何进行后台数据模拟?
查看>>
eclipse 字体、背景、自动提示设置
查看>>
Android无线测试之—UiAutomator UiScrollable API介绍六
查看>>
Python输入输出练习,运算练习,turtle初步练习
查看>>
第十一篇 logging模块
查看>>
C - Wandering Robot(ZOJ 4115)
查看>>
解决动态连通性——并查集
查看>>
SQLServer之修改视图
查看>>
Java 概述
查看>>
Tsql统计之二(实例详解 之rollup cube)
查看>>
c++中的messagebox()详细用法
查看>>
Javascript绝句欣赏
查看>>
navicat for mysql只导出数据表结构
查看>>
javascript的面向对象编程
查看>>
Python计算pi及其进度条显示
查看>>
News about MapXtreme 2005 for Windows Evaluation (V6.6) Beta 2
查看>>
AlertDialog(自定义对话框)示例
查看>>