這篇文章主要講解了“Java后端怎么實(shí)現(xiàn)微信小程序校驗(yàn)信息”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java后端怎么實(shí)現(xiàn)微信小程序校驗(yàn)信息”吧!
我們提供的服務(wù)有:網(wǎng)站制作、做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、天元ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的天元網(wǎng)站制作公司
前端只需要將圖片和內(nèi)容傳過來即可
HttpClient的依賴和json轉(zhuǎn)換的依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency>
用于獲取到access_token后進(jìn)行轉(zhuǎn)換,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 { /** * 圖片違規(guī)檢測(cè),對(duì)外提供,直接使用 * * @param accessToken * @param file * @return */ public static Boolean imgFilter(String accessToken, MultipartFile file) { String contentType = file.getContentType(); return checkPic(file, accessToken, contentType); } /** * 文本違規(guī)檢測(cè),對(duì)外提供,直接使用 * * @param accessToken * @param content * @return */ public static Boolean cotentFilter(String accessToken, String content) { return checkContent(accessToken, content); } /** * 校驗(yàn)圖片是否有敏感信息 * * @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");// 轉(zhuǎn)成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); return true; } } /** * 校驗(yàn)內(nèi)容是否有敏感信息 * @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<String, String> 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");// 轉(zhuǎn)成string JSONObject jso = JSONObject.parseObject(result); return getResult(jso); } catch (Exception e) { e.printStackTrace(); return true; } } /** * 返回狀態(tài)信息,可以修改為自己的邏輯 * @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");// 轉(zhuǎn)成string accessTokenVO = JSONObject.parseObject(result, AccessTokenVO.class); //返回token return accessTokenVO.getAccess_token(); } catch (IOException e) { e.printStackTrace(); } return null; } }
import com.itheima.fete.utils.SenInfoCheckUtil; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; /** * @author 兮赫 * 校驗(yàn)內(nèi)容是否敏感 */ @RestController @RequestMapping("/check") public class CheckController { /** * 校驗(yàn)內(nèi)容 * @param content * @return * @throws IOException */ @GetMapping("/content/{content}") public Boolean checkContent(@PathVariable String content) { String accessToken = SenInfoCheckUtil.getAccessToken(); return SenInfoCheckUtil.cotentFilter(accessToken, content); } /** * 校驗(yàn)圖片 * @param multipartFile * @return */ @PostMapping("/image") public Boolean checkImage(@RequestPart(value = "file") MultipartFile multipartFile){ String accessToken = SenInfoCheckUtil.getAccessToken(); return SenInfoCheckUtil.imgFilter(accessToken, multipartFile); } }
感謝各位的閱讀,以上就是“Java后端怎么實(shí)現(xiàn)微信小程序校驗(yàn)信息”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java后端怎么實(shí)現(xiàn)微信小程序校驗(yàn)信息這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
網(wǎng)站欄目:Java后端怎么實(shí)現(xiàn)微信小程序校驗(yàn)信息
本文路徑:http://www.rwnh.cn/article49/jscihh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站內(nèi)鏈、動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)