用户登录过滤器

开发web项目,一般必须先登录成功才能进入主页,但在浏览器里直接输入主页地址,同样可以直接进入,毫无安全性可言,所以需要登录过滤器来过滤未登录用户到登录页面
package com.*.util.filter;

import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.*.common.model.User;
import com.*.util.config.ProperConfigUtil;

public class LoginFilter extends HttpServlet implements Filter {

	private static final long serialVersionUID = 1L;

	/**
	 * 排除的表达式
	 */
	@SuppressWarnings("serial")
	private static final Set<Pattern> excludeSet = new HashSet<Pattern>() {
		{
			// 用户模块部分
			// add(Pattern.compile("/user_.+\\.do"));

			add(Pattern.compile("/login.do"));
			add(Pattern.compile("/user.do"));
		}
		
	};

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		String sessionId = httpRequest.getParameter("JSESSIONID");
//		boolean blen = httpRequest.isRequestedSessionIdFromURL();
		HttpSession session = httpRequest.getSession();
		sessionId = session.getId();
		@SuppressWarnings("unchecked")
		Map<String, HttpSession> sessions = (Map<String, HttpSession>) httpRequest
				.getSession().getServletContext().getAttribute("sessions");
		String webroot = ProperConfigUtil.getConfigByKey("webroot");
		if(webroot == null){
			webroot = session.getServletContext().getRealPath("");
			ProperConfigUtil.setConfig("webroot", webroot);
		}
		User user = null;
		if (sessionId != null && !"".equals(sessionId)) {
			if (sessions.containsKey(sessionId)) {
				user = (User) sessions.get(sessionId).getAttribute("USER");
			} else {
				httpResponse.sendRedirect(httpRequest.getContextPath()
						+ "/login.do?method=goLogin");
				return;
			}

		} 
		if(user == null) {
			user = (User) session.getAttribute("USER");
		}

		String code = httpRequest.getMethod();
		String url = httpRequest.getRequestURI();
		if (code == null) {
			httpResponse.sendRedirect(httpRequest.getContextPath()
					+ "/login.do?method=goLogin");
			return;
		}

		for (Pattern excludePattern : excludeSet) {
			if (excludePattern.matcher(url).find()) {
				chain.doFilter(request, response);
				return;
			}
		}

		if (user == null || "".equals(user.getUserName())) {
			// 判断获取的路径不为空且不是访问登录页面或执行登录操作时跳转
			if (url != null && !url.equals("") && code.indexOf("Login") < 0
					&& code.indexOf("login") < 0) {
				httpResponse.sendRedirect(httpRequest.getContextPath()
						+ "/login.do?method=goLogin");
				return;
			}
		}
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void destroy() {

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值