반응형
[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로 튕김
}
}
반응형
'Spring' 카테고리의 다른 글
[Spring]Data truncation: Truncated incorrect date value 에러 (0) | 2022.07.14 |
---|---|
[Spring] logback 특정 로그 필터해서 안보이게 하기 (0) | 2022.06.14 |
[Spring] 디버그 실행 시 SilentExitException()에 멈춤 (0) | 2022.06.08 |
[Spring] JSTL <c:if></c:if> 사용법 (0) | 2022.04.20 |
[Spring] mybatis에서 selectKey (0) | 2022.04.12 |
댓글