YiDunControllerApi.java 3.55 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyYidun;
import com.cnlive.shenhe.service.YiDunService;
import com.cnlive.shenhe.utils.CommonConstStates;
import com.cnlive.shenhe.utils.HttpClientUtils;
import com.cnlive.shenhe.utils.YiDunAntiFraudUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Author: xujincheng
 * @Date: 2020/3/26 13:43
 */
@Controller
@RequestMapping("/api/yiDun")
public class YiDunControllerApi {

    private static Logger logger = LoggerFactory.getLogger(YiDunControllerApi.class);

    @Autowired
    YiDunService yiDunService;

    /**
     * 网站地址URL产品密钥ID,产品标识
     */
    private final static String SECRETID = "c7f33c191020bcc9cbacb7d70417fd2f";
    /**
     * 网站地址URL产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露
     */
    private final static String SECRETKEY = "833222b4951a67a9bc63b5ed0c24d07d";
    /**
     * 网站地址URL提交接口地址
     */
    private final static String URL_SUBMIT = "https://as.dun.163yun.com/v1/crawler/submit";

    /**
     * 向易盾提交网站检测地址(用于测试)
     *
     * @param url 网站地址
     */
    @PostMapping("/submitYiDunUrl")
    @ResponseBody
    public void submitYiDunUrl(String url) {
        Map<String, String> params = new HashMap<String, String>(16);

        // 1.设置公共参数
        params.put("secretId", SECRETID);
        params.put("version", "v1.0");
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        params.put("nonce", String.valueOf(new Random().nextInt()));

        // 2.设置私有参数
        //数据唯一标识,能够根据该值定位到该条数据(查询作用)
        params.put("dataId", UUID.randomUUID().toString());
        //网页URL, 支持contentType类型: html、txt、doc、docx、ppt、pptx、xls、xlsx、pdf
        params.put("url", url);
        //1:检测文本,2:检测图片,同时检测文本+图片则以逗号分隔,例:1,2。
        params.put("checkFlags", "1,2");
        //不是必传参数
        params.put("callback", UUID.randomUUID().toString());
        // 3.生成签名信息
        String signature = YiDunAntiFraudUtil.genSignature(SECRETKEY, params);
        params.put("signature", signature);

        // 4.发送HTTP请求
        String s = HttpClientUtils.post_str(URL_SUBMIT, params);
        logger.info("向易盾提交网站检测地址返回的结果:{}", s);
        JSONObject jsonObject = JSONObject.parseObject(s);
        int code = jsonObject.getIntValue("code");
        String msg = jsonObject.getString("msg");
        if (code == 200 && "ok".equals(msg)) {
            JSONObject result = jsonObject.getJSONObject("result");
            String dataId = result.getString("dataId");
            String taskId = result.getString("taskId");
            ShyYidun shyYidun = new ShyYidun();
            shyYidun.setId(dataId);
            shyYidun.setTask_id(taskId);
            shyYidun.setData_id(dataId);
            //网页
            shyYidun.setCheck_type(CommonConstStates.WANGYE);
            shyYidun.setCreate_time(new Date());
            yiDunService.addYiDunMsg(shyYidun);
        }
    }
}