KONGA下的HAMC插件功能 --JAVA代码实现

Posted vietahole

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了KONGA下的HAMC插件功能 --JAVA代码实现相关的知识,希望对你有一定的参考价值。

 

设置HAMC插件


 

 

 

postman模拟请发发送:

 

 

 Java代码:

 

HMAC-SHA-256工具类

 

 1 import java.security.InvalidKeyException;
 2 import java.security.NoSuchAlgorithmException;
 3 import java.util.Formatter;
 4 import javax.crypto.Mac;
 5 import javax.crypto.spec.SecretKeySpec;
 6 
 7 public class HmacSha256Util {
 8 
 9     private static final String HMAC_SHA1_ALGORITHM = "HmacSHA256";
10     private static String toHexString(byte[] bytes) {
11         Formatter formatter = new Formatter();
12         for (byte b : bytes) {
13             formatter.format("%02x", b);
14         }
15         return formatter.toString();
16     }
17  
18     public static String signature(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
19         SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
20         Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
21         mac.init(signingKey);
22         return toHexString(mac.doFinal(data.getBytes()));
23     }
24  
25     public static byte[] signatureReturnBytes(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
26         SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
27         Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
28         mac.init(signingKey);
29         return mac.doFinal(data.getBytes());
30     }
31 
32 }

 

SHA-256 工具方法:

public static String SHA256(String param) {
        if (StringUtils.isBlank(param)) {
            throw new IllegalArgumentException("param can not be null");
        }
        try {
            byte[] bytes = param.getBytes("utf-8");
            final MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.reset();
            md.update(bytes);
            final Base64 base64 = new Base64();
            final byte[] enbytes = base64.encode(md.digest());
            return new String(enbytes);
        } catch (final NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("unknown algorithm SHA-256");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

测试:

 1 public class Test01 {
 2     
 3     @Test
 4     public void getSHA256() throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException{
 5          StringBuilder stb = new StringBuilder();
 6          String content = stb.append("Date: ").append(getTime()).append("\\n").append("abc: ").append("123456").append("\\n").append("Digest: SHA-256=").append(getBody()).toString();
 7          System.out.println("签名前的数据:"+content);
 8          String secret="kOI6ZHLGdgd2GB8osYZSl7QDCrHzAFg1";
 9          String signature2 = new String(Base64.getEncoder().encode(HmacSha256Util.signatureReturnBytes(content, secret)), "US-ASCII");    
10         System.out.println("显示指定编码[推荐] signature:"+signature2);
11        
12     }
13 public String getTime() {
14         Date d=new Date();
15         DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
16         format.setTimeZone(TimeZone.getTimeZone("GMT"));
17         String hdate = format.format(d);
18         return hdate;
19     }
20     
21     
22     public String getBody() throws UnsupportedEncodingException {
23         String body = "{\\"item\\":[{\\"address\\":\\"hpSg3NXvBAdaDcaeGQiPFF9tciW9BmcQv8Qm\\"}]}";        
24         String sha2562 = Sha256Util.SHA256(body);
25         return sha2562;
26     }  
27     
28 }

 

 最经过postman调试成功访问

以上是关于KONGA下的HAMC插件功能 --JAVA代码实现的主要内容,如果未能解决你的问题,请参考以下文章

konga的初步使用

konga 安装

使用Kong和Konga管理微服务和API

kong和konga的安装部署

konga 操作

docker 启动konga 接入ldap