This article describes the settings of the mobile gateway RPC interceptor, RPC request header, RPC Cookie and RPC signature.
Added setting RPC signature content in 10.2.3 baseline.
Intercept RPC requests
In some situations during business development, it is required to control the network requests from clients (for example, intercept network requests, forbid the access to some interfaces or limit traffic), and you can do that with RPC interceptor.
Create a global interceptor
public class CommonInterceptor implements RpcInterceptor {
/**
* Pre-interception: Call back before sending RPC.
* @param proxy: RPC proxy object.
* @param clazz: Model class of rpcface, you can judge which RPC model class is called through clazz.
* @param method: The method called by the current RPC.
* @throws RpcException
* @return true means proceeding; false means interrupting the current request and throwing RpcException, with error code: 9.
*/
@Override
public boolean preHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<? > clazz,
Method method,
Object[] args,
Annotation annotation,
ThreadLocal<Map<String, Object>> extParams)
throws RpcException {
//Do something...
return true;
}
/**Post-interception: Call back when RPC is successfully initiated.
* @return true means proceeding; false means interrupting the current request and throwing RpcException, with error code: 9.
*/
@Override
public boolean postHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<? > clazz,
Method method,
Object[] args,
Annotation annotation) throws RpcException {
//Do something...
return true;
}
/**
Exception interception: Call back when initiating RPC failed.
* @param exception: Error occurs on the current RPC.
* @return true means proceeding to throw the current exception; false means returning as normal without throwing any exception. If there is no special requirement, don’t return false.
*/
@Override
public boolean exceptionHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<? > clazz,
Method method,
Object[] args,
RpcException exception,
Annotation annotation) throws RpcException {
//Do something...
return true;
}
}
Register an interceptor
During framework startup, register the interceptor when RpcService
is being initialized, for example:
public class MockLauncherApplicationAgent extends LauncherApplicationAgent {
public MockLauncherApplicationAgent(Application context, Object bundleContext) {
super(context, bundleContext);
}
@Override
public void preInit() {
super.preInit();
}
@Override
public void postInit() {
super.postInit();
RpcService rpcService = getMicroApplicationContext().findServiceByInterface(RpcService.class.getName());
rpcService.addRpcInterceptor(OperationType.class, new CommonInterceptor());
}
}
Set RPC request header
Set the RPC request header in the initRpcConfig
method of the MainActivity
class. For details, see Code sample.
private void initRpcConfig(RpcService rpcService) {
//Set the request header
Map<String, String> headerMap = new HashMap<>();
headerMap.put("key1", "val1");
headerMap.put("key2", "val2");
rpcInvokeContext.setRequestHeaders(headerMap);
}
Set RPC cookie
Set cookie
Set RPC cookie by calling the following interface. Among them, the rule of Your domain
is all the contents between the first .
of the gateway URL and the first /
after the dot. For example, if the gateway URL is http://test-cn-hangzhou-mgs-gw.cloud.alipay.com/mgw.htm
, then Your domain
is .cloud.alipay.com
.
GwCookieCacheHelper.setCookies(Your domain, cookiesMap);
Remove cookie
Call the following interface to remove the set cookies.
GwCookieCacheHelper.removeAllCookie();
Set SM3 signature verification
After the RPC is initialized, the global signature verification method can be specified as the sm3 type through the setGlobalSignType
method of the MPRpc
class.
MPRpc.setGlobalSignType(TransportConstants.SIGN_TYPE_SM3);
Set RPC signature
Interface
class TransportConstants {
public static final int SIGN_TYPE_DEFAULT = 0; // Default signature method, namely md5
public static final int SIGN_TYPE_MD5 = 1; // md5
public static final int SIGN_TYPE_HMACSHA256 = 3; // hmacsha256
public static final int SIGN_TYPE_SHA256 = 4; // sha256
public static final int SIGN_TYPE_SM3 = 5; // sm3
}
// Global rpc signature algorithm setting
MPRpc.setGlobalSignType(int signType);
Example
Set the global RPC signType and take effect for all RPCs.
// Set the signature method to SM3
MPRpc.setGlobalSignType(TransportConstants.SIGN_TYPE_SM3);