微信ticket问题
This commit is contained in:
parent
1e749fe5eb
commit
3cd9cb27a9
|
|
@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>微信服务端接口</p>
|
||||
* @author clunt
|
||||
|
|
@ -49,8 +51,8 @@ public class WxController {
|
|||
@ApiOperation(value = "获取jsapi_ticket", httpMethod = "GET")
|
||||
@GetMapping("/getWxTicket")
|
||||
@ResponseBody
|
||||
public Result<String> getWxTicket(){
|
||||
return Result.success(wxService.getWxTicket());
|
||||
public Result<Map<String, String>> getWxTicket(@RequestParam(value = "url") String url){
|
||||
return Result.success(wxService.getWxTicket(url));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package com.playlet.web.service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>微信service类</p>
|
||||
* @author clunt
|
||||
|
|
@ -14,6 +16,6 @@ public interface WxService {
|
|||
|
||||
String getWxInfo(String openId, String accessToken);
|
||||
|
||||
String getWxTicket();
|
||||
Map<String, String> getWxTicket(String requestUrl);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,12 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Formatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
|
|
@ -62,7 +68,7 @@ public class WxServiceImpl implements WxService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String getWxTicket() {
|
||||
public Map<String, String> getWxTicket(String requestUrl) {
|
||||
String accessTokenResult = HttpUtils.sendGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx2b7d9259c1188067&secret=919165aae35838480986555f6c03ae6f");
|
||||
log.info("accessTokenResult : {} ", accessTokenResult);
|
||||
String accessToken = JSONObject.parseObject(accessTokenResult).getString("access_token");
|
||||
|
|
@ -70,6 +76,58 @@ public class WxServiceImpl implements WxService {
|
|||
log.info("调用微信获取Ticket,入参url:{}", url);
|
||||
String result = HttpUtils.sendGet(url);
|
||||
log.info("调用微信获取Ticket,响应内容:{}", result);
|
||||
String ticket = JSONObject.parseObject(result).getString("ticket");
|
||||
//生成11位时间戳
|
||||
long time11 = System.currentTimeMillis() / 1000;
|
||||
String timestamp = String.valueOf(time11);
|
||||
//生成16位随机字符串
|
||||
String nonce = create16String();
|
||||
String string1 = "jsapi_ticket=" + ticket + "&noncestr=" + nonce + "×tamp=" + timestamp + "&url=" + requestUrl;
|
||||
// 2.1这里利用了hutool的加密工具类
|
||||
log.info("使用sha1加密前的细信息:===>【{}】", string1);
|
||||
String signature = "";
|
||||
try {
|
||||
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
||||
crypt.reset();
|
||||
crypt.update(string1.getBytes("UTF-8"));
|
||||
signature = encryptSha1(crypt.digest());
|
||||
} catch (Exception e) {
|
||||
log.info(e.getMessage());
|
||||
}
|
||||
Map<String,String> resultMap = new HashMap();
|
||||
resultMap.put("signature", signature);
|
||||
resultMap.put("timestamp", timestamp);
|
||||
resultMap.put("nonce", nonce);
|
||||
resultMap.put("url", url);
|
||||
resultMap.put("ticket", ticket);
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 生成微信sha1方法签名
|
||||
**/
|
||||
public static String encryptSha1(final byte[] hash) {
|
||||
Formatter formatter = new Formatter();
|
||||
for (byte b : hash) {
|
||||
formatter.format("%02x", b);
|
||||
}
|
||||
String result = formatter.toString();
|
||||
formatter.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 生成16位随机大小写加数字的字符串
|
||||
**/
|
||||
public static String create16String(){
|
||||
String a = "ZXCVBNMASDFGHJKLQWERTYUIOPzxcvbnmasdfghjklqwertyuiop0123456789";
|
||||
StringBuilder con = new StringBuilder();
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
con.append(a.charAt(random.nextInt(62)));
|
||||
}
|
||||
return con.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue