플랫폼엘 정리or해석
커스텀 어노테이션 적용후,인터셉터에 적용하기
by shulk
2024. 7. 12.
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface LoginRequiredType {
LoginType value() default LoginType.ADMIN;
public enum LoginType {
ADMIN, // GHS 연동을 위한 로그인이 필요
BOOTH // AAF 기능을 쓰기 위한 로그인 필요 - ex ) facebook
}
}
@Component
public class NeedLoginInterceptor implements HandlerInterceptor {
//public class NeedLoginInterceptor extends AnnotationBasedHandlerInterceptorAdapter<LoginRequiredType> {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//어노테이션 체크 - Controller에 @Auth 어노테이션이 있는지 확인
if (!(handler instanceof HandlerMethod)) {
return true;
}
HttpSession session = request.getSession();
boolean isLogin = handleIfIsLogin(session);
boolean isExpired = handleIfIsExpired(session);
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (null != handlerMethod.getMethodAnnotation(LoginRequiredType.class) || null != handlerMethod.getBeanType().getAnnotation(LoginRequiredType.class)) {
if (!isLogin) {//1. 비로그인 유저
response.sendRedirect("/login");
} else {
if (isExpired)//2. 로그인 유저이지만 타임아웃
{
StringBuffer redirectUrl = request.getRequestURL();
session.removeAttribute(Const.ATTR_LOGIN_MEMBER);
session.removeAttribute(Const.ATTR_LOGIN_TYPE);
response.sendRedirect("/login?redirectUrl=" + URLEncoder.encode(redirectUrl.toString(), "utf-8"));
}
}
}
return true;
}
private boolean handleIfIsLogin(HttpSession session) {
if ( session.getAttribute(Const.ATTR_LOGIN_MEMBER) == null ){
return false;
}
return true;
}
private boolean handleIfIsExpired(HttpSession session) {
long last_access_dttm = session.getLastAccessedTime();
Date now = new Date();
Date expired_dttm = new Date(last_access_dttm+Const.EXPIRED_LONG_TIME);
if (expired_dttm.after(now)){
return false;
}
return true;
}
}