springboot 2.3.4.RELEASE
import java.util.*;/*** @Title: ExpiryMap 可以设置过期时间的Map* @description ExpiryMap继承至HashMap 重写了所有对外的方法,对每个key值都设置了有效期* @Author: xx* @Version: 1.0*/public class ExpiryMap<K, V> extends HashMap<K, V> {private static final long serialVersionUID = 1L;/*** default expiry time 2s*/private long EXPIRY = 1000 * 2;private HashMap<K, Long> expiryMap = new HashMap<>();/** 缓存实例对象 */private volatile static ExpiryMap<String, String> SameUrlMap;/*** 采用单例模式获取实例* @return*/public static ExpiryMap getInstance() {//第一次判空,提高效率if (null == SameUrlMap) {//保证线程安全synchronized (ExpiryMap.class) {//第二次判空,保证单例对象的唯一性,防止第一次有多个线程进入第一个if判断if (null == SameUrlMap) {SameUrlMap = new ExpiryMap<>();}}}return SameUrlMap;}public ExpiryMap(){super();}public ExpiryMap(long defaultExpiryTime){this(1 << 4, defaultExpiryTime);}public ExpiryMap(int initialCapacity, long defaultExpiryTime){super(initialCapacity);this.EXPIRY = defaultExpiryTime;}@Overridepublic V put(K key, V value) {expiryMap.put(key, System.currentTimeMillis() + EXPIRY);return super.put(key, value);}@Overridepublic boolean containsKey(Object key) {return !checkExpiry(key, true) && super.containsKey(key);}/*** @param key* @param value* @param expiryTime 键值对有效期 毫秒* @return*/public V put(K key, V value, long expiryTime) {expiryMap.put(key, System.currentTimeMillis() + expiryTime);return super.put(key, value);}@Overridepublic int size() {return entrySet().size();}@Overridepublic boolean isEmpty() {return entrySet().size() == 0;}@Overridepublic boolean containsValue(Object value) {if (value == null) {return Boolean.FALSE;}Set<Entry<K, V>> set = super.entrySet();Iterator<Entry<K, V>> iterator = set.iterator();while (iterator.hasNext()) {Entry<K, V> entry = iterator.next();if(value.equals(entry.getValue())){if(checkExpiry(entry.getKey(), false)) {iterator.remove();return Boolean.FALSE;}else {return Boolean.TRUE;}}}return Boolean.FALSE;}@Overridepublic Collection<V> values() {Collection<V> values = super.values();if(values == null || values.size() < 1) {return values;}Iterator<V> iterator = values.iterator();while (iterator.hasNext()) {V next = iterator.next();if(!containsValue(next)) {iterator.remove();}}return values;}@Overridepublic V get(Object key) {if (key == null) {return null;}if(checkExpiry(key, true)) {return null;}return super.get(key);}/**** @Description: 是否过期* @param key* @return null:不存在或key为null -1:过期 存在且没过期返回value 因为过期的不是实时删除,所以稍微有点作用*/public Object isInvalid(Object key) {if (key == null) {return null;}if(!expiryMap.containsKey(key)){return null;}long expiryTime = expiryMap.get(key);boolean flag = System.currentTimeMillis() > expiryTime;if(flag){super.remove(key);expiryMap.remove(key);return -1;}return super.get(key);}@Overridepublic void putAll(Map<? extends K, ? extends V> m) {for (Entry<? extends K, ? extends V> e : m.entrySet()) {expiryMap.put(e.getKey(), System.currentTimeMillis() + EXPIRY);}super.putAll(m);}@Overridepublic Set<Entry<K,V>> entrySet() {Set<Entry<K, V>> set = super.entrySet();Iterator<Entry<K, V>> iterator = set.iterator();while (iterator.hasNext()) {Entry<K, V> entry = iterator.next();if(checkExpiry(entry.getKey(), false)) {iterator.remove();}}return set;}/**** @Description: 是否过期* @param expiryTime true 过期* @param isRemoveSuper true super删除* @return*/private boolean checkExpiry(Object key, boolean isRemoveSuper){if(!expiryMap.containsKey(key)){return Boolean.FALSE;}long expiryTime = expiryMap.get(key);boolean flag = System.currentTimeMillis() > expiryTime;if(flag){if(isRemoveSuper) {super.remove(key);}expiryMap.remove(key);}return flag;}public static void main(String[] args) throws InterruptedException {ExpiryMap<String, String> map = new ExpiryMap<>();map.put("test", "xxx");map.put("test2", "ankang", 5000);System.out.println("test==" + map.get("test"));Thread.sleep(3000);System.out.println("test==" + map.get("test"));System.out.println("test2==" + map.get("test2"));Thread.sleep(3000);System.out.println("test2==" + map.get("test2"));}}
import org.springframework.boot.web.client.RestTemplateBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.client.RestTemplate;@Configurationpublic class Config {@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder){return builder.build();}}
/*** appID指政务微信中我的单位信息下边有个CorpId* appsecret指对应的应用入口信息中的secret* ip指私有化部署的政务微信的内网或者外网IP* port指私有化部署的政务微信的内网或者外网端口*/import org.springframework.boot.context.properties.ConfigurationProperties;import org.ponent;@Component@ConfigurationProperties(prefix="wx")public class WeiXinConfig {private String appID;private String appsecret;private String ip;private String port;public String getAppID() {return appID;}public void setAppID(String appID) {this.appID = appID;}public String getAppsecret() {return appsecret;}public void setAppsecret(String appsecret) {this.appsecret = appsecret;}public String getIp() {return ip;}public void setIp(String ip) {this.ip = ip;}public String getPort() {return port;}public void setPort(String port) {this.port = port;}}
import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/weixin")public class WeiXinDemoController {@Autowiredprivate WeiXinService weiXinService;@Autowiredprivate WeiXinConfig weiXinConfig;/*** 初始化微信JSSDK配置信息* @param request* @return* @throws Exception*/@RequestMapping("/initConfigInfo")public String initConfig (HttpServletRequest request) throws Exception{String url = request.getParameter("url");//当前页面URLMap map = weiXinService.initConfigInfo(url);String json = weiXinService.mapToJson(map);return json;}/*** 拼接网页授权链接* @return*/@RequestMapping(value = { "/oauth2Url" })public String Oauth2API(HttpServletRequest request){//获取项目域名// String requestUrl = request.getServerName();// String contextPath = request.getContextPath();//String url = request.getParameter("url");//跳转页面URL// String redirect_uri = "";// try {// redirect_uri = .URLEncoder.encode(url, "utf-8");// } catch (UnsupportedEncodingException e) {// e.printStackTrace();// }// String oauth2Url = "https://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort()+"/connect/oauth2/authorize?appid=" + weiXinConfig.getAppID() + "&redirect_uri=" + redirect_uri//+ "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";return "https://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort();}@RequestMapping(value = { "/getUserinfo" })public String getUserinfo(HttpServletRequest request){String code = request.getParameter("code");//随机码Map map = weiXinService.getUserinfo(code);String json = weiXinService.mapToJson(map);return json;}}
import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.HashMap;import java.util.Map;import java.util.UUID;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.ponent;import org.springframework.stereotype.Service;import org.springframework.util.StringUtils;import org.springframework.web.client.RestTemplate;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;@Component@Servicepublic class WeiXinService {@Autowiredprivate WeiXinConfig weiXinConfig;@Autowiredprivate RestTemplate restTemplate;/*测试*/public Map initConfigInfo(String url) throws Exception {String accessToken = "";if(StringUtils.isEmpty(ExpiryMap.getInstance().get("accessToken"))){accessToken = this.getJSSDKAccessToken();ExpiryMap.getInstance().put("accessToken", accessToken, 7200000);}else{accessToken = ExpiryMap.getInstance().get("accessToken").toString();}String jsapiTicket = "";if(StringUtils.isEmpty(ExpiryMap.getInstance().get("jsapiTicket"))){jsapiTicket = this.getJSSDKJsapiTicket(accessToken);ExpiryMap.getInstance().put("jsapiTicket", jsapiTicket, 7200000);}else{jsapiTicket = ExpiryMap.getInstance().get("jsapiTicket").toString();}System.out.println("accessToken==" + ExpiryMap.getInstance().get("accessToken"));System.out.println("jsapiTicket==" + ExpiryMap.getInstance().get("jsapiTicket"));String timestamp = Long.toString(System.currentTimeMillis() / 1000);String nonceStr = UUID.randomUUID().toString();String signature = this.buildJSSDKSignature(jsapiTicket,timestamp,nonceStr,url);Map<String,String> map = new HashMap<String,String>();map.put("url", url);map.put("jsapi_ticket", jsapiTicket);map.put("nonceStr", nonceStr);map.put("timestamp", timestamp);map.put("signature", signature);map.put("appid", weiXinConfig.getAppID());return map;}public String getJSSDKAccessToken() {String token = null;String url = "http://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort()+"/cgi-bin/gettoken?corpid="+weiXinConfig.getAppID()+"&corpsecret="+weiXinConfig.getAppsecret();String json = postRequestForWeiXinService(url);Map map = jsonToMap(json);if (map != null) {token = (String) map.get("access_token");}return token;}public String getJSSDKJsapiTicket(String token) {String url = "http://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort()+"/cgi-bin/get_jsapi_ticket?access_token="+token;String json = postRequestForWeiXinService(url);Map map = jsonToMap(json);String jsapi_ticket = null;if (map != null) {jsapi_ticket = (String) map.get("ticket");}return jsapi_ticket;}//获取用户idpublic Map getUserinfo(String code) {String url = "http://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort()+"/cgi-bin/user/getuserinfo?access_token="+ExpiryMap.getInstance().get("accessToken").toString()+"&code="+code;String json = getRequestForWeiXinService(url);Map map = jsonToMap(json);String userId = "";if (map != null) {userId = (String) map.get("UserId");}if(!"".equals(userId)){String url_ = "http://"+weiXinConfig.getIp()+":"+weiXinConfig.getPort()+"/cgi-bin/user/get?access_token="+ExpiryMap.getInstance().get("accessToken").toString()+"&userid="+userId;String json_ = getRequestForWeiXinService(url_);map = jsonToMap(json_);}return map;}/*** 构建分享链接的签名。* @param ticket* @param nonceStr* @param timestamp* @param url* @return* @throws Exception*/public static String buildJSSDKSignature(String ticket,String timestamp,String nonceStr ,String url) throws Exception {String orderedString = "jsapi_ticket=" + ticket+ "&noncestr=" + nonceStr + "×tamp=" + timestamp+ "&url=" + url;System.out.println("noncestr=" + nonceStr);System.out.println("timestamp=" + timestamp);return SHA1(orderedString);}public static String SHA1(String str) {try {MessageDigest digest = MessageDigest.getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可digest.update(str.getBytes());byte messageDigest[] = digest.digest();// Create Hex StringStringBuffer hexStr = new StringBuffer();// 字节数组转换为 十六进制 数for (int i = 0; i < messageDigest.length; i++) {String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);if (shaHex.length() < 2) {hexStr.append(0);}hexStr.append(shaHex);}return hexStr.toString();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}/** * 将字节数组转换为十六进制字符串 * * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) { strDigest += byteToHexStr(byteArray[i]); } return strDigest; } /** * 将字节转换为十六进制字符串 * * @param mByte * @return */ private static String byteToHexStr(byte mByte) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] tempArr = new char[2]; tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; tempArr[1] = Digit[mByte & 0X0F]; String s = new String(tempArr); return s; }public String mapToJson(Map map){Gson gson = new Gson();String json = gson.toJson(map);return json;}private Map jsonToMap(String json) {Gson gons = new Gson();Map map = gons.fromJson(json, new TypeToken<Map>(){}.getType());return map;}private String postRequestForWeiXinService(String getAccessTokenUrl) {ResponseEntity<String> postForEntity = restTemplate.postForEntity(getAccessTokenUrl, null, String.class);String json = postForEntity.getBody();return json;}private String getRequestForWeiXinService(String getUserInfoUrl) {ResponseEntity<String> postForEntity = restTemplate.getForEntity(getUserInfoUrl.toString(), String.class);String json = postForEntity.getBody();return json;}}
/***页面写一个H5页面即可**/1、引入js文件(可以引入在线,也可以把在线的js下载下来放到本地引入【内网环境】)<script src="js/jweixin-1.2.0.js" type="text/javascript" charset="utf-8"></script>2、验证是否能够联通var url = (window.location.href).split('#')[0];$.ajax({url: FX.basePath+"weixin/initConfigInfo?url="+url,contentType: "application/json;charset=UTF-8",type: 'GET',dataType:'json',success: function (text) {console.log(text);wx.config({beta: true,// 调用wx.invoke形式的接口值时,该值必须为true。debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。appId: text.appid, // 必填,政务微信的cropIDtimestamp: text.timestamp, // 必填,生成签名的时间戳nonceStr: text.nonceStr, // 必填,生成签名的随机串signature: text.signature,// 必填,签名,见附录1jsApiList: ['openUserProfile','getAllPhoneContacts'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2});},error: function (jqXHR, textStatus, errorThrown) {}});3、拼接授权网页,此处可以在页面设置一个按钮,点击触发这个拼接网页的功能,也可以在wx.ready(function(){});中写。注意:如果是私有化部署的政务微信,需要把下边域名改成私有化部署的地址location.href = 'https://open./connect/oauth2/authorize?appid=wwf7b551efdf3701d5&redirect_uri='+localUrl+'&response_type=code&scope=snsapi_base&agentid=1000006&state=asrt123#wechat_redirect'function urlencode (str) {str = (str + '').toString();return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');}4、获取用户信息$.ajax({url: FX.basePath+"weixin/getUserinfo?code="+code,contentType: "application/json;charset=UTF-8",type: 'GET',dataType:'json',success: function (text) {//获取的用户信息json串console.log(JSON.stringify(text).replace(/[\\]/g,''));},error: function (jqXHR, textStatus, errorThrown) {$("#user_ticket").html(JSON.stringify(jqXHR));}});
配置文件