com.ntscorp.notice.interceptor.SessionInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.ntscorp.notice.interceptor.SessionInterceptor.java

Source

/*
 * @(#)LogInterceptor.java 2015. 5. 6.
 *
 * Copyright 2015 NAVER Corp. All rights Reserved. 
 * NAVER PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.ntscorp.notice.interceptor;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.ntscorp.annotation.AuthCheck;

/**
 * @author hyeseong.kwon@nhn.com
 */
public class SessionInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(SessionInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception arg3) throws Exception {

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        logger.info("HomeController   ");

        AuthCheck authCheck = ((HandlerMethod) handler).getMethodAnnotation(AuthCheck.class);

        if (authCheck != null) {
            HttpSession session = request.getSession();
            String userId = (String) session.getAttribute("userId");

            if (userId == null) {
                response.sendRedirect("login/form"); //? userid ?  ?? ? ...
                return false;
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        logger.info("HomeController ??  ");
    }

}