如何用 Spring 或 POJO 类 替换 org.jboss.resteasy.core.ResourceMethodInvoker

How to replace org.jboss.resteasy.core.ResourceMethodInvoker with Spring or POJO classes

我们有一个多年前使用 RESTEasy 开发的应用程序。实现使用 RestEasy 过滤器,实现非常接近此处显示的代码: RESTEasy ContainerRequestFilter – RESTEasy security filter example 我正在将该应用程序迁移到 Spring Boot,因为我们已经使用 Spring Boot 开发了所有其他应用程序。我通过删除 JAX-RS 和 RESTEasy 并将 RESTEasy 过滤器替换为 Spring 过滤器来转换代码,类似于此处显示的代码: How to Define a Spring Boot Filter? 我在当前实现中有代码检查如下方法的注释:

ResourceMethodInvoker methodInvoker = (ResourceMethodInvoker) requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
Method method = methodInvoker.getMethod();
if(!method.isAnnotationPresent(PermitAll.class))
{
    doSomething();
}

我正在寻找一些方法来使用 POJO 或 Spring 实现相同的方法验证逻辑,目前我似乎还没有找到。任何帮助将不胜感激。

谢谢。

IN spring 引导你可以使用拦截器。在拦截器中,您将有权访问 HnadlerMethod,从中您可以知道正在调用哪个服务方法以及它在哪个资源中,您还可以获取是否存在天气注释。

步骤:创建处理程序拦截器并将其注册到 spring 上下文,以便调用它。在调用 Filter 之后调用处理程序拦截器。它类似于 rest easy containerRequestFilter。还有,javax.servlet.Filter是在containerFilter之前调用的,这里也是一样的。

Link :https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html`

@Configuration
public class HandlerIntercepter extends HandlerInterceptorAdapter
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler) throws Exception
{
    System.out.println("handler called");
    HandlerMethod handlerMethod = (HandlerMethod) handler;
    Class<?> clazz = handlerMethod.getBeanType();
    Method m = handlerMethod.getMethod();
    if (clazz != null)
    {
        boolean isClzAnnotation = 
clazz.isAnnotationPresent(RequireSignInClassLevel.class);
    }
    if (m != null)
    {
        boolean isMethondAnnotation = 
m.isAnnotationPresent(RequireSignIn.class);
    }
    return true;
}}
@Component
public class AppConfig extends WebMvcConfigurerAdapter
{
@Autowired
HandlerIntercepter HandlerIntercepter;

@Override
public void addInterceptors(InterceptorRegistry registry)
{
    registry.addInterceptor(HandlerIntercepter);
}
}`