首页>代码>ssm+maven搭建简单java学生信息后台管理系统>/com.student.smm/interceptor/StopLoginInterceptor.java
package interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.bdqn.entity.Employee;


public class StopLoginInterceptor implements HandlerInterceptor {
	
	private List<String> excludeURIS;//白名单:未登录的情况下允许访问
	public List<String> getExcludeURIS() {
		return excludeURIS;
	}

	public void setExcludeURIS(List<String> excludeURIS) {
		this.excludeURIS = excludeURIS;
	}
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		boolean isok = false;
		//1.获得请求的uri
		String baseuri=request.getRequestURI();
		String projecturi = request.getContextPath();
		String uri = baseuri.replace(projecturi, "");
		
		//2.请求URI是否在白名单中,是则放行,否则判断并拦截
		if(this.excludeURIS.contains(uri)){
			isok = true;
		}else{
		//3.判断session内置对象中是否在suserK-V
			HttpSession session = request.getSession();
			Employee users=	session.getAttribute("user")==null?null:(Employee)session.getAttribute("user");
		    if(users != null){
		    	isok = true;
		    }else{
		    	//放置“提示信息”
		    	session.setAttribute("errmsg", "会话异常,请重新登录!");
		    	response.sendRedirect(request.getContextPath()+"/login.jsp");
		    }
		}
			return isok;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}



}
最近下载更多
zhangbo2020  LV6 2023年11月22日
李亮  LV19 2023年8月28日
wddqwe  LV1 2023年6月29日
微信网友_6505997864357888  LV3 2023年6月5日
ruoran7  LV1 2023年5月28日
qiangmin1223  LV11 2023年4月24日
kk1031  LV8 2023年4月13日
764364777  LV2 2023年2月23日
微信网友_6248713511227392  LV11 2022年12月5日
云龙123456  LV7 2022年12月2日
最近浏览更多
minjiang110  LV3 1月3日
zzzzzzz09173 2023年12月27日
暂无贡献等级
WBelong  LV7 2023年12月25日
磊哥哥哥哥  LV13 2023年12月22日
颜菜菜  LV2 2023年12月21日
wwwww816  LV3 2023年12月19日
zhangbo2020  LV6 2023年11月22日
jiemomo  LV12 2023年11月2日
husiyu  LV3 2023年10月4日
李亮  LV19 2023年8月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友