增加微信授权域名验证

完成微信公众号支付前半段
This commit is contained in:
clunt 2022-04-10 18:26:49 +08:00
parent f1618ce278
commit 7fbee4f068
4 changed files with 57 additions and 1 deletions

View File

@ -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");
}
/**
* 验证微信签名
*/

View File

@ -0,0 +1 @@
bRFuvYpyQ4WLr0on

View File

@ -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;
}
}

View File

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