From 7fbee4f0684b7df31111ba399a115ef9291c6161 Mon Sep 17 00:00:00 2001 From: clunt Date: Sun, 10 Apr 2022 18:26:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E5=9F=9F=E5=90=8D=E9=AA=8C=E8=AF=81=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E5=89=8D=E5=8D=8A=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ghy/web/controller/tool/WxController.java | 12 ++++- .../static/MP_verify_bRFuvYpyQ4WLr0on.txt | 1 + .../java/com/ghy/common/utils/WxUtils.java | 44 +++++++++++++++++++ .../com/ghy/framework/config/ShiroConfig.java | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 ghy-admin/src/main/resources/static/MP_verify_bRFuvYpyQ4WLr0on.txt create mode 100644 ghy-common/src/main/java/com/ghy/common/utils/WxUtils.java diff --git a/ghy-admin/src/main/java/com/ghy/web/controller/tool/WxController.java b/ghy-admin/src/main/java/com/ghy/web/controller/tool/WxController.java index ebb68044..191819af 100644 --- a/ghy-admin/src/main/java/com/ghy/web/controller/tool/WxController.java +++ b/ghy-admin/src/main/java/com/ghy/web/controller/tool/WxController.java @@ -1,6 +1,8 @@ package com.ghy.web.controller.tool; +import com.alibaba.fastjson.JSONObject; import com.ghy.common.core.controller.BaseController; +import com.ghy.common.utils.WxUtils; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,7 +20,7 @@ public class WxController extends BaseController { @GetMapping("/token") @ResponseBody - public String demo3(String timestamp, String nonce, String signature, String echostr, HttpServletRequest request) throws IOException { + public String token(String timestamp, String nonce, String signature, String echostr) throws IOException { String token = "gqz"; boolean checkSignature = checkSignature(signature, timestamp, nonce, token); if (checkSignature) { @@ -28,6 +30,14 @@ public class WxController extends BaseController { } } + @GetMapping("/openid") + @ResponseBody + public String openId(HttpServletRequest request) throws Exception{ + String code = request.getParameter("code"); + JSONObject wxUser = WxUtils.getOpenid(code); + return wxUser.getString("openid"); + } + /** * 验证微信签名 */ diff --git a/ghy-admin/src/main/resources/static/MP_verify_bRFuvYpyQ4WLr0on.txt b/ghy-admin/src/main/resources/static/MP_verify_bRFuvYpyQ4WLr0on.txt new file mode 100644 index 00000000..5b0d4663 --- /dev/null +++ b/ghy-admin/src/main/resources/static/MP_verify_bRFuvYpyQ4WLr0on.txt @@ -0,0 +1 @@ +bRFuvYpyQ4WLr0on \ No newline at end of file diff --git a/ghy-common/src/main/java/com/ghy/common/utils/WxUtils.java b/ghy-common/src/main/java/com/ghy/common/utils/WxUtils.java new file mode 100644 index 00000000..1b4a40f1 --- /dev/null +++ b/ghy-common/src/main/java/com/ghy/common/utils/WxUtils.java @@ -0,0 +1,44 @@ +package com.ghy.common.utils; + +import com.alibaba.fastjson.JSONObject; + +import javax.net.ssl.HttpsURLConnection; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; + +public class WxUtils { + + public static String getOpenIdUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; + + /*通过code获取用户openid*/ + public static JSONObject getOpenid(String code) throws IOException { + JSONObject jsonObject = null; + String path = getOpenIdUrl.replace("APPID", "wx404f2439a8c24e15").replace("SECRET", "49ade04a817067fe2d65ab2f17afce75").replace("CODE", code); + StringBuffer buffer = new StringBuffer(); + URL url = new URL(path); + HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); + httpUrlConn.setRequestMethod("POST"); + httpUrlConn.setDoOutput(true); + httpUrlConn.setDoInput(true); + httpUrlConn.setUseCaches(false); + // 将返回的输入流转换成字符串 + InputStream inputStream = httpUrlConn.getInputStream(); + InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + String str = null; + while ((str = bufferedReader.readLine()) != null) { + buffer.append(str); + } + bufferedReader.close(); + inputStreamReader.close(); + // 释放资源 + inputStream.close(); + inputStream = null; + httpUrlConn.disconnect(); + jsonObject = JSONObject.parseObject(buffer.toString()); + return jsonObject; + } +} diff --git a/ghy-framework/src/main/java/com/ghy/framework/config/ShiroConfig.java b/ghy-framework/src/main/java/com/ghy/framework/config/ShiroConfig.java index 1ddd5113..3dff55eb 100644 --- a/ghy-framework/src/main/java/com/ghy/framework/config/ShiroConfig.java +++ b/ghy-framework/src/main/java/com/ghy/framework/config/ShiroConfig.java @@ -278,6 +278,7 @@ public class ShiroConfig //部分接口不需要登陆校验 filterChainDefinitionMap.put("/wx/**", "anon"); // 对静态资源设置匿名访问 + filterChainDefinitionMap.put("/MP_verify_bRFuvYpyQ4WLr0on.txt", "anon"); filterChainDefinitionMap.put("/favicon.ico**", "anon"); filterChainDefinitionMap.put("/ruoyi.png**", "anon"); filterChainDefinitionMap.put("/html/**", "anon");