📖自定义filter过滤器拦截未登录(非法)请求

发布: 2018-03-16
热度: 48
趋势: 48
权重: 0
🎯

在JAVA的WEB工程中我们可以将JSP页面文件放在WEB-INFO中限制用户进行URL直接访问,但静态资源如js、css文件却是需要被外部直接访问的,直接对外暴露又不太安全,可以通过自定义过滤器处理

关于 filter 过滤器

filter 过滤器,用于过滤到 servlet 的 request,它可以改变一个 request 和修改一个 response。

filter 不是 servlet,不能产生 response。

filter 能够在一个 request 到达 servlet 之前预处理 request,也可以在离开 servlet 时处理 response。

为什么使用 filter

WEB 工程中有些页面本身是对外开放的,但是我们实际上又会要求登录才可见。

虽然 WEB-INFO 目录可以禁止页面直接访问资源(WEB-INFO 目录只能通过 servlet 去访问)。

如果我们将资源页面文件放在 WEB-INFO 下也可以达到无法访问,但是这就对我们前端编码造成很大的困扰。

WEB-INF 是受保护目录,WEB-INF 里面的文件只可以由 servlet 去访问,不能通过 url 地址栏去请求访问。

所以,我们应该通过别的方式来动态判断处理资源文件是否应该被访问

#web.xml 配置

一个登录状态检查校验器,检查所有的向 jsp 文件发起的请求,并排除登录/登出/首页的访问拦截。

<filter>
	<filter-name>checkLoginStatusFilter</filter-name>
	<filter-class>com.mebugs.filter.CheckLoginStatusFilter</filter-class>
	<init-param>
		<param-name>exceptPath</param-name>
		<param-value>login.jsp;logout.jsp;index.jsp</param-value>
	</init-param>
	<filter-mapping>
		<filter-name>checkLoginStatusFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
</filter>

自定义过滤器类

依赖于服务端保存了登录后新生成的 sessionID,且服务端需要实现超时注销和登出注销 session 的能力。

/*
 * 文件名:CheckLoginStatusFilter.java
 * 描述:检查是否处于登录状态
 * check the request sessionID login status
 * @author   mebugs
 * @version  1.0
 */
public class CheckLoginStatusFilter implements Filter{
	//定义除外的访问PATH
	private String exceptPath;

	//初始化参数,获取web.xml中配置的忽略校验地址
	public void init(FilterConfig config) throws ServletException     { 
		this.exceptPath = config.getInitParameter("exceptPath");
	}

	public void destroy(){
		//销毁方法
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
		HttpServletRequest httpRequest = null;
		if (request instanceof HttpServletRequest){
			httpRequest = (HttpServletRequest)request;
		}
		HttpServletResponse httpResponse = null; 
		if (response instanceof HttpServletResponse){
			httpResponse = (HttpServletResponse)response;
		} 
		if (null == httpRequest || null == httpResponse) {
			return;
		} 
		String requestURI = httpRequest.getRequestURI();
		String contextPath = httpRequest.getContextPath();
		String sessionID = httpRequest.getSession().getId();
		String userName = null;
		//Constants.SEMICOLON分号,针对配置多种排除的地址的时候
		String[] exceptPathList =  this.exceptPath.split(Constants.SEMICOLON);
		for (int i = 0; i < exceptPathList.length; i++){
			if (requestURI.endsWith(exceptPathList[i]){
				//如果请求是忽略的地址
				chain.doFilter(request, response);
				return;
			}
		}
		if(null != sessionID){
			userName = (String) httpRequest.getSession().getAttribute("username");
		}
		if (null != userName){
			//检查浏览器sessionID与服务器中缓存的登录后sessionID是否一致
			if(sessionID.equals(UserSessionData.sessionMap.get(userName)){
				//登录状态 sessionID一致
				chain.doFilter(request, response);
				return;
			}
		}
	
		//其他场景通通非法
		httpResponse.setStatus(911);
		httpResponse.setHeader("sessionstatus", "timeout");
		httpResponse.addHeader("loginPath", contextPath + "/login.jsp");
		httpResponse.setCharacterEncoding("UTF-8");
		httpResponse.setContentType("text/html");
		PrintWriter out = httpResponse.getWriter();
		out.println("<script>");
		out.println("top.location.href = '" + contextPath + "/login.jsp'");
		out.println("</script>");
		return;
	}
}

结尾小结

通过自定义过滤器可以更加人性化的对客户端发起的请求进行过滤。

对于资源请求限制为已登录用户才能访问,这样可以避免绝大多数非法攻击。

当前文章暂无讨论,留下脚印吧!
大纲
  • 关于 filter 过滤器
  • 为什么使用 filter
  • 自定义过滤器类
  • 结尾小结
提交成功,请等待审核通过后全面展示!

发表评论

昵称
邮箱
链接
签名
评论

温馨提示:系统将通过浏览器临时记忆您曾经填写的个人信息且支持修改,评论提交后仅自己可见,内容需要经过审核后方可全面展示。

选择头像