Skip to content

Latest commit

 

History

History
177 lines (111 loc) · 3.95 KB

README.md

File metadata and controls

177 lines (111 loc) · 3.95 KB

API调用帮助库

一、使用方法

1、MAVEN工程配置

在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>

2、直接引用

下载apihelper-1.5.1-all.jar文件并放到classpath中。

二、基本功能

供第三方应用通过网关调用API时进行安全认证时使用。

提供pClient复用链接实现(需要HttpClient4.5.13以上版本)。

提供Retrofit2注解式RestAPI客户端实现(依赖OKHttpClient3.10版以上)。

通过网关调用的一些通用工具工具类(需要Spring支持)。

三、RSA工具类

参见gateway.api.RSAUtils类,用于基本的RSA密钥加载操作。

1、PublicKey parsePublicKey(String pub)

加载PKCS8格式的RSA公钥。

2、PublicKey parsePublicKeyFromPEM(String pub)

加载PEM格式的RSA公钥。

3、PrivateKey parsePrivateKey(String priv)

加载PKCS8格式的RSA私钥。

4、PrivateKey parsePrivateKeyFromPEM(String priv)

加载PEM格式的RSA私钥。

四、创建JWT令牌

1、加载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");
2、创建JWT上下文
JwtContext jwt_context = gateway.JwtContext.create("your apptoken", rsakey, 1200);
3、创建JWT令牌
JwtToken jwt_token = jwt_context.createJwtToken();
  • 获得Authorization头需要的字符串
jwt_token.toAuthorizationString();
  • 获得jwt请求参数需要的字符串
jwt_token.toParameterString();
  • 判断令牌是否过期
jwt_token.isExpired();

注:如果令牌已过期请使用上下文重新创建。

五、使用复用的HttpClient连接

参见HttpClientHelper的requestExecute方法。

六、使用Retrofit2实现服务客户端

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类实现。