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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.