Filter登录检查

登录检查

未登录时,无论什么页面,都重定向到 login.html 。添加 LoginFilter ,拦截路径为 *.html。

@WebFilter("*.html")
public class LoginFilter implements Filter
{
	public void init(FilterConfig fConfig) throws ServletException
	{
		// TODO Auto-generated method stub
	}

	public void destroy()
	{
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException
	{
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse) response;
		
		System.out.println("** LoginFilter: " + req.getServletPath());
		
		// login.html可以直接访问,不可过滤
		String servletPath=req.getServletPath();
		if(servletPath.equals("/login.html"))
		{
			chain.doFilter(request, response);
			return;
		}
		
		//其他页面需要登陆才可以访问
		Admin admin=null;
		HttpSession session=req.getSession();
		admin=(Admin) session.getAttribute("admin");
		if(admin==null)
		{
			// 重定向到 /login.html
			String contextPath=req.getContextPath();
			resp.sendRedirect(contextPath+"/login.html");
			return;
		}
		
		// 调用 chain.doFilter()表示 '通过'
		chain.doFilter(request, response);
	}

}

代码分析: LoginFilter.doFilter()

取得当前访问路径 req. getServletPath() ,如果是 login.html,直接放行 。如果是其他页面,检查是否登录。如未登录,则跳转到 login.html 。

注意: login.html 本身不要被过滤拦截 ; 页面重定向 resp.sendRedirect( )要加上 ContextPath

HTTP 302 重定向

页面重定向 response.sendRedirect( nextUrl )

  • sendRedirect( nextUrl ) 时,nextUrl 应是绝对地址,前面要加上 contextPath前缀。
  • 302 页面重定向,当浏览器收到这个应答后,会立即发起一个新的请求来下载新的目标页面。

本地自动登录

添加一个过滤器,实现服务器本机访问时免登录,自动以admin身份访问。由于是双重过滤器,要求 LocalAccessFilter 必须在 LoginFilter 之前运行。其实也可以直接修改 LoginFilter ,在里面判断客户端IP是否为127.0.0.1。

这是一种基于客户端IP的验证技术。

/**
 * ip 127.0.0.1 免登录
 */
@WebFilter({ "/AdminInfoJS", "/index.html" })
public class LocalAccessFilter implements Filter
{
	public void init(FilterConfig fConfig) throws ServletException
	{
		// TODO Auto-generated method stub
	}

	public void destroy()
	{
		// TODO Auto-generated method stub
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException
	{
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse) response;
		
		System.out.println("** LocalAccessFilter: " + req.getServletPath());
		
		String clientIP=req.getRemoteAddr();
		if(clientIP.equals("127.0.0.1"))
		{
			System.out.println("本地访问, 自动以 admin登录 ...");
			
			Admin admin=null;
			HttpSession session=req.getSession();
			admin=(Admin) session.getAttribute("admin");
			if(admin==null)
			{
				admin = getAdmin(); // 从数据库中取得
				session.setAttribute("admin", admin);
			}
		}
		// 调用 chain.doFilter()表示 '通过'
		chain.doFilter(request, response);
	}

	private Admin getAdmin()
	{
		Admin row=null;
		try (SqlSession sqlSession = db.MyBatis.sqlSessionFactory.openSession())
		{
			row = sqlSession.selectOne("ls.test.getAdmin", "admin");
		}
		return row;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值