Browse Source

广告违规素材推送

zhaoxian 1 year ago
parent
commit
5750809a33

+ 43 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/material/controller/ByteDanceVideoInfoController.java

@@ -2,6 +2,10 @@ package cn.com.ctop.toutiao.modules.material.controller;
 
 import cn.com.ctop.toutiao.modules.material.entity.ByteDanceVideoInfo;
 import cn.com.ctop.toutiao.modules.material.service.IByteDanceVideoInfoService;
+import cn.com.ctop.toutiao.modules.tool.callback.AuthTokenUtil;
+import cn.com.ctop.toutiao.modules.tool.callback.BaseResponse;
+import cn.com.ctop.toutiao.modules.tool.callback.CallbackResponse;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -15,7 +19,11 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.Part;
+import java.io.IOException;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Map;
 
 /**
  * @Description: 今日头条视频素材信息
@@ -162,4 +170,39 @@ public class ByteDanceVideoInfoController {
         }
         return result;
     }
+
+
+    /**
+     *广告违规素材推送
+     */
+    @RequestMapping(value = "/illegalVideosPush", method = { RequestMethod.GET, RequestMethod.POST })
+    public CallbackResponse illegalVideosPush(HttpServletRequest request,
+                                     @RequestBody String data,
+                                     @RequestParam(value = "challenge", defaultValue = "0") int challenge,
+                                     @RequestParam(value = "event", defaultValue = "") String event) throws IOException {
+        // 处理 verify 事件
+        if (null != event && event.equals("verify_webhook")) {
+            return new CallbackResponse(BaseResponse.ok(), challenge);
+        }
+
+        // 订阅任务配置页面的 "验证密钥"
+        String secretKey =  System.getenv("bd881f3bcd894ed48638dfb63eb627cf");
+
+        // 数据接收,验证消息
+        AuthTokenUtil.InputStreamCacher cacher = new AuthTokenUtil.InputStreamCacher(request.getInputStream());
+        boolean isValidToken =  AuthTokenUtil.isValidToken(secretKey, cacher, request.getHeader("X-Open-Signature"));
+        if (!isValidToken) {
+            return new CallbackResponse(new BaseResponse(400, "invalid token"), 0);
+        }
+        Map<String, String[]> parameterMap = request.getParameterMap();
+        System.out.println(parameterMap);
+
+        System.out.println(data);
+//        JSONObject parse = JSONObject.parseObject(data);
+//        System.out.println(parse);
+
+        // 数据处理流程...
+        return new CallbackResponse(BaseResponse.ok(), 0);
+    }
+
 }

+ 77 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/tool/callback/AuthTokenUtil.java

@@ -0,0 +1,77 @@
+package cn.com.ctop.toutiao.modules.tool.callback;
+
+
+import com.google.common.hash.HashCode;
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import  org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class AuthTokenUtil {
+    static Logger logger = LoggerFactory.getLogger(AuthTokenUtil.class);
+
+    public static byte[] readAsBytes(InputStreamCacher cacher){
+        int len = cacher.getLength();
+        byte[] buffer = new byte[len];
+        try(InputStream in = cacher.getInputStream()){
+            in.read(buffer, 0, len);
+        }catch (IOException e){
+            e.printStackTrace();
+        }
+        return buffer;
+    }
+    // 根据SecretKey计算签名
+    public static String genSignature(String secretKey, byte[] binaryparams){
+        HashFunction function = Hashing.hmacSha256(secretKey.getBytes());
+        HashCode code = function.hashBytes(binaryparams);
+        return code.toString();
+    }
+    // 校验签名
+    public static boolean isValidToken(String secretKey, InputStreamCacher cacher, String signature ) {
+        if (StringUtils.isEmpty(secretKey) || StringUtils.isEmpty(signature)) {
+            return false;
+        }
+        String reSignature = genSignature(secretKey,readAsBytes(cacher));
+        logger.info("secretKey: " + secretKey);
+        logger.info("content: " + new String(readAsBytes(cacher)));
+        logger.info("signature: " + signature);
+        logger.info("reSignature: " + reSignature);
+        return signature.equals(reSignature);
+    }
+
+    public static class InputStreamCacher {
+        private int length;
+        private ByteArrayOutputStream byteArrayOutputStream = null;
+
+        public InputStreamCacher(InputStream inputStream) {
+            if (inputStream == null)
+                return;
+            length = 0;
+            byteArrayOutputStream = new ByteArrayOutputStream();
+            byte[] buffer = new byte[1024];
+            int len;
+            try {
+                while ((len = inputStream.read(buffer)) > -1 ) {
+                    byteArrayOutputStream.write(buffer, 0, len);
+                    length += len;
+                }
+                byteArrayOutputStream.flush();
+            } catch (IOException e) {
+            }
+        }
+        public InputStream getInputStream() {
+            if (byteArrayOutputStream== null)
+                return null;
+            return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
+        }
+        public int getLength() {
+            return length;
+        }
+    }
+}

+ 21 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/tool/callback/BaseResponse.java

@@ -0,0 +1,21 @@
+package cn.com.ctop.toutiao.modules.tool.callback;
+
+
+import com.fasterxml.jackson.annotation.JsonAlias;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class BaseResponse implements Serializable {
+    @JsonAlias("StatusCode")
+    private Integer statusCode;
+    @JsonAlias("StatusMessage")
+    private String StatusMessage;
+
+    public static BaseResponse ok() {
+        return new BaseResponse(0, "ok");
+    }
+}

+ 16 - 0
jeecg-boot-module-system/src/main/java/cn/com/ctop/toutiao/modules/tool/callback/CallbackResponse.java

@@ -0,0 +1,16 @@
+package cn.com.ctop.toutiao.modules.tool.callback;
+
+import com.fasterxml.jackson.annotation.JsonAlias;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+@Data
+@AllArgsConstructor
+public class CallbackResponse {
+    @JsonAlias("BaseResp")
+    private BaseResponse baseResp;
+    @JsonAlias("challenge")
+    @JsonInclude(JsonInclude.Include.NON_EMPTY)
+    private Integer challenge;
+}