用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月 忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

登陆拦截器LoginInterceptor

2017-03-08 作者:知行合一心 举报

[java]代码库

package com.liang.bike.interceptor;

import com.liang.bike.bean.base.UserBase;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

public class LoginInterceptor implements HandlerInterceptor {
    private Logger logger = Logger.getLogger(LoginInterceptor.class);

    // 身份认证、身份授权
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 判断session
        HttpSession session = request.getSession();
        UserBase user = (UserBase) session.getAttribute("user");
        boolean flag = false;
        if (user != null) {
            flag = true;
        } else {
            // 获取请求的url
            String redirectUrl = request.getRequestURI();
            session.setAttribute("redirectUrl", redirectUrl);
            String bikeNo = request.getParameter("bikeNo");
            session.setAttribute("bikeNo", bikeNo);
            logger.info("请求的URL" + redirectUrl + bikeNo);
            // 执行这里表示用户身份需要认证,跳转登陆页面
            logger.info("跳转到登录页面:login.jsp");
            request.getRequestDispatcher("/WEB-INF/bike/user/login.jsp").forward(request, response);
        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("拦截器postHandle方法执行");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("拦截器afterCompletion方法执行");
    }
}


分享到:
更多

网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。