Java 实现uniapp本机手机号一键登录

这里简单的贴一下后端的解析代码 其他配置项参照uniapp的官方文档配置就好了

这里的accessToken和openid是前端请求uCloud获取的

@Data
public class UniAppLoginVO {
    private Integer code;
    private String message;
    private ResultDataVO data;
    private Boolean success;
    private ResultError error;
}
@Data
public class ResultDataVO {
    private Integer code;
    private Boolean success;
    private String phoneNumber;
}
    public User uniAppLogin(String accessToken, String openId) {
        try {
            String signStr = "access_token=" + accessToken + "&openid=" + openId;
            String sign = HmacSHA256Util.hmacSHA256(configMapper.selectConfigByConfigKey(CommonConstant.UNI_APP_LOGIN_KEY), signStr);
            String body = HttpRequest.get("你的云函数链接地址/getPhoneNumber?" + signStr + "&sign=" + sign).execute().body();
            UniAppLoginVO uniAppLoginVO = JSONObject.parseObject(body, UniAppLoginVO.class);
            if (uniAppLoginVO.getCode() == 1) {
                ResultDataVO resultDataVO = uniAppLoginVO.getData();
                if (resultDataVO.getSuccess().equals(Boolean.TRUE)) {
                    return verifyLoginByCaptcha(resultDataVO.getPhoneNumber(), null);
                }else {
                    throw new BasicsBootException("解析手机号失败");
                }
            }else {
                throw new BasicsBootException("服务异常获取手机号失败");
            }
        } catch (Exception e) {
            log.error("uniAppLogin error,{}", e.getMessage());
        }
        return null;
    }

以下是解析生成加密验证sign的工具类

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HmacSHA256Util {

    /**
     * HmacSHA256算法,返回的结果始终是32位
     * @param key 加密的键,可以是任何数据
     * @param content 待加密的内容
     * @return 加密后的内容
     * @throws Exception
     */
    public static byte[] hmacSHA256(byte[] key,byte[] content) throws Exception {
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        hmacSha256.init(new SecretKeySpec(key, 0, key.length, "HmacSHA256"));
        return hmacSha256.doFinal(content);
    }

    /**
     * 将加密后的字节数组转换成字符串
     *
     * @param b 字节数组
     * @return 字符串
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }
    /**
     * sha256_HMAC加密
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public static String hmacSHA256(String secret, String message) throws Exception {
        Mac hmacSha256 = Mac.getInstance("HmacSHA256");
        hmacSha256.init(new SecretKeySpec(secret.getBytes(), "HmacSHA256"));
        byte[] bytes = hmacSha256.doFinal(message.getBytes());
        return byteArrayToHexString(bytes);
    }
}

以上就是所有后台处理的方法及工具类

你可能感兴趣的:(Java,uniapp,个人笔记,java,uni-app)