Java后端怎么实现微信小程序校验信息
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要讲解了"Java后端怎么实现微信小程序校验信息",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java后端怎么实现微信小程序校验信息"吧!前
千家信息网最后更新 2026年11月03日Java后端怎么实现微信小程序校验信息
这篇文章主要讲解了"Java后端怎么实现微信小程序校验信息",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java后端怎么实现微信小程序校验信息"吧!
前端只需要将图片和内容传过来即可
pom依赖
HttpClient的依赖和json转换的依赖
com.alibaba fastjson 1.2.54 org.apache.httpcomponents httpclient 4.5.10
封装Vo类
用于获取到access_token后进行转换,access_token是什么就不用我多说了吧
public class AccessTokenVO { private String access_token; private Integer expires_in; //记得给get set方法}封装工具类
import com.alibaba.fastjson.JSONObject;import com.itheima.fete.pojo.AccessTokenVO;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.springframework.web.multipart.MultipartFile;import java.io.*;import java.util.*;/** * @author 兮赫 */public class SenInfoCheckUtil { /** * 图片违规检测,对外提供,直接使用 * * @param accessToken * @param file * @return */ public static Boolean imgFilter(String accessToken, MultipartFile file) { String contentType = file.getContentType(); return checkPic(file, accessToken, contentType); } /** * 文本违规检测,对外提供,直接使用 * * @param accessToken * @param content * @return */ public static Boolean cotentFilter(String accessToken, String content) { return checkContent(accessToken, content); } /** * 校验图片是否有敏感信息 * * @param multipartFile * @return */ private static Boolean checkPic(MultipartFile multipartFile, String accessToken, String contentType) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; HttpPost request = new HttpPost("https://api.weixin.qq.com/wxa/img_sec_check?access_token=" + accessToken); request.addHeader("Content-Type", "application/octet-stream"); InputStream inputStream = multipartFile.getInputStream(); byte[] byt = new byte[inputStream.available()]; inputStream.read(byt); request.setEntity(new ByteArrayEntity(byt, ContentType.create(contentType))); response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); return true; } } /** * 校验内容是否有敏感信息 * @param accessToken * @param content * @return */ private static Boolean checkContent(String accessToken, String content) { try { CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; HttpPost request = new HttpPost("https://api.weixin.qq.com/wxa/msg_sec_check?access_token=" + accessToken); request.addHeader("Content-Type", "application/json"); Map map = new HashMap<>(); map.put("content", content); String body = JSONObject.toJSONString(map); request.setEntity(new StringEntity(body, ContentType.create("text/json", "UTF-8"))); response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); return true; } } /** * 返回状态信息,可以修改为自己的逻辑 * @param jso * @return */ private static Boolean getResult(JSONObject jso) { Object errcode = jso.get("errcode"); int errCode = (int) errcode; if (errCode == 0) { return true; } else if (errCode == 87014) { return false; } else { return false; } } /** * 获取小程序的 access_token * @return */ public static String getAccessToken() { AccessTokenVO accessTokenVO = null; try { CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; //改成自己的appid和secret HttpGet request = new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbh0594d32gf315&secret=c9864f6e8aafg8313b6d5d608bd6a6b"); request.addHeader("Content-Type", "application/json"); response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String result = EntityUtils.toString(httpEntity, "UTF-8");// 转成string accessTokenVO = JSONObject.parseObject(result, AccessTokenVO.class); //返回token return accessTokenVO.getAccess_token(); } catch (IOException e) { e.printStackTrace(); } return null; }} 编写Controller层
import com.itheima.fete.utils.SenInfoCheckUtil;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import java.io.IOException;/** * @author 兮赫 * 校验内容是否敏感 */@RestController@RequestMapping("/check")public class CheckController { /** * 校验内容 * @param content * @return * @throws IOException */ @GetMapping("/content/{content}") public Boolean checkContent(@PathVariable String content) { String accessToken = SenInfoCheckUtil.getAccessToken(); return SenInfoCheckUtil.cotentFilter(accessToken, content); } /** * 校验图片 * @param multipartFile * @return */ @PostMapping("/image") public Boolean checkImage(@RequestPart(value = "file") MultipartFile multipartFile){ String accessToken = SenInfoCheckUtil.getAccessToken(); return SenInfoCheckUtil.imgFilter(accessToken, multipartFile); }}感谢各位的阅读,以上就是"Java后端怎么实现微信小程序校验信息"的内容了,经过本文的学习后,相信大家对Java后端怎么实现微信小程序校验信息这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
信息
内容
程序
图片
UTF-8
学习
对外
封装
检测
违规
不用
前端
就是
工具
思路
情况
文本
文章
方法
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网高科技
今日互联网科技
互联网和科技创新
美国互联网科技巨头监管
上海互联网科技有限公司
北京互联网科技有限公司
万易互联网科技有限公司
撒么互联网科技公司
互联网前沿科技
哈尔滨新华互联网科技
华中科技有哪些互联网大佬
互联网之后的科技是什么
福建科技产业互联网发展
青岛科技大学互联网大赛研究生
互联网科技商业
互联网时代的科技公司
互联网科技融合创新
易租星互联网科技有限公司
数字科技和互联网金融哪个好
互联网领先科技成果宣传视频
中智农互联网科技
互联网的科技
为想互联网科技
中国互联网科技发展
华中科技大学互联网
四个字互联网科技公司取名
新兴科技互联网创新大赛颁奖典礼
深圳艾果互联网通讯科技
互联网科技年终奖
科技和互联网行业的特点