Merge pull request #160 from sutra/sec-check
Implement WeChat Mini Program Sec Check API
This commit is contained in:
commit
bd9e832f47
2
pom.xml
2
pom.xml
@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<name>weixin4j</name>
|
<name>weixin4j</name>
|
||||||
<url>https://github.com/foxinmy/weixin4j</url>
|
<url>https://github.com/foxinmy/weixin4j</url>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-base</artifactId>
|
<artifactId>weixin4j-base</artifactId>
|
||||||
<name>weixin4j-base</name>
|
<name>weixin4j-base</name>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<artifactId>weixin4j-example</artifactId>
|
<artifactId>weixin4j-example</artifactId>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-mp</artifactId>
|
<artifactId>weixin4j-mp</artifactId>
|
||||||
<name>weixin4j-mp</name>
|
<name>weixin4j-mp</name>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-qy</artifactId>
|
<artifactId>weixin4j-qy</artifactId>
|
||||||
<name>weixin4j-qy</name>
|
<name>weixin4j-qy</name>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-server</artifactId>
|
<artifactId>weixin4j-server</artifactId>
|
||||||
<version>1.1.9</version>
|
<version>1.1.9</version>
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-serverX</artifactId>
|
<artifactId>weixin4j-serverX</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.1</version>
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>com.foxinmy</groupId>
|
<groupId>com.foxinmy</groupId>
|
||||||
<artifactId>weixin4j</artifactId>
|
<artifactId>weixin4j</artifactId>
|
||||||
<version>1.8.2</version>
|
<version>1.9.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>weixin4j-wxa</artifactId>
|
<artifactId>weixin4j-wxa</artifactId>
|
||||||
<name>weixin4j-wxa</name>
|
<name>weixin4j-wxa</name>
|
||||||
|
|||||||
@ -12,6 +12,7 @@ import com.foxinmy.weixin4j.token.TokenManager;
|
|||||||
import com.foxinmy.weixin4j.wxa.api.CustomMessageApi;
|
import com.foxinmy.weixin4j.wxa.api.CustomMessageApi;
|
||||||
import com.foxinmy.weixin4j.wxa.api.LoginApi;
|
import com.foxinmy.weixin4j.wxa.api.LoginApi;
|
||||||
import com.foxinmy.weixin4j.wxa.api.QrCodeApi;
|
import com.foxinmy.weixin4j.wxa.api.QrCodeApi;
|
||||||
|
import com.foxinmy.weixin4j.wxa.api.SecCheckApi;
|
||||||
import com.foxinmy.weixin4j.wxa.api.TemplateApi;
|
import com.foxinmy.weixin4j.wxa.api.TemplateApi;
|
||||||
import com.foxinmy.weixin4j.wxa.api.TemplateMessageApi;
|
import com.foxinmy.weixin4j.wxa.api.TemplateMessageApi;
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ public class WeixinAppFacade {
|
|||||||
private final TemplateApi templateApi;
|
private final TemplateApi templateApi;
|
||||||
private final TemplateMessageApi templateMessageApi;
|
private final TemplateMessageApi templateMessageApi;
|
||||||
private final CustomMessageApi customMessageApi;
|
private final CustomMessageApi customMessageApi;
|
||||||
|
private final SecCheckApi secCheckApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs {@link WeixinAppFacade} using {@link FileCacheStorager}.
|
* Constructs {@link WeixinAppFacade} using {@link FileCacheStorager}.
|
||||||
@ -108,6 +110,7 @@ public class WeixinAppFacade {
|
|||||||
this.templateApi = new TemplateApi(tokenManager, properties);
|
this.templateApi = new TemplateApi(tokenManager, properties);
|
||||||
this.templateMessageApi = new TemplateMessageApi(tokenManager, properties);
|
this.templateMessageApi = new TemplateMessageApi(tokenManager, properties);
|
||||||
this.customMessageApi = new CustomMessageApi(tokenManager, properties);
|
this.customMessageApi = new CustomMessageApi(tokenManager, properties);
|
||||||
|
this.secCheckApi = new SecCheckApi(tokenManager, properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -155,4 +158,14 @@ public class WeixinAppFacade {
|
|||||||
return customMessageApi;
|
return customMessageApi;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内容安全相关的 API。
|
||||||
|
*
|
||||||
|
* @return 内容安全相关的 API。
|
||||||
|
* @since 1.9
|
||||||
|
*/
|
||||||
|
public SecCheckApi getSecCheckApi() {
|
||||||
|
return secCheckApi;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,98 @@
|
|||||||
|
package com.foxinmy.weixin4j.wxa.api;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
|
import com.foxinmy.weixin4j.http.apache.content.InputStreamBody;
|
||||||
|
import com.foxinmy.weixin4j.http.apache.mime.FormBodyPart;
|
||||||
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
|
import com.foxinmy.weixin4j.token.TokenManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 违法违规内容检查。
|
||||||
|
*
|
||||||
|
* @since 1.9
|
||||||
|
*/
|
||||||
|
public class SecCheckApi extends TokenManagerApi {
|
||||||
|
|
||||||
|
public SecCheckApi(TokenManager tokenManager) {
|
||||||
|
super(tokenManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SecCheckApi(TokenManager tokenManager, Properties properties) {
|
||||||
|
super(tokenManager, properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
private InputStream scale(InputStream inputStream, int maxWidth, int maxHeight) throws IOException {
|
||||||
|
final BufferedImage srcBufferedImage = ImageIO.read(inputStream);
|
||||||
|
|
||||||
|
final int srcWidth = srcBufferedImage.getWidth();
|
||||||
|
final int srcHeight = srcBufferedImage.getHeight();
|
||||||
|
final float scale = Math.min((float) maxWidth / (float) srcWidth, (float) maxHeight / (float) srcHeight);
|
||||||
|
|
||||||
|
final BufferedImage scaledBufferedImage;
|
||||||
|
if (scale < 1F) {
|
||||||
|
final int width = (int) (srcWidth * scale);
|
||||||
|
final int height = (int) (srcHeight * scale);
|
||||||
|
final Image scaledImage = srcBufferedImage.getScaledInstance(width, height, Image.SCALE_DEFAULT);
|
||||||
|
|
||||||
|
scaledBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
|
scaledBufferedImage.getGraphics().drawImage(scaledImage, 0, 0 , null);
|
||||||
|
} else {
|
||||||
|
scaledBufferedImage = srcBufferedImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
ImageIO.write(scaledBufferedImage, "png", outputStream);
|
||||||
|
return new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验一张图片是否含有违法违规内容。
|
||||||
|
*
|
||||||
|
* @param inputStream the image input stream.
|
||||||
|
* @throws WeixinException indicates getting access token failed, or the content is risky.
|
||||||
|
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/imgSecCheck.html">校验一张图片是否含有违法违规内容</a>
|
||||||
|
*/
|
||||||
|
public void imgSecCheck(InputStream inputStream) throws WeixinException {
|
||||||
|
final String imgSecCheckUri = this.getAccessTokenRequestUri("wxa_img_sec_check");
|
||||||
|
final InputStreamBody inputStreamBody = new InputStreamBody(inputStream, "media");
|
||||||
|
final FormBodyPart formBodyPart = new FormBodyPart("media", inputStreamBody);
|
||||||
|
final WeixinResponse response = weixinExecutor.post(imgSecCheckUri, formBodyPart);
|
||||||
|
final WxaApiResult r = response.getAsObject(WxaApiResult.TYPE_REFERENCE);
|
||||||
|
r.checkErrCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void imgSecCheck(InputStream inputStream, int maxWidth, int maxHeight) throws WeixinException {
|
||||||
|
try {
|
||||||
|
this.imgSecCheck(this.scale(inputStream, maxWidth, maxHeight));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WeixinException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查一段文本是否含有违法违规内容。
|
||||||
|
*
|
||||||
|
* @param content 要检测的文本内容,长度不超过 500KB,编码格式为 UTF-8。
|
||||||
|
* @throws WeixinException indicates getting access token failed, or the content is risky.
|
||||||
|
* @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api/msgSecCheck.html">检查一段文本是否含有违法违规内容</a>
|
||||||
|
*/
|
||||||
|
public void msgSecCheck(String content) throws WeixinException {
|
||||||
|
final Map<String, String> params = new HashMap<String, String>(1);
|
||||||
|
params.put("content", content);
|
||||||
|
final WxaApiResult r = this.post("wxa_msg_sec_check", params, WxaApiResult.TYPE_REFERENCE);
|
||||||
|
r.checkErrCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@
|
|||||||
# ----------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------
|
||||||
|
|
||||||
api_base_url=https://api.weixin.qq.com
|
api_base_url=https://api.weixin.qq.com
|
||||||
|
api_wxa_url={api_base_url}/wxa
|
||||||
api_cgi_url={api_base_url}/cgi-bin
|
api_cgi_url={api_base_url}/cgi-bin
|
||||||
|
|
||||||
# \u767b\u5f55\u51ed\u8bc1\u6821\u9a8c
|
# \u767b\u5f55\u51ed\u8bc1\u6821\u9a8c
|
||||||
@ -10,10 +11,10 @@ sns_jscode2session={api_base_url}/sns/jscode2session?appid=%s&secret=%s&js_code=
|
|||||||
|
|
||||||
|
|
||||||
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3A: \u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
|
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3A: \u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
|
||||||
wxa_getwxacode={api_base_url}/wxa/getwxacode?access_token=%s
|
wxa_getwxacode={api_wxa_url}/getwxacode?access_token=%s
|
||||||
|
|
||||||
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3B\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u6781\u591a\u7684\u4e1a\u52a1\u573a\u666f
|
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u7801 \u63a5\u53e3B\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u6781\u591a\u7684\u4e1a\u52a1\u573a\u666f
|
||||||
wxa_getwxacodeunlimit={api_base_url}/wxa/getwxacodeunlimit?access_token=%s
|
wxa_getwxacodeunlimit={api_wxa_url}/getwxacodeunlimit?access_token=%s
|
||||||
|
|
||||||
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u4e8c\u7ef4\u7801 \u63a5\u53e3C\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
|
# \u83b7\u53d6\u5c0f\u7a0b\u5e8f\u4e8c\u7ef4\u7801 \u63a5\u53e3C\uff1a\u9002\u7528\u4e8e\u9700\u8981\u7684\u7801\u6570\u91cf\u8f83\u5c11\u7684\u4e1a\u52a1\u573a\u666f
|
||||||
wxaapp_createwxaqrcode={api_base_url}/cgi-bin/wxaapp/createwxaqrcode?access_token=%s
|
wxaapp_createwxaqrcode={api_base_url}/cgi-bin/wxaapp/createwxaqrcode?access_token=%s
|
||||||
@ -42,4 +43,8 @@ wxopen_template_message_send={api_cgi_url}/message/wxopen/template/send?access_t
|
|||||||
message_custom_send={api_cgi_url}/message/custom/send?access_token=%s
|
message_custom_send={api_cgi_url}/message/custom/send?access_token=%s
|
||||||
|
|
||||||
# \u5ba2\u670d\u8f93\u5165\u72b6\u6001
|
# \u5ba2\u670d\u8f93\u5165\u72b6\u6001
|
||||||
message_custom_typing={api_cgi_url}/message/custom/typing?access_token=%s
|
message_custom_typing={api_cgi_url}/message/custom/typing?access_token=%
|
||||||
|
# imgSecCheck
|
||||||
|
wxa_img_sec_check={api_wxa_url}/img_sec_check?access_token=%s
|
||||||
|
# msgSecCheck
|
||||||
|
wxa_msg_sec_check={api_wxa_url}/msg_sec_check?access_token=%s
|
||||||
Loading…
x
Reference in New Issue
Block a user