|
|
@@ -3,15 +3,14 @@ package com.chuanghai.repair.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.auth0.jwt.JWT;
|
|
|
-import com.auth0.jwt.JWTVerifier;
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
-import com.auth0.jwt.interfaces.DecodedJWT;
|
|
|
import com.sun.deploy.net.URLEncoder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
@@ -33,152 +32,162 @@ import java.util.Map;
|
|
|
public class CreateTokenUtil {
|
|
|
|
|
|
//设置过期时间
|
|
|
- private static final long EXPIRE_DATE=30*60*100000;
|
|
|
+ private static final long EXPIRE_DATE = 30 * 60 * 100000;
|
|
|
//token秘钥
|
|
|
private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWE";
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 数据加密生成token
|
|
|
+ *
|
|
|
* @param userId
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getToken (String userId){
|
|
|
+ public static String getToken(String userId) {
|
|
|
String token = "";
|
|
|
try {
|
|
|
//过期时间
|
|
|
- Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
|
|
|
+ Date date = new Date(System.currentTimeMillis() + EXPIRE_DATE);
|
|
|
//秘钥及加密算法
|
|
|
Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
|
|
|
//设置头部信息
|
|
|
- Map<String,Object> header = new HashMap<>();
|
|
|
- header.put("typ","JWT");
|
|
|
- header.put("alg","HS256");
|
|
|
+ Map<String, Object> header = new HashMap<>();
|
|
|
+ header.put("typ", "JWT");
|
|
|
+ header.put("alg", "HS256");
|
|
|
//携带username,password信息,生成签名
|
|
|
token = JWT.create()
|
|
|
.withHeader(header)
|
|
|
- // .withClaim("username",username)
|
|
|
- .withClaim("userId",userId).withExpiresAt(date)
|
|
|
+ // .withClaim("username",username)
|
|
|
+ .withClaim("userId", userId).withExpiresAt(date)
|
|
|
.sign(algorithm);
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
- return null;
|
|
|
+ return null;
|
|
|
}
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 解析token
|
|
|
+ *
|
|
|
* @param token
|
|
|
* @param userId
|
|
|
* @return
|
|
|
*/
|
|
|
|
|
|
- public static String verify(String token,String userId) {
|
|
|
+ public static String verify(String token, String userId) {
|
|
|
return JWT.decode(token).getClaim(userId).asString();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 微校小程序H5页面 通过 wxcode 获取 access_token
|
|
|
+ * 通过 wxcode 获取微信移动端H5 JSAPI中基础支持的ACCESS_TOKEN
|
|
|
* 访问链接:https://open.wecard.qq.com/connect/oauth/authorize?app_key=EE28EE2C93296F4E&response_type=code
|
|
|
- * &scope=snsapi_base&ocode=1015730314&redirect_uri=http://binguo.vaiwan.com/login/getCardNumber&state=STATE
|
|
|
+ * &scope=snsapi_base&ocode=1015730314&redirect_uri=http://binguo.vaiwan.com/login/getCardNumber&state=STATE
|
|
|
+ *
|
|
|
* @param wxcode
|
|
|
- * @param state
|
|
|
+ * @param
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getAccessToken(String wxcode,String state) {
|
|
|
+ public static String getAccessToken(String wxcode) {
|
|
|
String access_token = "";
|
|
|
- System.out.println("before");
|
|
|
+
|
|
|
try {
|
|
|
|
|
|
- wxcode = "wxcode=" + wxcode + "&";
|
|
|
- String app_key = "app_key=EE28EE2C93296F4E&";
|
|
|
- String app_secret = "app_secret=5071958561AA629530AAA31503088330&";
|
|
|
- String grant_type = "grant_type=authorization_code&";
|
|
|
- String redirect_uri = "redirect_uri=http://binguo.vaiwan.com/login/getCardNumber";
|
|
|
- String url = "https://open.wecard.qq.com/connect/oauth2/token?";
|
|
|
- String accessTokenUrl = url + wxcode + app_key + app_secret + grant_type + redirect_uri;
|
|
|
- // -------------------------------> 获取Rest客户端实例
|
|
|
+ String app_key = "EE28EE2C93296F4E";
|
|
|
+ String app_secret = "5071958561AA629530AAA31503088330";
|
|
|
+ String grant_type = "authorization_code";
|
|
|
+ String redirect_uri = "http://binguo.vaiwan.com/login/getAccessToken";
|
|
|
+ String url = "https://open.wecard.qq.com/connect/oauth2/token"; //用于用户 获取基础的access_token
|
|
|
+ MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
|
|
+ param.add("wxcode", wxcode);
|
|
|
+ param.add("app_key", app_key);
|
|
|
+ param.add("app_secret", app_secret);
|
|
|
+ param.add("grant_type", grant_type);
|
|
|
+ param.add("redirect_uri", redirect_uri);
|
|
|
+
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
- // -------------------------------> (选择性设置)请求头信息
|
|
|
- // HttpHeaders实现了MultiValueMap接口
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- // 设置contentType
|
|
|
- // httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
|
|
- httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- // 给请求header中添加一些数据
|
|
|
- // ------------------------------->将请求头、请求体数据,放入HttpEntity中
|
|
|
- // 请求体的类型任选即可;只要保证 请求体 的类型与HttpEntity类的泛型保持一致即可
|
|
|
- Map<String, Object> paramMap = new HashMap<>();
|
|
|
- HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(paramMap, httpHeaders);
|
|
|
- StringBuffer paramsURL = new StringBuffer(accessTokenUrl);
|
|
|
- // 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:flag的参数值就是“&”,不encoding的话,传不过去)
|
|
|
- // paramsURL.append("?flag=" + URLEncoder.encode("&&", "utf-8"));
|
|
|
- URI uri = URI.create(paramsURL.toString());
|
|
|
- // -------------------------------> 执行请求并返回结果
|
|
|
- // 此处的泛型 对应 响应体数据 类型;即:这里指定响应体的数据装配为String
|
|
|
- ResponseEntity<String> response = restTemplate.postForEntity(uri, httpEntity, String.class);
|
|
|
- JSONObject objParam = JSON.parseObject(response.getBody());
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
|
|
|
+ JSONObject objParam = JSON.parseObject(responseEntity.getBody());
|
|
|
for (Map.Entry<String, Object> entry : objParam.entrySet()) {
|
|
|
Object o = entry.getValue();
|
|
|
if (o instanceof String) {
|
|
|
if (entry.getKey().equals("access_token")) {
|
|
|
access_token = (String) entry.getValue();
|
|
|
- System.out.println(access_token+"util ");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-// String app_key = "EE28EE2C93296F4E";
|
|
|
-// String app_secret = "5071958561AA629530AAA31503088330";
|
|
|
-// String grant_type = "authorization_code";
|
|
|
-// String redirect_uri = "http://binguo.vaiwan.com/login/getAccessToken";
|
|
|
-// String url = "https://open.wecard.qq.com/connect/oauth2/token";
|
|
|
-// JSONObject accessTokenUrl = new JSONObject();
|
|
|
-// accessTokenUrl.put("wxcode",wxcode);
|
|
|
-// accessTokenUrl.put("app_key",app_key);
|
|
|
-// accessTokenUrl.put("app_secret",app_secret);
|
|
|
-// accessTokenUrl.put("grant_type",grant_type);
|
|
|
-// accessTokenUrl.put("redirect_uri",redirect_uri);
|
|
|
-// String urlToken = accessTokenUrl.toString();
|
|
|
-// urlToken = urlToken.replace("\"", "%22")
|
|
|
-// .replace("{", "%7b").replace("}", "%7d");
|
|
|
-// StringBuffer paramsURL = new StringBuffer(url);
|
|
|
-// // 字符数据最好encoding一下;这样一来,某些特殊字符才能传过去(如:flag的参数值就是“&”,不encoding的话,传不过去)
|
|
|
-// paramsURL.append("?flag=" + URLEncoder.encode("&&", "utf-8"));
|
|
|
-// URI uri = URI.create(paramsURL.toString());
|
|
|
-// // -------------------------------> 获取Rest客户端实例
|
|
|
-// RestTemplate restTemplate = new RestTemplate();
|
|
|
-// HttpHeaders headers = new HttpHeaders();
|
|
|
-// headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
-// HttpEntity<String> request = new HttpEntity< String>(urlToken, headers);
|
|
|
-// ResponseEntity<String> responseEntity = restTemplate.postForEntity(uri, request, String.class);
|
|
|
-// JSONObject objParam = JSON.parseObject(responseEntity.getBody());
|
|
|
-// for (Map.Entry<String, Object> entry : objParam.entrySet()) {
|
|
|
-// Object o = entry.getValue();
|
|
|
-// if (o instanceof String) {
|
|
|
-// if (entry.getKey().equals("access_token")) {
|
|
|
-// access_token = (String) entry.getValue();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return access_token;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 获取应用凭证
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+// @Scheduled(cron = "0 */1 * * * ?")
|
|
|
+ @Scheduled(cron = "0 59 0/1 * * ?") // 每隔 1hour59minutes 执行一次刷新access_token
|
|
|
+ public static String getAccessTokenGlobal() {
|
|
|
+ String access_token_global = "";
|
|
|
+ try {
|
|
|
+
|
|
|
+ String app_key = "EE28EE2C93296F4E";
|
|
|
+ String app_secret = "5071958561AA629530AAA31503088330";
|
|
|
+ String grant_type = "client_credentials";
|
|
|
+ String scope = "base";
|
|
|
+ String ocode = "1015730314";
|
|
|
+ String url = "https://open.wecard.qq.com/cgi-bin/oauth2/token"; //用于获取应用有效凭证
|
|
|
+ Map<String, String> param = new HashMap<>();
|
|
|
+ param.put("app_key", app_key);
|
|
|
+ param.put("app_secret", app_secret);
|
|
|
+ param.put("grant_type", grant_type);
|
|
|
+ param.put("scope", scope);
|
|
|
+ param.put("ocode", ocode);
|
|
|
+
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<Map<String, String>> request = new HttpEntity<>(param, headers);
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
|
|
|
+ JSONObject objParam = JSON.parseObject(responseEntity.getBody());
|
|
|
+
|
|
|
+ for (Map.Entry<String, Object> entry : objParam.entrySet()) {
|
|
|
+ Object o = entry.getValue();
|
|
|
+ if (o instanceof String) {
|
|
|
+ if (entry.getKey().equals("access_token")) {
|
|
|
+ access_token_global = (String) entry.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return access_token_global;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 微校小程序H5页面 通过 access_token 获取 用户信息
|
|
|
+ *
|
|
|
* @param access_token
|
|
|
* @return
|
|
|
*/
|
|
|
- public static JSONObject getUserInfoJSON(String access_token){
|
|
|
+ public static JSONObject getUserInfoJSON(String access_token) {
|
|
|
JSONObject userInfoJSON = null;
|
|
|
- try{
|
|
|
+ try {
|
|
|
String url = "https://open.wecard.qq.com/connect/oauth/get-user-info";
|
|
|
JSONObject accessTokenUrl = new JSONObject();
|
|
|
- accessTokenUrl.put("access_token",access_token);
|
|
|
+ accessTokenUrl.put("access_token", access_token);
|
|
|
String urlToken = accessTokenUrl.toString();
|
|
|
urlToken = urlToken.replace("\"", "%22")
|
|
|
.replace("{", "%7b").replace("}", "%7d");
|
|
|
@@ -190,13 +199,13 @@ public class CreateTokenUtil {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
- HttpEntity<String> request = new HttpEntity< String>(urlToken, headers);
|
|
|
+ HttpEntity<String> request = new HttpEntity<String>(urlToken, headers);
|
|
|
ResponseEntity<String> responseEntity = restTemplate.postForEntity(uri, request, String.class);
|
|
|
userInfoJSON = JSON.parseObject(responseEntity.getBody());
|
|
|
for (Map.Entry<String, Object> entry : userInfoJSON.entrySet()) {
|
|
|
Object o = entry.getValue();
|
|
|
if (o instanceof String) {
|
|
|
- System.out.println("key:"+entry.getKey()+":"+"value:"+entry.getValue());
|
|
|
+ System.out.println("key:" + entry.getKey() + ":" + "value:" + entry.getValue());
|
|
|
}
|
|
|
}
|
|
|
// RestTemplate restTemplate = new RestTemplate();
|
|
|
@@ -216,7 +225,7 @@ public class CreateTokenUtil {
|
|
|
// System.out.println("key:"+entry.getKey()+":"+"value:"+entry.getValue());
|
|
|
// }
|
|
|
// }
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
@@ -225,15 +234,16 @@ public class CreateTokenUtil {
|
|
|
|
|
|
/**
|
|
|
* 微校小程序H5页面 通过 access_token 获取 用户信息
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @return
|
|
|
*/
|
|
|
- public static JSONObject sendNotice(String url){
|
|
|
+ public static JSONObject sendNotice(String url) {
|
|
|
JSONObject userInfoJSON = null;
|
|
|
- try{
|
|
|
+ try {
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
|
- MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
|
|
|
- map.add("shopid","1");
|
|
|
+ MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
|
|
|
+ map.add("shopid", "1");
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
|
|
|
@@ -247,7 +257,7 @@ public class CreateTokenUtil {
|
|
|
// URI urm = URI.create(unoticeURl.toString());
|
|
|
// ResponseEntity<String> responsem = restTemplate.postForEntity(urm, httpEntity, String.class);
|
|
|
// userInfoJSON = JSON.parseObject(responsem.getBody());
|
|
|
- }catch (Exception e){
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
@@ -256,6 +266,7 @@ public class CreateTokenUtil {
|
|
|
|
|
|
/**
|
|
|
* 获取请求头中的数据
|
|
|
+ *
|
|
|
* @param request
|
|
|
* @param
|
|
|
* @return
|
|
|
@@ -269,8 +280,8 @@ public class CreateTokenUtil {
|
|
|
String value = request.getHeader(key);
|
|
|
map.put(key, value);
|
|
|
}
|
|
|
- for (Map.Entry<String, String> entry:map.entrySet()) {
|
|
|
- if(entry.getKey().equals("access_token")){
|
|
|
+ for (Map.Entry<String, String> entry : map.entrySet()) {
|
|
|
+ if (entry.getKey().equals("access_token")) {
|
|
|
access_token = entry.getValue();
|
|
|
}
|
|
|
}
|
|
|
@@ -278,13 +289,12 @@ public class CreateTokenUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
public static void main(String[] args) {
|
|
|
- String id ="1003";
|
|
|
+ String id = "1003";
|
|
|
String userId = "userId";
|
|
|
String token = getToken(id);
|
|
|
System.out.println(token);
|
|
|
- String s = verify(token,userId);
|
|
|
+ String s = verify(token, userId);
|
|
|
System.out.println(s);
|
|
|
}
|
|
|
}
|