[SPRING] WebMvcConfigurationSupport없이 Spring HandlerInterceptor 등록하기
SPRINGWebMvcConfigurationSupport없이 Spring HandlerInterceptor 등록하기
WebMvcConfigurationSupport를 확장하지 않고 Java Config를 사용하여 Spring에서 HandlerInterceptor의 인스턴스를 등록하려고합니다. @Configuration 클래스에 추가 할 때 보안 주석을 처리하는 인터셉터를 등록하는 주석이있는 라이브러리를 만듭니다.
WebMvcConfigurationSupport # addInterceptors를 사용하여 구현했지만 스프링의 다른 자동 작업과 충돌하여 일부 응용 프로그램 자체 논리를 오버로드합니다. 그것은 또한 단순해야하는 무언가를 위해 엄청나게 무거워 보입니다. 나는 지금 노력하고있다 :
@Configuration
public class AnnotationSecurityConfiguration {
@Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping;
@PostConstruct
public void attachInterceptors() {
requestMappingHandlerMapping.setInterceptors(new Object[] {
new SecurityAnnotationHandlerInterceptor()
});
}
}
그러나 인터셉터는 애플리케이션이 웹 요청에 실제로 사용하는 것보다 완전히 다른 RequestMappingHandlerMapping 인스턴스에 등록 된 것처럼 보입니다. 또한 BeanFactoryPostProcessor로 구현 될 때 beanFactory.getBean (RequestMappingHandlerMapping.class)을 시도 할 때 HealthMvcEndpoint에 NullPointerException이 발생합니다.
해결법
-
==============================
1.편집 :이 클래스는 이후로 더 이상 사용되지 않습니다. Spring 5에 대해서는 아래의 @bosco 응답을 참조하십시오.
편집 :이 클래스는 이후로 더 이상 사용되지 않습니다. Spring 5에 대해서는 아래의 @bosco 응답을 참조하십시오.
솔루션을 사용하여 간단히 설명하면 다음과 같습니다.
@Configuration public class AnnotationSecurityConfiguration extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SecurityAnnotationHandlerInterceptor()); } }
스프링 부트에서는 WebMvcConfigurer 유형의 모든 bean이 자동으로 감지되어 MVC 컨텍스트를 수정할 수 있습니다.
-
==============================
2.단지 @ Blauhirn의 주석을 말하는 WebMvcConfigurerAdapter는 버전 5.0에서 더 이상 사용되지 않습니다.
단지 @ Blauhirn의 주석을 말하는 WebMvcConfigurerAdapter는 버전 5.0에서 더 이상 사용되지 않습니다.
새 방법을 참조하십시오.
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyCustomInterceptor()) // Optional .addPathPatterns("/myendpoint"); } }
또한, 여기에 언급 된 바와 같이 @EnableWebMvc에 주석을 달지 마십시오. MVC 용 스프링 부트 자동 구성을 유지하려는 경우입니다.
from https://stackoverflow.com/questions/28013671/register-spring-handlerinterceptor-without-webmvcconfigurationsupport by cc-by-sa and MIT license
'SPRING' 카테고리의 다른 글
[SPRING] JNDI에서 얻은 데이터 소스를 닫아야합니까? (0) | 2019.04.19 |
---|---|
[SPRING] 스프링 데이터 MongoDB와 함께 낙관적 인 잠금을 사용하는 방법? (0) | 2019.04.19 |
[SPRING] @Valid 유효성 검사에서 일부 필드 제외 (0) | 2019.04.19 |
[SPRING] 숫자와 다음, 이전의 Jpa 페이징 (0) | 2019.04.19 |
[SPRING] App Engine에서 Spring AOP를 사용하면 StackOverflowError가 발생합니다. (0) | 2019.04.19 |