일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 최종 프로젝트
- JPA
- 인텔리제이
- REST API 규칙
- ERD 작성
- spring서버
- 스프링 부트 공식 문서
- 1차캐시
- queryDSL
- @IdClass
- JoinColumn
- Q 클래스
- 스프링부트오류
- REST란
- 복합키
- Unsatisfied dependency
- uncheck Exception
- jpa에러
- Error creating bean with name
- Spring Spring boot 차이
- 빈생성안됨
- github
- jpa회원가입
- git
- JPA주의사항
- 스프링 부트 기능
- json gson 차이
- json
- Filter
- jwt메서드
- Today
- Total
Everyday Dev System
스프링부트로 인터셉트 활용하기 본문
- InterCeptor란?
- Interceptor 활용하기
먼저, Interceptor가 무엇인지 알아야 합니다.
1. 인터셉터란?
컨트롤러와 핸들러를 호출하기 전과 후에 요청과 응답을 참조하거나 가공하는 역할을 합니다.
'낚아채다' 라는 의미로, 사용자 요청에 의해 서버에 들어온 Request 객체를 컨트롤러의 핸들러로 도달하기
전에 낚아채서 개발자가 원하는 추가 작업을 한 후 핸들러로 보낼 수 있도록 돕습니다.
예로 로그인체크 혹은 권한 체크 등을 할 수 있습니다.
특정 컨트롤러 url에 매핑되어 인터센터를 지정할 수 있습니다. 또한, 장점으로는 여러 url에 컨트롤러 접근 이전에 전처리가 필요할 경우에 코드의 중복을 최소화할 수 있습니다.
2. 인터셉터 활용 예제
간단한 예시를 아래를 통해 보도록 하겠습니다.
먼저, HandlerInterceptor를 상속받아 인터셉터 클래스를 커스튬합니다.
테스트를 위해 단순히 로그만 찍어 실행되는지만 확인하도록 하겠습니다.
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
@Slf4j
public class HandleInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.info("preHandler");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
log.info("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
log.info("afterCompletion");
}
}
WebMvcConfigurer를 상속하여 구현체를 만든 후 커스튬한 인터셉터를 Bean으로 등록합니다.
import com.sparta.myselectshop.HandleInterceptor;
import org.springframework.context.annotation.Bean;
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 {
@Bean
public HandleInterceptor handleInterceptor() {
return new HandleInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(handleInterceptor()).addPathPatterns("/api/user/signup");
}
}
addInterceptors() 메서드 내에 url 패턴과 일치하면 아래와 같이 출력되는 것을 볼 수 있습니다.
references :
https://popo015.tistory.com/115
[Spring] 스프링 인터셉터(Interceptor)란 ?
목표 Interceptor 란 무엇인지 알아본다. Interceptor 를 직접 구현해본다. 순서 1. 인터셉터(Interceptor) 1.1 인터셉터란? 1.2 왜 사용하는가? 1.3 구현수단 1.4 어떤 메서드를 가지고 있는가? 2. 인터셉터 동작
popo015.tistory.com
'나의 호기심' 카테고리의 다른 글
MariaDB 도입 배경 (2) | 2023.11.11 |
---|---|
JNI 사용법과 예제 코드 (0) | 2023.10.10 |
System.arraycopy 메서드가 native인 이유 (0) | 2023.09.30 |
JNI란? (0) | 2023.09.30 |
Java에서 Exception 뜯어보기 (0) | 2023.08.04 |