YiDunUrlResultJob.java 3.3 KB
package com.cnlive.shenhe.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyYidun;
import com.cnlive.shenhe.serviceImpl.YiDunServiceImpl;
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 易盾网站检测
 *
 * @Author: xujincheng
 * @Date: 2020/3/23 15:16
 */
@Component
public class YiDunUrlResultJob {

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

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


    @Autowired
    YiDunServiceImpl yiDunServiceImpl;


    /**
     * 查询易盾网站检测结果接口
     */
    @Scheduled(cron = "0/1 * 10-23 * * ? ")//每天10点到23点执行,每分钟一次
    public void YiDunUrl() {
        Map<String, String> params1 = new HashMap<String, String>(5);
        // 1.设置公共参数
        params1.put("secretId", SECRETID);
        params1.put("version", "v1.0");
        params1.put("timestamp", String.valueOf(System.currentTimeMillis()));
        params1.put("nonce", String.valueOf(new Random().nextInt()));

        // 2.生成签名信息
        String signature1 = YiDunAntiFraudUtil.genSignature(SECRETKEY, params1);
        params1.put("signature", signature1);

        // 3.发送HTTP请求
        String s1 = HttpClientUtils.post_str(URL_RESULT, params1);
        logger.info("查询易盾网站检测结果:{}", s1);

        // 4.解析接口返回值
        JSONObject resultObject = JSONObject.parseObject(s1);
        int code = resultObject.getIntValue("code");
        String msg = resultObject.getString("msg");
        if (code == 200 && "ok".equals(msg)){
            JSONArray resultJSONArray = resultObject.getJSONArray("result");
            for(int i=0;i<resultJSONArray.size();i++){

                JSONObject jsonObject = resultJSONArray.getJSONObject(i);
                String dataId = jsonObject.getString("dataId");
                //检测结果, 1:正常(建议通过) 2:异常(建议拦截) 3:疑似(建议人工确认)0:无结果(检测失败)
                int result= jsonObject.getIntValue("result");
                if(result==1){

                }

                ShyYidun yiDunMsg = yiDunServiceImpl.getYiDunMsg(dataId);
                ShyYidun newYiDunMsg = new ShyYidun();
                newYiDunMsg.setId(yiDunMsg.getId());
                newYiDunMsg.setUpdate_time(new Date());
                yiDunServiceImpl.updateYiDunMsg(newYiDunMsg);
            }
        }

    }
}