(JAVA)支付寶小程序登錄相關(authToken獲取用戶唯一userId、encryptedData解密手機號)

前言:

最近公司做一個支付寶小程序項目,用支付寶userId做唯一用戶id,後臺encryptedData解密出用戶支付寶綁定的手機號信息,其中

參數:authToken和encryptedData均爲前端傳入,需要和前端協調開發。

正文開始:

貼代碼:

1.authtoken獲取userId 前端文檔  後端文檔

public String findUserId(String authCode) throws AdminException, AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.url, AlipayConfig.app_id, AlipayConfig.private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.public_key, AlipayConfig.signtype);
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(authCode);
        // request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
        //String accessToken = response.getAccessToken();
        if (response.isSuccess()) {
            //log.info("調用成功");
            //log.info("支付寶用戶唯一id:" + response.getUserId());
            // log.info("token令牌:" + response.getAccessToken());   //訪問令牌。通過該令牌調用需要授權類接口
            return response.getUserId();
        }
        return null;
    }

其中的accessToken我沒有用到,你們用到了解除註釋就可以,親測可以獲取到。

2.encryptedData解密手機號:

//解密手機號
        JSONObject jsonObject =JSONObject.parseObject(userSmallLoginRequest.getEncryptedData());
        String phoneResult = AESCBCUtil.RealDecrypt(jsonObject.getString("response"), AlipayConfig.aesSecretKey);
        JSONObject jsonObject1 = JSONObject.parseObject(phoneResult);
        if(!"10000".equals(jsonObject1.getString("code"))){
            throw new AdminException("用戶手機號解密失敗");
        }
        String phone = jsonObject1.getString("mobile");

其中的AlipayConfig.aesSecretKey是支付寶小程序設置的密鑰

貼工具類:

package com.dq.utils;


import com.alipay.api.internal.util.codec.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author: martin
 * @date: 2018/8/21 20:11
 * @description:
 */
public class AESCBCUtil {
    /**
     *
     * @param content 密文
     * @param key aes密鑰
     * @return 原文
     */
    public static String RealDecrypt(String content, String key) throws Exception {

        //反序列化AES密鑰
        SecretKeySpec keySpec = new SecretKeySpec(Base64.decodeBase64(key.getBytes()), "AES");

        //128bit全零的IV向量
        byte[] iv = new byte[16];
        for (int i = 0; i < iv.length; i++) {
            iv[i] = 0;
        }
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        //初始化加密器並加密
        Cipher deCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        deCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec);
        byte[] encryptedBytes = Base64.decodeBase64(content.getBytes());
        byte[] bytes = deCipher.doFinal(encryptedBytes);
        return new String(bytes);

    }
}

其中userSmallLoginRequest.getEncryptedData()爲傳入的加密串,phone就是拿到的手機號,JSONObject爲阿里巴巴的fastjson,前端獲取代碼是 官方鏈接

my.getPhoneNumber({
    success: (res) => {
        let encryptedData = res.response;
        my.request({
            url: '你的後端服務端',
            data: encryptedData,
        });
    },
    fail: (res) => {
        console.log(res);
        console.log('getPhoneNumber_fail');
    },
});

其中傳入後端的data格式應該是:

{"response": "","xx":"xxx"}

 經過我們業務邏輯解析出來的格式應該是:

{
  "code": "10000",
  "msg": "Success",
  "mobile": "1597671905"
}

mobile就是手機號,沒了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章