[英]Spring Boot - Interceptor: Exclude path only in GET request
I am working on a Java Spring project and we wrote an Interceptor for security.我正在研究 Java Spring 项目,我们为安全编写了一个拦截器。 This class implements WebMvcConfigurer, and we override the addInterceptors method:这个 class 实现了 WebMvcConfigurer,我们重写了addInterceptors方法:
@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.现在,路径“/resc”有一个 GET 请求,也有一个 POST 请求。 The point is the POST request must be intercepted and GET request to same path not.关键是 POST 请求必须被拦截,而不是 GET 请求到同一路径。
Is there a way to achieve that?有没有办法做到这一点? Thanks谢谢
The InterceptorRegistration does not provide any methods for your purpose. InterceptorRegistration没有为您的目的提供任何方法。 But I think there is a way to achive your behavior.但我认为有一种方法可以实现你的行为。 You can autowire the ApplicationContext.您可以自动装配 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.例如你的“/resc”你可以用一个额外的if来检查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.