微信小程序校验上传内容图片合法性,JAVA后端HTTPS调用实现

    技术2022-07-11  87

    微信小程序校验上传内容图片合法性,JAVA后端HTTPS调用实现

    // An highlighted block package org.jeecg.common.wxhttp; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.util.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import com.alibaba.fastjson.JSONObject; /** * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html * 官网 学习资料 * @author songcheng * * 微信 小程序 * 校验 文字 、图片 是否违规 * */ @Service public class RestTemplateToInterface { @Autowired private RestTemplate restTemplate; private static final String RESPONSE_SUCCEED="0"; //返回结果 code private static final String WX_API_TOKEN="WX_API_TOKEN"; //缓存KEY private static final long TOKEN_TIME_OUT_SECOND=60*60*1; //1小时 //官方2小时失效 @Autowired private TokenReq req; @Autowired private RedisUtil redisUtil; /** * 获取微信 API TOKEN校验 * @return */ public String getWxToken(){ String token= (String) redisUtil.get(WX_API_TOKEN); if(StringUtils.isNotBlank((String) token)){ return token; }else{ String url ="https://api.weixin.qq.com/cgi-bin/token"; StringBuffer stringBuffer=new StringBuffer(url); Map<String,String> map=new HashMap<>(); map.put("grant_type", req.getGrant_type()); map.put("appid", req.getAppid()); map.put("secret", req.getSecret()); Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); if (iterator.hasNext()) { stringBuffer.append("?"); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); //过滤value为null,value为null时进行拼接字符串会变成 "null"字符串 if (entry.getValue() != null) { stringBuffer.append(entry).append("&"); } url = stringBuffer.substring(0, stringBuffer.length() - 1); } } GeneralToken responseEntity = restTemplate.getForObject(url, GeneralToken.class); token = responseEntity.getAccess_token(); //设置 redisUtil.set(WX_API_TOKEN, token, TOKEN_TIME_OUT_SECOND); } return token; } /** * 校验 文本内容是否违规 * @return */ public boolean toCheckText(String content,String token){ String url ="https://api.weixin.qq.com/wxa/msg_sec_check?access_token="+token; //header参数 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); //放入body中的json参数 JSONObject obj = new JSONObject(); obj.put("content", content); //组装 HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers); ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, JSONObject.class); JSONObject json = responseEntity.getBody(); if(RESPONSE_SUCCEED.equals(json.getString("errcode"))){ return true; } return false; } /** * 校验 图片内容是否违规 * @return * @throws IOException */ public boolean toCheckImg(MultipartFile file,String token) throws IOException{ String url ="https://api.weixin.qq.com/wxa/img_sec_check?access_token="+token; //header参数 HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("multipart/form-data"); headers.setContentType(type); MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); map.add("media", file.getResource()); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(map, headers); JSONObject json = restTemplate.postForObject(url, request, JSONObject.class); if(RESPONSE_SUCCEED.equals(json.getString("errcode"))){ return true; } return false; } }

    使用springboot自带的HTTP调用请模板

    package org.jeecg.common.wxhttp; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /** * HTTP 请求 调用第三方服务 * @Author: songcheng * @Date: 2020/06/28 */ @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; } }

    Pom文件 引入

    <!--spring restTemplate--> <!-- @ConfigurationProperties annotation processing (metadata for IDEs) 生成spring-configuration-metadata.json类,需要引入此类--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> ```
    Processed: 0.012, SQL: 9