简体   繁体   中英

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

We have an application developed many years back using RESTEasy. The implementation uses RestEasy filters, the implementation is very close to the code shown here: RESTEasy ContainerRequestFilter – RESTEasy security filter example I am migrating that application to Spring Boot as we have all the other applications developed using Spring Boot. I am converting the code by taking out JAX-RS and RESTEasy and replacing the RESTEasy filter by Spring Filter something similar to the code shown here: How to Define a Spring Boot Filter? I have the code in the current implementation which checks for annotation on a method like below:

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

I am looking for some way to implement the same method verification logic using POJO or Spring which I didn't seem to find so far. Any help would be greatly appreciated.

Thanks.

IN spring boot you can use Interceptor for the same. In interceptor you will have access to HnadlerMethod from which you can know which service method is getting called and it is in which resource and you can get the weather annotation is present or not.

Steps: create a handler interceptor and register it with spring context so that it will be called. Handler interceptor is getting called after Filter is called. It is similar like in rest easy containerRequestFilter. Also, javax.servlet.Filter is called before containerFilter , here also it is in same way.

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);
}
}`

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM