From 8783b25c0669d69a906ee0d0d80fef4058ba2015 Mon Sep 17 00:00:00 2001 From: Revers <0x00stone@gmail.com> Date: Fri, 29 Mar 2024 20:37:01 +0800 Subject: [PATCH] fix wechat share --- .../controller/WeChatAouth2Controller.java | 7 +++- .../luoo/user/dto/request/WeChatShareReq.java | 19 ++++++++++ .../user/service/WeChatAouth2Service.java | 38 +++++++++++++++++-- 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/request/WeChatShareReq.java diff --git a/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java b/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java index 520fca5..b23a2c3 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/WeChatAouth2Controller.java @@ -1,6 +1,8 @@ package com.luoo.user.controller; +import annotation.VerifyParam; import api.Result; +import com.luoo.user.dto.request.WeChatShareReq; import com.luoo.user.service.WeChatAouth2Service; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -24,8 +26,9 @@ public class WeChatAouth2Controller extends BaseController { @ApiOperation(value = "分享获取微信token", notes = "成功后返回token,不成功返回null") @PostMapping("/getShareAccessToken") - public Result getShareAccessToken() { - return weChatAouth2Service.getShareToken(); + public Result getShareAccessToken(@RequestBody @VerifyParam WeChatShareReq weChatShareReq) { + + return weChatAouth2Service.getShareToken(weChatShareReq.getUrl()); } // @ApiOperation(value = "微信登录/注册",notes = "微信登录/注册,成功后返回authorization") diff --git a/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatShareReq.java b/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatShareReq.java new file mode 100644 index 0000000..ee0ec22 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/request/WeChatShareReq.java @@ -0,0 +1,19 @@ +package com.luoo.user.dto.request; + +import annotation.VerifyParam; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + + +/** + * @author Revers. + * @date 2024/03/29 20:32 + **/ + +@Data +public class WeChatShareReq { + + @ApiModelProperty(name = "url", value = "当前网页的URL,不包含#及其后面部分", required = true) + @VerifyParam(required = true) + private String url; +} diff --git a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java index a8fde56..c9f300e 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java +++ b/luoo_user/src/main/java/com/luoo/user/service/WeChatAouth2Service.java @@ -31,6 +31,9 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.UUID; import java.util.concurrent.TimeUnit; /** @@ -63,10 +66,10 @@ public class WeChatAouth2Service { @Autowired private UserInfoDao userInfoDao; - public Result getShareToken(){ + public Result getShareToken(String url){ String ticket = (String)redisTemplate.opsForValue().get("shareTicket"); if(ticket != null && StringUtils.isNotBlank(ticket)) - return Result.success(ticket); + return Result.success(sha1Signature(url,ticket)); Aouth2WeChatStableTokenResponse accessToken = getAccessShareToken(); if(accessToken == null || accessToken.getAccess_token() == null ) @@ -77,7 +80,7 @@ public class WeChatAouth2Service { return Result.failed("分享微信失败"); redisTemplate.opsForValue().set("shareTicket",aouth2WeChatJSAPIResponse.getTicket(),6000,TimeUnit.SECONDS); - return Result.success(aouth2WeChatJSAPIResponse.getTicket()); + return Result.success(sha1Signature(url,aouth2WeChatJSAPIResponse.getTicket())); } // public Result loginOrRegister(WeChatLoginReq loginReq,String ipAddress){ @@ -97,6 +100,35 @@ public class WeChatAouth2Service { // return Result.success(token); // } + + public String sha1Signature(String url,String ticket) { + String signature = "jsapi_ticket=" + ticket + "&noncestr=" + UUID.randomUUID() + + "×tamp=" +System.currentTimeMillis() / 1000 + "&url=" + url; + try { + //指定sha1算法 + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.update(signature.getBytes()); + //获取字节数组 + byte messageDigest[] = digest.digest(); + // Create Hex String + StringBuffer hexString = new StringBuffer(); + // 字节数组转换为 十六进制 数 + for (int i = 0; i < messageDigest.length; i++) { + String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); + if (shaHex.length() < 2) { + hexString.append(0); + } + hexString.append(shaHex); + } + return hexString.toString().toUpperCase(); + + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + + return null; + } + public Result login(WeChatLoginReq loginReq,String ipAddress){ Aouth2WeChatTokenResponse accessToken = getAccessToken(loginReq.getCode());