본문 바로가기
Spring

[Spring] Interceptor 로그인 처리

by 2do0 2022. 6. 8.
반응형

[Spring] Interceptor 로그인 처리

// HandlerInterceptor를 상속받은 HttpInterceptor 클래스 생성

@Component
public class HttpInterceptor implements HandlerInterceptor {
    /**
     * 세션에 LOGIN_ID 값 체크
     * @throws IOException
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws IOException {
        HttpSession session = request.getSession();
        if (session.getAttribute("LOGIN_ID") == null)
        {
        	response.sendRedirect("/login.do"); // Session의 LOGIN_ID가 없으면 로그인으로 튕김
            return false;
        }
        return true;
    }
}

 

// WebMvcConfigurer를 상속받은 CommonMvcSetting 클래스 생성

@Configuration
public class CommonMvcSetting implements WebMvcConfigurer {
	 /**
     * 인터셉터 설정
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(httpInterceptor).excludePathPatterns("/resources/**", "/login.do", "/loginCheck.do");
        // excludePathPatterns를 이용해 url을 인터셉터에서 제외 시킴
        // 제외된 url말고는 Session에 LOGIN_ID가 없다면 /login.do로 튕김
    }
}
반응형

댓글