interceptor

This commit is contained in:
root
2025-09-15 13:01:59 +09:00
parent ec2201278f
commit 030d3c4ceb
3 changed files with 56 additions and 1 deletions

View File

@@ -0,0 +1,37 @@
package site.ocr.prd;
import java.util.Enumeration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@Component
public class SessionLoggingInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
//현재 세션이 있으면 가져오기, 없으면 null
HttpSession sessoin = request.getSession(false);
if (sessoin != null) {
StringBuilder sb = new StringBuilder("[SESSION DEBUG] :: ");
//세션ID(누구 세션인지)
sb.append("ID=").append(sessoin.getId()).append("\\n");
//세션값
Enumeration<String> attrvName = sessoin.getAttributeNames();
while (attrvName.hasMoreElements()) {
String name = attrvName.nextElement();
String value = (String) sessoin.getAttribute(name);
sb.append("NAME=").append(name).append(", ").append("VALUE=").append(value).append("\\n");
}
System.out.println(sb.toString());
} else {
System.out.println("[SESSION DEBUG] :: No session found.");
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}

View File

@@ -2,7 +2,6 @@ package site.ocr.prd;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
@Configuration

View File

@@ -0,0 +1,19 @@
package site.ocr.prd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Autowired
private SessionLoggingInterceptor sessionLoggingInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
WebMvcConfigurer.super.addInterceptors(registry);
registry.addInterceptor(sessionLoggingInterceptor).addPathPatterns("/**");
}
}