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


public class loginFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		 HttpServletRequest req = (HttpServletRequest)request;
	        HttpServletResponse resp =(HttpServletResponse) response;
	        HttpSession session = req.getSession();
	           // 获得用户请求的URI
	        String path = req.getRequestURI();
	        // 从session取得已经登录验证的凭证 我这里的demo用的是password来作为登录凭证
	        String adminName = (String) session.getAttribute("adminName");
	        // login.jsp页面无需过滤(根据自己项目的要求来)
	        if(path.indexOf("adminGoMain") == -1) {//注意:登录页面千万不能过滤  不然过滤器就。。。。。自行调试不要偷懒!这样记忆深刻
	        	chain.doFilter(req, resp);
	            return;
	        } else if(path.indexOf("adminGoMain") > -1){//如果不是login.jsp进行过滤
	            if (adminName == null || "".equals(adminName)) {
	            	resp.sendRedirect("adminLogin");
	            } else {
	                // 已经登陆,继续此次请求
	            	chain.doFilter(req, resp);
	            }
	        }
		
	}

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

}
最近下载更多
yykkzzll  LV4 4月24日
卡布琪洛  LV4 2月26日
mds123456  LV2 2023年11月5日
wangyan111111  LV8 2023年10月9日
deluser  LV3 2023年9月7日
哇塞塞哈哈哈  LV8 2023年5月4日
yang886  LV21 2023年3月22日
2194399475  LV1 2023年3月15日
weiili  LV1 2023年3月9日
骑着导弹让蜗牛追去  LV8 2023年2月13日
最近浏览更多
yykkzzll  LV4 4月24日
ming_123_9715  LV23 3月13日
admin_z  LV22 1月29日
lanyue  LV3 1月10日
Gin19960217  LV4 1月5日
lilong007  LV20 2023年12月30日
mds123456  LV2 2023年11月5日
wangyan111111  LV8 2023年10月9日
有可能是今天  LV1 2023年9月22日
微信网友_6657940693880832  LV1 2023年9月21日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友