EasyOpen started to support interceptors in version 1.3.1.
The EasyOpen Interceptor implementation principle is similar to the Springmvc Interceptor, where the interceptor acts on the API method, that is, the @api annotation method.
The interceptor is defined as follows:
/** * Interceptor, principle with SPRINGMVC interceptor * @author TANGHC * */public interface Apiinterceptor {/** * preprocessing callback method, executed before method call * @param Request * @param response * @param serviceobj service class * @param Argu method Parameters * @return * Throws Exception */boolean Prehandle (HttpServletRequest request, httpservletresponse response, Object serviceobj
, Object Argu) throws Exception;
This method is called after the/** * interface method finishes executing. * @param Request * @param response * @param serviceobj service class * @param argu parameter * @param The result method returns a knot Fruit * @throws Exception */void Posthandle (HttpServletRequest request, httpservletresponse response, Object SE
Rviceobj, Object Argu, object result) throws Exception;
/** * Results after package execution * @param request * @param response * @param serviceobj service class * @param argu parameters * @param result is wrapped * @param e * @throws Exception */void Aftercompletion (HttpservLetrequest request, HttpServletResponse Response, Object Serviceobj, Object Argu, object result, Exception e)
Throws Exception;
/** * Match Interceptor * @param Apimeta Interface Information * @return */boolean match (Apimeta Apimeta); }
An adapter Apiinterceptoradapter Interceptor execution process is also provided:
Same as the SPRINGMVC interceptor execution Process Prehandle If False is returned, the API method is not called, and then aftercompletion is called in reverse order, which needs to be returned by response custom if Prehandle returns True. Proceed to the next Prehandle prehandle execution is complete, reverse execution posthandle
The final reverse call aftercompletion the normal flow:
Apiinterceptor1.prehandle
apiinterceptor2.prehandle
apimethod.invoke ()//API method call
Apiinterceptor2.posthandle
apiinterceptor1.posthandle
apiinterceptor2.aftercompletion
Apiinterceptor1.aftercompletion
Configuring Interceptors
Create a new log processing interceptor, inherit the Apiinterceptoradapter, override the method in the parent class
public class Loginterceptor extends Apiinterceptoradapter {@Override public boolean prehandle (httpservletrequest Request, HttpServletResponse response, Object Serviceobj, Object Argu) throws Exception {System.out
. println ("======prehandle======");
System.out.println ("IP:" + requestutil.getclientip (request));
SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
if (Argu! = null) {SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ());
} return true; } @Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object serviceobj, O
Bject Argu, Object result) throws Exception {System.out.println ("======posthandle======");
SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
if (Argu! = null) {SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ()); } System.out.println ("Result:" + JSON.)toJSONString (result)); } @Override public void aftercompletion (HttpServletRequest request, httpservletresponse response, Object Serviceo BJ, Object Argu, object result, Exception e) throws Exception {System.out.println ("======aftercomplet
ion====== ");
SYSTEM.OUT.PRINTLN ("interface class:" + Serviceobj.getclass (). GetName ());
SYSTEM.OUT.PRINTLN ("parameter class:" + Argu.getclass (). GetName ());
SYSTEM.OUT.PRINTLN ("End result:" + json.tojsonstring (result));
System.out.println ("E:" + e); }
}
To add interceptors to the APICONFGI:
@Override
protected void Initapiconfig (Apiconfig apiconfig) {
...
Configure Interceptor
apiconfig.setinterceptors (
new apiinterceptor[] {new Loginterceptor ()});
...
}
Intercept Range
By default, all interfaces are blocked, and if you want to intercept the specified interface, you can override the Boolean match () method:
Block only the Goods.get interface
@Override public
boolean match (Apimeta Apimeta) {
return apimeta.getname (). Equals (" Goods.get ");
}