在pom中加加入以下配置:
<repositories>
<repository>
<id>central</id>
<name>central</name>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>https://repo1.maven.org/maven2/</url>
</pluginRepository>
</pluginRepositories>
在依赖中加入下属配置:
<dependency>
<groupId>cn.snz1</groupId>
<artifactId>apihelper</artifactId>
<version>1.5.1</version>
<type>pom</type>
</dependency>
下载apihelper-1.5.1-all.jar文件并放到classpath中。
供第三方应用通过网关调用API时进行安全认证时使用。
提供pClient复用链接实现(需要HttpClient4.5.13以上版本)。
提供Retrofit2注解式RestAPI客户端实现(依赖OKHttpClient3.10版以上)。
通过网关调用的一些通用工具工具类(需要Spring支持)。
参见gateway.api.RSAUtils
类,用于基本的RSA密钥加载操作。
加载PKCS8格式的RSA公钥。
加载PEM格式的RSA公钥。
加载PKCS8格式的RSA私钥。
加载PEM格式的RSA私钥。
使用RSAUtils工具加载RSA私钥:
- PEM格式
RSAKey rsakey = (RSAKey)gateway.RSAUtils.parsePrivateKey(" PEM of your rsa privatekey")
- PKCS8格式
RSAKey rsakey = (RSAKey)gateway.RSAUtils.parsePrivateKey("PKCS8 of your rsa privatekey");
JwtContext jwt_context = gateway.JwtContext.create("your apptoken", rsakey, 1200);
JwtToken jwt_token = jwt_context.createJwtToken();
- 获得
Authorization
头需要的字符串
jwt_token.toAuthorizationString();
- 获得
jwt
请求参数需要的字符串
jwt_token.toParameterString();
- 判断令牌是否过期
jwt_token.isExpired();
注:如果令牌已过期请使用上下文重新创建。
参见HttpClientHelper的requestExecute方法。
Retrofit2大大简化了基于REST接口的客户端开发过程,因此建议使用此方式调用通过应用网关提供的API。
构造在请求头中加入JWT令牌认证的OkHttpClient对象:
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(
new OkHttpClientJwtInterceptor(jwtContext)).build();
注:jwtContext请提前创建
然后构造Retrofit2服务实例:
Retrofit retrofit = new Retrofit.Builder().baseUrl(gatweayURLPrefix)
.addCallAdapterFactory(SynchCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(JsonUtils.getGson())).client(client).build();
准备好已注解的客户端接口类:
import gateway.api.Return;
import gateway.api.NotExceptException;
public interface TestService {
@GET("/test/interface")
public Return<Map<String, Object>> test() throws NotExceptException;
}
然后通过Retrofit2实例获取委派实现:
TestService proxy_impl = retrofit.create(TestService.class);
proxy_impl.test();
其他更多的Retrofit2资料请参见Retrofit2官网
参见gateway.api.ViaGatewayUtlis
类实现。