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;
	}
}
最近下载更多
Rubiai  LV1 2024年12月26日
Administration620  LV1 2024年12月9日
111rose  LV1 2024年11月22日
star33xxz  LV1 2024年6月28日
2032867896  LV1 2024年6月20日
hongdongdong  LV14 2024年6月18日
卢旯旯  LV4 2024年6月10日
yqlllll  LV2 2024年6月4日
minjiang110  LV3 2024年1月3日
wwwww816  LV5 2023年12月19日
最近浏览更多
甜心冰淇淋  LV4 6月15日
shhhhhh 6月13日
暂无贡献等级
llxxyy 6月9日
暂无贡献等级
段池卿  LV4 5月23日
tomsrt 4月22日
暂无贡献等级
xiaoaitx  LV8 1月1日
ma406805131  LV19 2024年12月26日
xiuele 2024年12月26日
暂无贡献等级
Rubiai  LV1 2024年12月26日
Administration620  LV1 2024年12月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友