微信静默授权全步骤

首先,整理一下获取授权的步骤。

一、配置微信服务器。

//省略import部分
@RequestMapping("/wechat")
@Controller
public class MobileWechatController {

    private static String token = "wechat";

    @RequestMapping(value = "/testWx")
    public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {

        System.out.println("========WechatController========= ");

        Enumeration pNames = request.getParameterNames();
        while (pNames.hasMoreElements()) {
            String name = (String) pNames.nextElement();
            String value = request.getParameter(name);
            // out.print(name + "=" + value);

            String log = "name =" + name + "     value =" + value;
            System.out.println(log + "aaaa");
        }
        String signature = request.getParameter("signature");/// 微信加密签名
        String timestamp = request.getParameter("timestamp");/// 时间戳
        String nonce = request.getParameter("nonce"); /// 随机数
        String echostr = request.getParameter("echostr"); // 随机字符串
        PrintWriter out = response.getWriter();
        if (checkSignature(signature, timestamp, nonce)) {
            System.out.println("--------接入成功--------");
            out.print(echostr);
        }
        out.close();
        out = null;
    }
    private static boolean checkSignature(String signature, String timestamp, String nonce) {
        System.out.println("signature:" + signature + "timestamp:" + timestamp + "nonce:" + nonce);
        String[] arr = new String[] { token, timestamp, nonce };
        // 将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(arr);
        StringBuilder content = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            content.append(arr[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;

        try {
            md = MessageDigest.getInstance("SHA-1");
            // 将三个参数字符串拼接成一个字符串进行sha1加密
          
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni微信h5静默授权是指在用户进入uni微信H5页面时,通过微信授权接口获取用户的openid,并且不弹出授权页面让用户手动授权的一种授权方式。 实现uni微信H5静默授权获取openid步骤如下: 1. 首先,在uni-app中引入微信JSSDK,通过在index.html中引入微信JS SDK库文件,或者通过npm安装并在main.js中引入微信JSSDK。 2. 在uni微信H5页面中编写获取openid的逻辑。可以在页面加载完成后,通过微信JSSDK提供的接口wx.config进行微信JS SDK的配置。在配置完成后,可以通过调用wx.ready函数,在ready回调函数中进行获取openid的操作。 3. 使用uni.request或uni.get请求后端接口,将微信提供的code发送至后端。 4. 后端接口需要通过微信的网页授权接口,调用接口获取access_token和openid。接口调用成功后,可以把openid返回给前端。 5. 前端接收到openid后,可以进行后续的业务逻辑处理,例如用户登录、数据统计等。 需要注意的是,uni微信H5静默授权获取openid需要满足一定的条件,包括要求用户在微信客户端中已经授权过且未取消授权,页面的域名需要与微信公众平台的配置一致等。 总结起来,通过微信JSSDK的配置和调用微信的网页授权接口,可以实现uni微信H5静默授权获取openid的功能。这使得开发者能够更加便捷地获取用户的openid,并基于openid实现个性化的功能和服务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值