728x90
Interceptor
인터셉터는 클라이언트로부터 서버로 들어온 요청(request) 객체를 컨트롤러(controller)의 핸들러(handler)로 도달하기 전에 가로채는 기능을 수행한다.
이를 통해 원하는 추가 작업이나 로직을 수행한 후 핸들러로 요청을 보낼 수 있도록 해주는 모듈이다.
일반적으로 핸들러(주로 @Controller로 표시됨)가 실행되기 전에 인터셉터가 먼저 실행되고 인터셉터를 통해 요청에 대해 원하는 작업이나 로직을 수행한 후 컨트롤러로 요청 객체를 전달한다.
인터셉터는 로그인, 권한 체크, 헤더나 로그인 세션 검증, API 토큰 검증 등의 작업에 사용된다.
cf) 로그인을 한 사용자가 아닌 상태에서 '개인정보페이지'를 요청하면 개인정보 페이지 대신 로그인 페이지 등으로 이동하도록 처리할 수 있다.
Intercpetor 1~8
(내용이 중복되므로 2~8 생략)
package kr.bit.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class Inter1 implements HandlerInterceptor{
//요청 발생 시 호출되는 메서드 코드가 중복되는 부분이 있을 때 인터셉터를 통해 처리한다
//로그인여부 확인, 등급별 서비스 권한, 관리자/회원 작업처리에서 많이 사용
//controller 메서드가 호출되기 전에 호출됨
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("pre-inter1");
return true;
}
//controller 메서드가 호출된 이후에 호출됨(view처리 수행 전에 호출됨)
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("post-inter1");
}
//view처리 끝나고 응답결과가 브라우저로 전달되기 전에 호출됨
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) throws Exception {
System.out.println("afterCompletion-inter1");
}
}
ServletAppContext
package kr.bit.config;
import kr.bit.interceptor.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.config.annotation.*;
@Configuration
@EnableWebMvc
@ComponentScan("kr.bit.controller")
public class ServletAppContext implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
WebMvcConfigurer.super.configureViewResolvers(registry);
registry.jsp("/WEB-INF/views/", ".jsp");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
WebMvcConfigurer.super.addResourceHandlers(registry);
registry.addResourceHandler("/**").addResourceLocations("/resources/");
}
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource res = new ReloadableResourceBundleMessageSource();
res.setBasename("/WEB-INF/properties/error");
return res;
}
//인터셉터 등록- 경로와 인터셉터를 설정하기 위해
public void addInterceptors(InterceptorRegistry reg) {
WebMvcConfigurer.super.addInterceptors(reg);
Inter1 i1=new Inter1();
Inter2 i2=new Inter2();
Inter3 i3=new Inter3();
Inter4 i4=new Inter4();
Inter5 i5=new Inter5();
Inter6 i6=new Inter6();
Inter7 i7=new Inter7();
Inter8 i8=new Inter8();
InterceptorRegistration r1= reg.addInterceptor(i1);
InterceptorRegistration r2= reg.addInterceptor(i2);
InterceptorRegistration r3= reg.addInterceptor(i3);
InterceptorRegistration r4= reg.addInterceptor(i4);
InterceptorRegistration r5= reg.addInterceptor(i5);
InterceptorRegistration r6= reg.addInterceptor(i6);
InterceptorRegistration r7= reg.addInterceptor(i7);
InterceptorRegistration r8= reg.addInterceptor(i8);
r1.addPathPatterns("/t1");//interceptor가 가로채 갈 주소를 등록
r2.addPathPatterns("/t1");
r3.addPathPatterns("/t2");
r4.addPathPatterns("/t1","/t2");
r5.addPathPatterns("/s1/t3", "/s1/t4");
r6.addPathPatterns("/*"); //t1,/t2
r7.addPathPatterns("/s1/*"); //s1을 시작되어지는 하나의 경로 /s1/t3, /s1/t4
r8.addPathPatterns("/**"); // /s1/t3, /s1/t4
r8.excludePathPatterns("/*");//interceptor가 가로채가지 않을 주소를 등록 (/t1,/t2)
}
}
728x90
'Frameworks > Spring' 카테고리의 다른 글
[Spring] MVC (Restcontroller) (2) | 2024.05.13 |
---|---|
[Spring] MVC (DB Setting with MyBatis) (2) | 2024.05.12 |
[Spring] Validator Customizing (0) | 2024.05.10 |
[Spring] Type of JSR Annotations (0) | 2024.05.10 |
[Spring] MVC (FormTag - path (Radiobutton Checkbox etc..) (1) | 2024.05.09 |