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");