每日一博 - App key和App Secret

在这里插入图片描述


概述

App Key和App Secret是API接口调用中常用的身份验证机制,确保只有合法的应用程序可以访问API。

下面是一个详细的解释和步骤:

1. App Key(API接口验证序号):

  • 定义: 用于验证API接入合法性的标识符,类似于网站的用户名。
  • 作用: 确保只有被授权的应用程序可以访问API。
  • 获取方式: 通常在开发者注册应用程序时,由API提供者分配。

2. App Secret(API接口密钥):

  • 定义: 与App Key配套使用的密钥,类似于密码。
  • 作用: 与App Key一起用于生成身份验证令牌,确保安全性。
  • 获取方式: 由API提供者在应用程序注册时生成,需要妥善保管,不应公开。

3. 使用流程:

  • 应用程序在调用API之前,需要将App Key和App Secret与API请求一起发送。
  • API提供者使用这两者生成身份验证令牌,验证应用程序的合法性。
  • 仅当身份验证通过时,API提供者才会响应应用程序的请求。

4. 安全注意事项:

  • 保密性: App Secret需要保密存储,不应明文传输或暴露在客户端。
  • 更新周期: 建议定期更新App Secret,以增强安全性。
  • 权限控制: 针对不同的API调用,确保应用程序具有最小必要的权限。

Code

package com.artisan.app;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.UUID;

/**
 * @author 小工匠
 * @version 1.0
 * @mark: show me the code , change the world
 */
public class AppUtils {
   

    //生成 app_secret 密钥
    private final static String SERVER_NAME = "_abc123";
    private final static String[] chars = new String[]{
   "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    /**
     * @Description: <p>
     * 短8位UUID思想其实借鉴微博短域名的生成方式,但是其重复概率过高,而且每次生成4个,需要随即选取一个。
     * 本算法利用62个可打印字符,通过随机生成32位UUID,由于UUID都为十六进制,所以将UUID分成8组,每4个为一组,然后通过模62操作,结果作为索引取出字符,
     * 这样重复率大大降低。
     * 经测试,在生成一千万个数据也没有出现重复,完全满足大部分需求。
     */
    public static String getAppId() {
   
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
   
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }

    /**
     * 通过appId和内置关键词生成APP Secret
     */
    public static String getAppSecret(String appId) {
   
        try {
   
            String[] array = new String[]{
   appId, SERVER_NAME};
            StringBuffer sb = new StringBuffer();
            // 字符串排序
            Arrays.sort(array);
            for (int i = 0; i < array.length; i++) {
   
                sb.append(array[i]);
            }
            String str = sb.toString();
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();
            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
   
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
   
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (NoSuchAlgorithmException e) {
   
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public static void main(String[] args) {
   
        String appId = getAppId();
        String appSecret = getAppSecret(appId);
        System.out.println("appId: " + appId);
        System.out.println("appSecret: " + appSecret);
    }
}
    

在这里插入图片描述

相关推荐

  1. 每日

    2023-12-20 05:38:01       46 阅读
  2. 每日题:Leetcode1314.矩阵区域

    2023-12-20 05:38:01       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-20 05:38:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-20 05:38:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-20 05:38:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-20 05:38:01       20 阅读

热门阅读

  1. 通过fu过 Function Calling 查询数据库

    2023-12-20 05:38:01       34 阅读
  2. 普冉(PUYA)单片机开发笔记 [完结篇]:使用体会

    2023-12-20 05:38:01       48 阅读
  3. 高空作业MR混合现实情景实训教学应用

    2023-12-20 05:38:01       41 阅读
  4. 【算法小题 go程序版】递归练习 -- 猴子吃桃问题

    2023-12-20 05:38:01       40 阅读
  5. 链式表的实现

    2023-12-20 05:38:01       32 阅读
  6. Docker使用7-Use Docker Compose

    2023-12-20 05:38:01       56 阅读
  7. 【嵌入式面试】嵌入式经典面试题汇总(C语言)

    2023-12-20 05:38:01       34 阅读
  8. Python基础dict字典定义与函数

    2023-12-20 05:38:01       38 阅读
  9. 【SpringBoot实战】基于MybatisPlus实现基本增删改查

    2023-12-20 05:38:01       52 阅读
  10. 【安全】audispd调研

    2023-12-20 05:38:01       39 阅读
  11. C#连接数据库40错误

    2023-12-20 05:38:01       39 阅读