티스토리 뷰

Interceptor에 오류페이지를 타지 않게 하는 방법은 간단하다.

다만 이 방법은 오류페이지에 대한 컨트롤러를 따로 사용 할 때에만 유용한 방법 같다

 

package hello.exception;

import hello.exception.filter.LogFilter;
import hello.exception.interceptor.LogInterceptor;
import jakarta.servlet.DispatcherType;
import jakarta.servlet.Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
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 WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogInterceptor())
                .order(1)
                .addPathPatterns("/**")
                .excludePathPatterns("/css/*","*.ico","/error","/error-page/**"); //Filter와 다르게 DispatcherType을 넣을 수 없다.
                // 대신 excludePathPatterns 이라는 강력한 메소드가 있어서 error-page 관련을 빼면 된다.

    }

    //@Bean
    public FilterRegistrationBean logFilter(){
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new LogFilter());
        filterRegistrationBean.setOrder(1);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST,DispatcherType.ERROR); //요청과 에러일 경우에만 호출된다.

        return filterRegistrationBean;
    }

}

 

registry 객체에 에러 페이지 url에 해당하는 (/error-page/**) 모든 페이지에 대해 인터셉터를 타지 않게 제한 하면 끝이다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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 29 30 31
글 보관함