微信分享
parent
faafffaaed
commit
4b7bc6c3dc
@ -0,0 +1,51 @@
|
|||||||
|
package com.jingcheng.template.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.jingcheng.template.service.WeixinService;
|
||||||
|
import com.jingcheng.template.util.AjaxRequest;
|
||||||
|
import com.jingcheng.template.util.AjaxResult;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/wechat")
|
||||||
|
@CrossOrigin
|
||||||
|
public class WechatController {
|
||||||
|
@Resource
|
||||||
|
WeixinService weixinService;
|
||||||
|
|
||||||
|
@RequestMapping("get-sign")
|
||||||
|
public AjaxResult getSign(@RequestBody AjaxRequest ajaxRequest) {
|
||||||
|
AjaxResult ajaxResult = new AjaxResult();
|
||||||
|
JSONObject data = ajaxRequest.getData();
|
||||||
|
if (data == null) {
|
||||||
|
ajaxResult.setRetcode(AjaxResult.FAILED);
|
||||||
|
ajaxResult.setRetmsg("data missing");
|
||||||
|
return ajaxResult;
|
||||||
|
} else {
|
||||||
|
String url = data.getString("url");
|
||||||
|
if (StringUtils.isEmpty(url)) {
|
||||||
|
ajaxResult.setRetcode(AjaxResult.FAILED);
|
||||||
|
ajaxResult.setRetmsg("url missing");
|
||||||
|
return ajaxResult;
|
||||||
|
} else {
|
||||||
|
Map<String, String> sign = weixinService.getSign(url);
|
||||||
|
ajaxResult.setRetmsg("SUCCESS");
|
||||||
|
ajaxResult.setRetcode(AjaxResult.SUCCESS);
|
||||||
|
ajaxResult.setData(sign);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ajaxResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("refresh-access-token")
|
||||||
|
public void refreshToken(){
|
||||||
|
weixinService.refreshAccessToken();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
package com.jingcheng.template.mapper;
|
||||||
|
|
||||||
|
import com.jingcheng.template.model.WechatConfig;
|
||||||
|
import com.jingcheng.template.util.CommonMapper;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface WechatConfigMapper extends CommonMapper<WechatConfig> {
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package com.jingcheng.template.model;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
@TableName("wechat_config")
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = false)
|
||||||
|
public class WechatConfig extends BaseEntity{
|
||||||
|
private String accessToken;
|
||||||
|
}
|
||||||
@ -0,0 +1,77 @@
|
|||||||
|
package com.jingcheng.template.service;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
|
||||||
|
import com.jingcheng.template.mapper.WechatConfigMapper;
|
||||||
|
import com.jingcheng.template.model.WechatConfig;
|
||||||
|
import com.jingcheng.template.util.HttpRequestUtils;
|
||||||
|
import com.jingcheng.template.util.LogUtils;
|
||||||
|
import com.jingcheng.template.util.Sign;
|
||||||
|
|
||||||
|
import com.sun.org.apache.bcel.internal.generic.NEW;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class WeixinService{
|
||||||
|
|
||||||
|
@Value("${weixin.appId}")
|
||||||
|
private String appId;
|
||||||
|
@Value("${weixin.appSecret}")
|
||||||
|
private String appSecret;
|
||||||
|
@Value("${weixin.getAccessTokenUrl}")
|
||||||
|
private String getAccessTokenUrl;
|
||||||
|
@Value("${wechat.getTicketUrl}")
|
||||||
|
private String getTicketUrl;
|
||||||
|
@Resource
|
||||||
|
private WechatConfigMapper wechatConfigMapper;
|
||||||
|
|
||||||
|
Logger logger = LogUtils.getBussinessLogger();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 刷新accesstoken
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public void refreshAccessToken() {
|
||||||
|
String accessTokenUrl = getAccessTokenUrl + appId + "&secret=" + appSecret;
|
||||||
|
String result = HttpRequestUtils.sendGet(accessTokenUrl);
|
||||||
|
String accessToken = JSON.parseObject(result).getString("access_token");
|
||||||
|
WechatConfig wechatConfig = wechatConfigMapper.selectByPrimaryKey(1L);
|
||||||
|
wechatConfig.setAccessToken(accessToken);
|
||||||
|
wechatConfig.setUpdateDateTime(new Date());
|
||||||
|
wechatConfigMapper.updateByPrimaryKeySelective(wechatConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过accessToken刷新ticket
|
||||||
|
*/
|
||||||
|
public String getTicket() {
|
||||||
|
WechatConfig wechatConfig = wechatConfigMapper.selectByPrimaryKey(1L);
|
||||||
|
String access_token = wechatConfig.getAccessToken();
|
||||||
|
String getTicketNewUrl = getTicketUrl + access_token + "&type=jsapi";
|
||||||
|
String ticketResult = HttpRequestUtils.sendGet(getTicketNewUrl);
|
||||||
|
JSONObject ticketJson = JSONObject.parseObject(ticketResult);
|
||||||
|
String jsapi_ticket = ticketJson.getString("ticket");
|
||||||
|
return jsapi_ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> getSign(String url) {
|
||||||
|
String jsapiTicket = getTicket();
|
||||||
|
logger.info("jsapiTicket:" + jsapiTicket);
|
||||||
|
Map<String, String> sign = Sign.sign(jsapiTicket, url);
|
||||||
|
sign.put("appId", appId);
|
||||||
|
logger.info("sign:" + sign);
|
||||||
|
return sign;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
package com.jingcheng.template.util;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Formatter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CK
|
||||||
|
* 2020/8/12
|
||||||
|
* 微信JS-SDK使用权限签名算法
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class Sign {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String jsapi_ticket = "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg";
|
||||||
|
|
||||||
|
// 注意 URL 一定要动态获取,不能 hardcode
|
||||||
|
String url = "http://mp.weixin.qq.com?params=value";
|
||||||
|
Map<String, String> ret = sign(jsapi_ticket, url);
|
||||||
|
for (Map.Entry<String,String> entry : ret.entrySet()) {
|
||||||
|
System.out.println(entry.getKey() + ", " + entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> sign(String jsapi_ticket, String url) {
|
||||||
|
Map<String, String> ret = new HashMap<String, String>();
|
||||||
|
String nonce_str = create_nonce_str();
|
||||||
|
String timestamp = create_timestamp();
|
||||||
|
String string1;
|
||||||
|
String signature = "";
|
||||||
|
|
||||||
|
//注意这里参数名必须全部小写,且必须有序
|
||||||
|
string1 = "jsapi_ticket=" + jsapi_ticket +
|
||||||
|
"&noncestr=" + nonce_str +
|
||||||
|
"×tamp=" + timestamp +
|
||||||
|
"&url=" + url;
|
||||||
|
// System.out.println(string1);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
|
||||||
|
crypt.reset();
|
||||||
|
crypt.update(string1.getBytes("UTF-8"));
|
||||||
|
signature = byteToHex(crypt.digest());
|
||||||
|
}
|
||||||
|
catch (NoSuchAlgorithmException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
catch (UnsupportedEncodingException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
ret.put("url", url);
|
||||||
|
ret.put("jsapi_ticket", jsapi_ticket);
|
||||||
|
ret.put("nonceStr", nonce_str);
|
||||||
|
ret.put("timestamp", timestamp);
|
||||||
|
ret.put("signature", signature);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String byteToHex(final byte[] hash) {
|
||||||
|
Formatter formatter = new Formatter();
|
||||||
|
for (byte b : hash)
|
||||||
|
{
|
||||||
|
formatter.format("%02x", b);
|
||||||
|
}
|
||||||
|
String result = formatter.toString();
|
||||||
|
formatter.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String create_nonce_str() {
|
||||||
|
return UUID.randomUUID().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String create_timestamp() {
|
||||||
|
return Long.toString(System.currentTimeMillis() / 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue