- N 대의 서버가 동작하는 상황에서 서버의 세션은 공유가 불가능하다.
- 데이터베이스를 세션처럼 사용하여 서버를 운영해보자.
- Redis
mvn spring-boot:run -Dspring-boot.run.profiles=redis
- RDB (MySQL)
mvn spring-boot:run -Dspring-boot.run.profiles=rdb
public interface SessionAspect {
Object session(ProceedingJoinPoint pjp) throws Throwable;
}
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SessionContext {
private static final ThreadLocal<Optional<String>> threadLocal = new ThreadLocal<>();
public static void set(String sessionId) {
threadLocal.set(Optional.ofNullable(sessionId));
}
public static Optional<String> get() {
return Optional.ofNullable(threadLocal.get())
.orElse(Optional.empty());
}
public static void remove() {
threadLocal.remove();
}
}
public class CookieInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
SessionContext.set(getSessionId(request.getCookies()));
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
SessionContext.remove();
}
}