Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信公众平台 接口配置 Spring Boot 实现 #60

Open
techiall opened this issue Sep 3, 2019 · 0 comments
Open

微信公众平台 接口配置 Spring Boot 实现 #60

techiall opened this issue Sep 3, 2019 · 0 comments

Comments

@techiall
Copy link
Owner

techiall commented Sep 3, 2019

微信官方文档 - 接入指南 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319

配合 微信公众平台 - 测试号

以下都在测试号中测试通过。

按照文档教材基本没什么问题。

  • 必须要有公网的服务器
    • 如果有了的话,可以使用 frp 等工具,将本地端口代理到公网的服务器的 80 / 443 端口,不能使用使用其他端口,不然收不到请求。
  • 不需要域名。

检验signature的 Spring Boot 示例代码

一个接受微信请求的实体类,控制层,签名认证类。

SignatureToken.java

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author techial
 */
@Data
@Accessors(chain = true)
public class SignatureToken {
    private String signature;

    private String timestamp;

    private String nonce;

    private String echostr;
}

SignatureController.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.techial.wechatpush.request.SignatureToken;
import top.techial.wechatpush.utils.SignatureUtils;

import java.util.Arrays;

/**
 * @author techial
 */
@RestController
@RequestMapping("/signature")
@Slf4j
public class SignatureController {

    private static final String TOKEN = "wtf-wechat";

    @GetMapping
    public String verify(SignatureToken signatureToken) {
        log.info(signatureToken.toString());
        String[] strings = new String[]{TOKEN, signatureToken.getTimestamp(), signatureToken.getNonce()};
        Arrays.sort(strings);
        String sortStr = String.join("", strings);
        String encodeStr = SignatureUtils.encode(sortStr);
        if (signatureToken.getSignature().equals(encodeStr)) {
            return signatureToken.getEchostr();
        }
        return null;
    }

    @PostMapping
    public void findAll(@RequestBody String str) {
        log.info(str);
    }

}

SignatureUtils.java

import lombok.SneakyThrows;

import java.security.MessageDigest;

/**
 * @author techial
 */
public class SignatureUtils {
    private SignatureUtils() {
        throw new IllegalStateException("Utility class");
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuilder result = new StringBuilder();
        for (byte value : b) {
            result.append(Integer.toString((value & 0xff) + 0x100, 16).substring(1));
        }
        return result.toString();
    }

    @SneakyThrows
    public static String encode(String str) {
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(str.getBytes());
        return byteArrayToHexString(messageDigest.digest());
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant