Java後臺微信內容安全接口imgSecCheck,msgSecCheck

官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
參考文檔:https://blog.csdn.net/u010651369/article/details/101697940?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

常見問題:{"errcode":41005,"errmsg":"media data missing hint:"}

1.文件上傳

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UploadAction {
    /**
	 * 上傳二進制文件
	 * @param graphurl 接口地址
	 * @param file 圖片文件
	 * @return
	 */
	public static String uploadFile(String graphurl, MultipartFile file) {
		String line = null;// 接口返回的結果
		try {
			// 換行符
			final String newLine = "\r\n";
			final String boundaryPrefix = "--";
			// 定義數據分隔線
			String BOUNDARY = "========7d4a6d158c9";
			// 服務器的域名
			URL url = new URL(graphurl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			// 設置爲POST情
			conn.setRequestMethod("POST");
			// 發送POST請求必須設置如下兩行
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			// 設置請求頭參數
			conn.setRequestProperty("connection", "Keep-Alive");
			conn.setRequestProperty("Charsert", "UTF-8");
			conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
			conn.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
			OutputStream out = new DataOutputStream(conn.getOutputStream());

			// 上傳文件
			StringBuilder sb = new StringBuilder();
			sb.append(boundaryPrefix);
			sb.append(BOUNDARY);
			sb.append(newLine);
			// 文件參數,photo參數名可以隨意修改
			sb.append("Content-Disposition: form-data;name=\"image\";filename=\"" + "https://api.weixin.qq.com" + "\"" + newLine);
			sb.append("Content-Type:application/octet-stream;charset=UTF-8");
			// 參數頭設置完以後需要兩個換行,然後纔是參數內容
			sb.append(newLine);
			sb.append(newLine);

			// 將參數頭的數據寫入到輸出流中
			out.write(sb.toString().getBytes());

			// 讀取文件數據
			out.write(file.getBytes());
			// 最後添加換行
			out.write(newLine.getBytes());

			// 定義最後數據分隔線,即--加上BOUNDARY再加上--。
			byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine).getBytes();
			// 寫上結尾標識
			out.write(end_data);
			out.flush();
			out.close();
			// 定義BufferedReader輸入流來讀取URL的響應
			BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			while ((line = reader.readLine()) != null) {
				return line;
			}
		} catch (Exception e) {
			System.out.println("發送POST請求出現異常!" + e);
		}
		return line;
	}
}

2.返回結果實體類

import java.io.Serializable;

public class AccessTokenWX implements Serializable {
	
	private static final long serialVersionUID = 2336546555320388951L;

	private String errCode;
	private String errMsg;

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getErrMsg() {
		return errMsg;
	}

	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	@Override
	public String toString() {
		return "{" +
				"errCode='" + errCode + '\'' +
				", errMsg='" + errMsg + '\'' +
				'}';
	}
}

3.任務處理

import com.alibaba.fastjson.JSON;
import com.tj.common.web.controller.AccessTokenWX;
import com.tj.common.web.controller.UploadAction;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.RecursiveTask;

public class CheckTask extends RecursiveTask<AccessTokenWX> {
    private String url;
    private MultipartFile file;
    private String content;
    public CheckTask(){}

    public CheckTask(String url, MultipartFile file, String content) {
        this.url = url;
        this.file = file;
        this.content = content;
    }

    @Override
    protected AccessTokenWX compute() {
        AccessTokenWX result = new AccessTokenWX();
        try {
            if (null == content){
                String json = UploadAction.uploadFile(url, file);
                result = JSON.parseObject(json, AccessTokenWX.class);
            }else {
                RestTemplate rest = new RestTemplate();
                Map<String,Object> param = new HashMap<String,Object>();
                param.put("content", content);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity requestEntity = new HttpEntity(param, headers);
                ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);
                String json = new String(entity.getBody(),"utf-8");
                result = JSON.parseObject(json, AccessTokenWX.class);
            }
        } catch (Exception e) {
            result.setErrCode("500");
            result.setErrMsg("system錯誤");
        }
        return result;
    }
}

4.檢測工具類

import com.tj.common.web.controller.AccessTokenWX;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;

/**
 * 微信檢測違規圖片或文字接口
 * 2020-04-02 by Jack魏
 */
public class WeiXinViolationCheckUtil {
    /**
     * 微信圖片檢測接口
     */
    static String IMG_CHECK = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=";
    /**
     * 微信文字檢測接口
     */
    static String TXT_CHECK = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=";

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

    /**
     * file:圖片路徑地址
     * content:文字內容
     * token:微信token
     */
    public static Map checkImgOrMsg(MultipartFile file, String content, String token) throws ExecutionException, InterruptedException {
        Map<String, Object> resultMap = new HashMap<>();
        ForkJoinPool pool = new ForkJoinPool();
        String contentUrl = TXT_CHECK + token;
        String imgUrl = IMG_CHECK + token;
        // 返回爲0說明檢測正常
        String ok = "0";
        ForkJoinTask<AccessTokenWX> contentResult = null;
        ForkJoinTask<AccessTokenWX> imgResult = null;
        resultMap.put("all", "ok");

        if (null != content && content.trim().length()>0){
            CheckTask contentTask = new CheckTask(contentUrl, null, content);
            contentResult = pool.submit(contentTask).fork();
            logger.info("文字檢測結果==="+contentResult.get());
        }
        if(null!=file && !file.isEmpty()){
            CheckTask contentTask = new CheckTask(imgUrl, file, null);
            imgResult = pool.submit(contentTask).fork();
            logger.info("圖片檢測結果==="+imgResult.get());
        }
        if (null != contentResult){
            resultMap.put("content", contentResult.get().getErrCode());
            if (!ok.equals(contentResult.get().getErrCode())){
                resultMap.put("all", "err");
            }
        }
        if (null != imgResult){
            resultMap.put("img", imgResult.get().getErrCode());
            if (!ok.equals(imgResult.get().getErrCode())){
                resultMap.put("all", "err");
            }
        }
        return resultMap;
    }
}

5.Controller

import com.tj.common.util.check.WeiXinViolationCheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
@RequestMapping("/sec")
@Api(tags = "文章圖片檢測是否違規")
public class SecurityCheckController {
    String APPID = "XXX";
    String SECRET = "XXX";
    String GET_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+SECRET;
    static Long oldTime = 0L;
    static String tokenCache = "";
    // 過期時間,2h-1m
    static Long outTime = 7200000L - 60000;

    public @ResponseBody String getToken(){
        Long nowTime = System.currentTimeMillis();
        Long cacheTime = nowTime - oldTime;
        if (oldTime <=0 || cacheTime > outTime){
            RestTemplate restTemplate;
            restTemplate = new RestTemplate();
            String json = restTemplate.getForObject(GET_TOKEN, String.class);
            tokenCache = json.split("\"access_token\":\"")[1].split("\",\"")[0].trim();
            oldTime = System.currentTimeMillis();
            return tokenCache;
        }else {
            return tokenCache;
        }
    }

    @ApiOperation("檢測文字或者圖片是否存在違規")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "文字"),
            @ApiImplicitParam(name = "file", value = "圖片上傳路徑")
    })
    @RequestMapping(value = "/checkImgOrMsg", method = RequestMethod.POST)
    public @ResponseBody Map check(@RequestParam(value = "file", required = false) MultipartFile file,
                                   @RequestParam(value = "content", required = false) String content) throws ExecutionException, InterruptedException {
        String token = getToken();
        return WeiXinViolationCheckUtil.checkImgOrMsg(file, content, token);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章