简体   繁体   English

如何使用Java为Web服务中的每个API调用特定方法?

[英]How to invoke a specific method for each API in a web-service in Java?

I have a web-service which provides dozens of APIs. 我有一个提供数十种API的网络服务。 I want to let each of them do something once they've be invoked automatically, like checking the authorization, without invoking manually. 我想让他们每个人在自动调用后就执行某些操作,例如检查授权,而无需手动调用。

For example, I got 3 APIs like described below : 例如,我得到了3个如下所述的API:

@POST
public Response postSome() {
   checkAuthorization();
   ...
}

@GET
public Response getSome() {
   checkAuthorization();
   ...
}

@DELETE
public Response deleteSome() {
   checkAuthorization();
   ...
}

Of course, I could invoke checkAuthorization() method in each of my APIs, but it was quiet troublesome, redundant, and unsafe, because me or other writer might forget doing this. 当然,我可以在每个API中调用checkAuthorization()方法,但是这样做很麻烦,冗长且不安全,因为我或其他编写者可能会忘记这样做。

Is there any method(eg inheritance ) or other way could achieve this, so I don't have to call checkAuthorization() every time I created a new API? 是否有任何方法(例如继承 )或其他方法可以实现此目的,所以我不必在每次创建新的API时都调用checkAuthorization()吗? Thanks. 谢谢。

You could achieve this using Interceptor. 您可以使用Interceptor来实现。

Spring allow you to intercept web request through handler interceptors. Spring允许您通过处理程序拦截器拦截Web请求。 The handler interceptor have to implement the HandlerInterceptor interface, which contains three methods : 处理程序拦截器必须实现HandlerInterceptor接口,该接口包含三种方法:

preHandle() – Called before the handler execution, returns a boolean value, “true” : continue the handler execution chain; preHandle() –在处理程序执行之前调用,返回一个布尔值“ true”:继续处理程序执行链; “false”, stop the execution chain and return it. “ false”,停止执行链并返回它。

postHandle() – Called after the handler execution, allow manipulate the ModelAndView object before render it to view page. postHandle() –在处理程序执行之后调用,允许在渲染ModelAndView对象以查看页面之前对其进行操作。

afterCompletion() – Called after the complete request has finished. afterCompletion() –在完成请求之后调用。 Seldom use, cant find any use case. 很少使用,找不到任何用例。

public class AuthenticationInterceptor extends HandlerInterceptorAdapter {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception)
    throws Exception {
    // TODO Auto-generated method stub

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
    throws Exception {
    // TODO Auto-generated method stub

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     checkAuthorization()
    }


}

Implement an AppConfig class or add the addInterceptors in one of the existing Configuration class. 实现AppConfig类或在现有Configuration类之一中添加addInterceptors。

@Configuration  
public class AppConfig extends WebMvcConfigurerAdapter  {  

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new AuthenticationInterceptor ());
    }
} 

Write your checkAuthorization() logic in preHandle() . 写下您的checkAuthorization()逻辑preHandle() Hope this helps. 希望这可以帮助。

You can check authorization on different levels 您可以检查不同级别的授权

You can create a method level annotation calling the checkAuthorization method, then you just annotate every method. 您可以创建一个调用checkAuthorization方法的方法级别注释,然后仅注释每个方法。 It may result in cleaner code, but the same way you need not to forget annotate your methods. 这可能会导致代码更简洁,但是您不必忘记对方法进行注释即可。

You can create a web service interceptor - a piece of code executed for each incoming request, but the implementation will depend on a used framework 您可以创建一个Web服务拦截器-针对每个传入请求执行的一段代码,但是实现将取决于所使用的框架

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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