2

I am working on a Java Spring project and we wrote an Interceptor for security. This class implements WebMvcConfigurer, and we override the addInterceptors method:

@Override
public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecurityInterceptor(aService, sService))
                .excludePathPatterns("/", "/ping", "/resc")
        }

This works nicely. Now, the path "/resc" has a GET request but also a POST request. The point is the POST request must be intercepted and GET request to same path not.

Is there a way to achieve that? Thanks

  • I'm pretty sure this already has an answer: https://stackoverflow.com/a/26848692, you need to replace OPTIONS with GET and the paths with your paths. – PeterS Feb 02 '21 at 08:54

1 Answers1

2

The InterceptorRegistration does not provide any methods for your purpose. But I think there is a way to achive your behavior. You can autowire the ApplicationContext. And than do this inside your Interceptor:

try {
  RequestMappingHandlerMapping req2HandlerMapping = (RequestMappingHandlerMapping)applicationContext.getBean("requestMappingHandlerMapping");
  HandlerExecutionChain handlerExeChain = req2HandlerMapping.getHandler(request);
  if (Objects.nonNull(handlerExeChain)) {
     Method method = ((HandlerMethod) handlerExeChain.getHandler()).getMethod();
     if (!method.isAnnotationPresent(GetMapping.class)) {
       //Provide your Security Checks here.
     }
} catch (Exception e) {
  //provide some Error Code
}

If you also want to chekc a specific path. For example your "/resc" you can check that es well with an additional if.

Tr1monster
  • 328
  • 3
  • 13