Skip to content

lingxi/java-signature

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

灵析 API 签名包

目前 java 版本不包括 HTTP 请求部分

依赖

手动下载 jar 包

Apache Commons 的 Hex

Json-Simple

基本用法

获取请求的验证数据

package lingxi.signature;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Authenticator auther = new Authenticator("your-key", "your-secret");

        // 请求参数
        HashMap<Object, Object> data = new HashMap<>();

        // 数组参数使用 ArrayList
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        ids.add(3);

        // HashMap 参数
        HashMap<Object, Object> m = new HashMap<>();
        m.put("key1", "value");
        m.put("key2", "value");
        m.put("ids", ids);

        // 普通分页参数
        data.put("page", 2);
        data.put("per_page", 3);

        try {
            HashMap query = auther.getAuthParams(data);

            System.out.println("http://api.lingxi.com/v1/test/auth?" + Helper.createQueryLink(query));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

签名验证

抛出了两个异常 SignatureTimestampException 时间戳过期,以及 SignatureValueException 签名错误

try {
    HashMap<Object, Object> data1 = new HashMap<>();
    data1.put("key", "secret");

    if (auther.attempt(data1)) {
        // passed, do you want...        
    }
} catch (SignatureTimestampException | SignatureValueException e) {
    System.out.println(e.getMessage());
} catch (Exception e) {
    e.printStackTrace();
}

Releases

No releases published

Packages

No packages published

Languages