一键登录功能实现(采用极光SDK)

前端流程

1. 引入极光认证 SDK:

<!-- 引入极光认证 SDK -->

<script type="text/javascript" src="https://jverification.jiguang.cn/scripts/jverification-web.5.2.0.min.js?v=4"></script>

2. 初始化 SDK

  • 参考极光官方文档:Web SDK API - 极光文档

    mounted() {
    // 确保 SDK 已加载
    if (window.JVerification) {
    this.initJVerification();
    } else {
    const script = document.createElement('script');
    script.src = 'https://jverification.jiguang.cn/sdk/web/jverification.js';
    script.onload = this.initJVerification;
    document.head.appendChild(script);
    }
    },
    methods: {
    initJVerification() {
    JVerification.init({
    appkey: '你的AppKey', // 替换成你的AppKey
    });
    }
    }

3. 获取 loginToken

这里需要加强注意:

  1. 极光官方文档中有两个 api 用于获取 token 但是一键登录是 loginAuth

  2. 在调用 loginAuth 中要使用公网 ip,并提前报备在极光平台中

后端流程

1. 发送请求到极光 API

  • 使用获取到的 Token,将其发送到极光提供的 API 进行验证,并获取加密的手机号码。

参考官方文档:一键登录 API - 极光文档

  • 也可采用前端直接调用极光 API

    import axios from 'axios';

    const getPhoneNumber = async (loginToken) => {
    try {
    const response = await axios.post(
    'https://api.verification.jpush.cn/v1/web/loginTokenVerify',
    {
    loginToken: loginToken,
    exID: '1234566', // 可选
    },
    {
    auth: {
    username: '你的AppKey', // 替换成你的AppKey
    password: '你的Master Secret', // 替换成你的Master Secret
    },
    headers: {
    'Content-Type': 'application/json',
    },
    }
    );
    return response.data;
    } catch (error) {
    console.error('Error:', error);
    return null;
    }
    };

    export default getPhoneNumber;

2. 解密手机号

  • 加密的手机号码需要用 RSA 私钥解密。此过程通常在服务器端完成 。

参考官网示例 Java:

复制代码
import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class RSADecrypt {
    public static void main(String[] args) throws Exception {String encrypted = args[0];
          String prikey = args[1];
          String result = decrypt(encrypted, prikey);
          System.out.println(result);
    }

    public static String decrypt(String cryptograph, String prikey) throws Exception {PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
           PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
    
           Cipher cipher=Cipher.getInstance("RSA");
          cipher.init(Cipher.DECRYPT_MODE, privateKey);
    
           byte [] b = Base64.getDecoder().decode(cryptograph);
           return new String(cipher.doFinal(b));
    }
}

解密完成后执行自己的登录逻辑,返回登录成功等信息。

其中涉及到 RSA 私钥解密加密的手机号码:给出 RSA 公私钥生成地址:https://www.metools.info/code/c80.html

总结

  • 前端:
  • 引入和初始化极光认证 SDK。
  • 获取 loginToken 并发送到极光 API。
  • 处理 API 响应并传递到后端。
  • 后端:
  • 发送 loginToken 请求到极光 API 验证。
  • 使用 RSA 私钥解密加密的手机号码。
相关推荐
苏三说技术9 小时前
SpringBoot4.2正式发布,人麻了!
后端
去伪存真9 小时前
《最终版》《打死不改版》全传一遍?RAG 系统文件去重实战
前端
anyup9 小时前
【全端支持】 uni-app x 专用请求库:axios 写法,多实例、拦截器、可中断、上传下载
前端·uni-app·unix
小飞在努力9 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
费曼学习法9 小时前
6 个 MCP Server 传输层重写实录:超时率 12% → 0.3%
前端·后端
HarmonSir9 小时前
Octop 架构解析:5 个可以直接抄的 Agent 运行时设计
后端·架构
htzyl2069 小时前
金额计算:为什么前端不该用浮点数存钱
前端·javascript·算法
Iseeyore9 小时前
Vue3 Vs React
前端·vue.js
verohh9 小时前
第二篇:模块解耦与边界治理——清理长期迭代积累的模块耦合问题
前端
律宏阔10 小时前
微信小程序 + Taro + TanStack Query 缓存与页面返回刷新踩坑实录
前端·微信小程序·taro