收藏
回答

公众号入门指引中的服务搭建有java demo吗,python看不懂

回答关注问题邀请回答
收藏

2 个回答

  • 唐榕
    唐榕
    2020-03-27

    微信回调上面的第一个接口是只调一次还是多次,

    2020-03-27
    有用
    回复 1
    • Mr.Zhao
      Mr.Zhao
      2020-03-27
      get是一次。post是多次
      2020-03-27
      回复
  • Mr.Zhao
    Mr.Zhao
    2020-03-26
    package com.example.demo.controller;
    
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.*;
    import org.apache.commons.codec.digest.DigestUtils;
    
    import java.util.Arrays;
    
    @RestController
    @RequestMapping("/hello")
    public class AuthController {
    
        @GetMapping(produces = MediaType.TEXT_PLAIN_VALUE)
        public String authGet(
                @RequestParam(name = "signature", required = false) String signature,
                @RequestParam(name = "timestamp", required = false) String timestamp,
                @RequestParam(name = "nonce", required = false) String nonce,
                @RequestParam(name = "echostr", required = false) String echostr) {
            if (this.checkSignature(timestamp, nonce, signature)) {
                return echostr;
            }
            return "非法请求";
        }
    
        @PostMapping(produces = MediaType.APPLICATION_XML_VALUE)
        public String post(
                @RequestBody String requestBody,
                @RequestParam(name = "msg_signature", required = false) String msgSignature,
                @RequestParam(name = "encrypt_type", required = false) String encryptType,
                @RequestParam(name = "signature", required = false) String signature,
                @RequestParam("timestamp") String timestamp,
                @RequestParam("nonce") String nonce) throws Exception {
            if (!this.checkSignature(timestamp, nonce, signature)) {
                return "";
            }
            return "success";
        }
    
        private boolean checkSignature(String timestamp, String nonce, String signature) {
            String token = "hello";
            String[] arr = {token, timestamp, nonce};
            Arrays.sort(arr);
            StringBuilder stringBuilder = new StringBuilder();
            for (String a : arr) {
                stringBuilder.append(a);
            }
            return DigestUtils.sha1Hex(stringBuilder.toString()).equals(signature);
        }
    
    }
    
    2020-03-26
    有用
    回复
登录 后发表内容
问题标签