package com.yyx.interceptor;

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 com.yyx.po.User;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void postHandle(HttpServletRequest request, 
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}
	@Override
	public void afterCompletion(HttpServletRequest request, 
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest request, 
		HttpServletResponse response, Object handler) throws Exception {
	    // 获取请求的URL  
        String url = request.getRequestURI();  
        // URL:除了login.jsp是可以公开访问的,其它的URL都进行拦截控制  
        if(url.indexOf("/login")>=0){  
            return true;  
        }  
        // 获取Session  
        HttpSession session = request.getSession();  
        User user = (User) session.getAttribute("USER_SESSION");
        // 判断Session中是否有用户数据,如果有,则返回true,继续向下执行
        if(user != null){
            return true;  
        }
         // 不符合条件的给出提示信息,并转发到登录页面
         request.setAttribute("msg", "您还没有登录,请先登录!");
         request.getRequestDispatcher("/WEB-INF/jsp/login.jsp")
                                                .forward(request, response);
		return false;
	}
}
最近下载更多
minjiang110  LV3 1月3日
wwwww816  LV3 2023年12月19日
sunshine9920  LV12 2023年11月7日
李亮  LV19 2023年8月28日
gecongkai  LV8 2023年6月22日
hedyyy  LV3 2023年6月16日
短巷拥猫  LV11 2023年6月15日
微信网友_6505997864357888  LV3 2023年6月5日
bismarcktirppitz  LV2 2023年6月4日
hxjhxjlhw  LV1 2023年5月30日
最近浏览更多
幸福一家人 4月23日
暂无贡献等级
alphaeix 3月18日
暂无贡献等级
1437047954 3月7日
暂无贡献等级
minjing123  LV8 1月4日
minjiang110  LV3 1月3日
Nuyoah_L 1月3日
暂无贡献等级
zzzzzzz09173 2023年12月27日
暂无贡献等级
潘潘123456  LV2 2023年12月24日
LiBaQi  LV2 2023年12月22日
磊哥哥哥哥  LV13 2023年12月22日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友