• 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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    @Data
    public class ResultDataVO {
        private Integer code;
        private Boolean success;
        private String phoneNumber;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
        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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    以下是解析生成加密验证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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

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

  • 相关阅读:
    DevOps持续集成-Jenkins(4)
    游戏思考26:游戏服务器压力测试文档(最近在忙这个,这个会更新频繁,12/03未完待续)
    网络编程——基础知识
    WPS或EXCEL表格单元格下拉快捷选择项修改及设置方法
    数据结构与算法面试
    【云原生 从零开始学Docker】三、Docker实战之安装Nginx和Tomcat
    nltk报错punkt 缺失 Error Loading Error11004
    我的周刊(第067期)
    linux shell脚本修改ini配置文件[session]下的键值
    分割回文串
  • 原文地址:https://blog.csdn.net/yumingyang0212/article/details/134210352