一键登录功能实现(采用极光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

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,并提前报备在极光平台中

<template>
  <div>
    <button @click="getLoginToken">一键登录</button>
  </div>
</template>

<script>
export default {
  methods: {
    async getToken() {
				const operater = 'CM'; // 示例运营商,替换为您需要的运营商代号
				const options = {
					operater,
					type:"full",
					success: (data) => {
						this.jverificationData = {
							...this.jverificationData,
							getTokenSuccess: data,
							loginToken: data.content,
							operater: data.operater
						};
						this.loginData.loginToken = data.content
						console.log('获取Token成功:', data);
            // 添加其它处理逻辑
					},
					fail: (data) => {
						this.jverificationData = {
							...this.jverificationData,
							getTokenError: data
							
						};
						console.error('获取Token失败:', data);
					},
				};

				if (window.JVerificationInterface && this.jverificationData.isInitSuccess) {
					window.JVerificationInterface.loginAuth(options);
				} else {
					console.error('SDK未初始化或初始化未成功,无法获取Token');
				}
			},
  }
}
</script>

后端流程

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 私钥解密加密的手机号码。
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为了在uniapp中接入极光一键登录,需要进行以下步骤: 1.在极光官网注册并创建应用,获取AppKey和AppSecret。 2.在uniapp项目中安装jpush插件,可以使用以下命令进行安装: ```shell npm install jpush-uniapp --save ``` 3.在App.vue文件中引入jpush插件并初始化: ```javascript import JPush from 'jpush-uniapp' export default { onLaunch: function() { JPush.init() } } ``` 4.在需要使用一键登录的页面中,调用jpush插件的login方法: ```javascript import JPush from 'jpush-uniapp' JPush.login({ appKey: 'yourAppKey', authPageConfig: { authLoginPage: 'yourAuthLoginPage', authLogo: 'yourAuthLogo', authNavHidden: true, authStatusBarHidden: true, authCustom: { privacyOne: ['yourPrivacyUrl1', 'yourPrivacyTitle1'], privacyTwo: ['yourPrivacyUrl2', 'yourPrivacyTitle2'] } } }, (res) => { console.log(res) }) ``` 其中,appKey为在极光官网创建应用时获取的AppKey,authPageConfig为一键登录页面的配置信息,包括登录页、logo、隐私协议等。 5.在Android平台上,需要在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.READ_PHONE_STATE" /> ``` 6.在iOS平台上,需要在Info.plist文件中添加以下权限: ```xml <key>NSPhotoLibraryUsageDescription</key> <string>需要您的同意,才能访问相册</string> <key>NSCameraUsageDescription</key> <string>需要您的同意,才能访问相机</string> <key>NSMicrophoneUsageDescription</key> <string>需要您的同意,才能访问麦克风</string> <key>NSContactsUsageDescription</key> <string>需要您的同意,才能访问通讯录</string> <key>NSLocationWhenInUseUsageDescription</key> <string>需要您的同意,才能访问位置</string> <key>NSLocationAlwaysUsageDescription</key> <string>需要您的同意,才能访问位置</string> <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>需要您的同意,才能访问位置</string> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xxxy _

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值