11 changed files with 324 additions and 17 deletions
@ -0,0 +1,46 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* |
|||
* https://www.renren.io
|
|||
* |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.elink.esua.epdc.jwt; |
|||
|
|||
import com.elink.esua.epdc.properties.EpmetProperties; |
|||
import io.jsonwebtoken.Jwts; |
|||
import io.jsonwebtoken.SignatureAlgorithm; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* Jwt工具类 |
|||
* |
|||
* @author Mark sunlightcs@gmail.com |
|||
* @since 1.0.0 |
|||
*/ |
|||
@Component |
|||
public class JwtUtils { |
|||
|
|||
@Autowired |
|||
private EpmetProperties epmetProperties; |
|||
|
|||
/** |
|||
* 生成上报接口accessToken |
|||
* |
|||
* @param claims |
|||
* @return java.lang.String |
|||
* @author Liuchuang |
|||
* @since 2020/9/7 14:11 |
|||
*/ |
|||
public String getEpmetAccessToken(Map<String,Object> claims){ |
|||
return Jwts.builder() |
|||
.setHeaderParam("typ", "JWT") |
|||
.setClaims(claims) |
|||
.signWith(SignatureAlgorithm.HS512, epmetProperties.getSecret()) |
|||
.compact(); |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
package com.elink.esua.epdc.properties; |
|||
|
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
/** |
|||
* 党群e事通接口配置 |
|||
* |
|||
* @Author:liuchuang |
|||
* @Date:2020/8/20 13:22 |
|||
*/ |
|||
@Configuration |
|||
@ConfigurationProperties(prefix = "epmet.config") |
|||
public class EpmetProperties { |
|||
|
|||
/** |
|||
* 应用ID |
|||
*/ |
|||
private String appId; |
|||
|
|||
/** |
|||
* 接口前缀 |
|||
*/ |
|||
private String url; |
|||
|
|||
/** |
|||
* 生成AccessToken密钥 |
|||
*/ |
|||
private String secret; |
|||
|
|||
/** |
|||
* 客户ID |
|||
*/ |
|||
private String customerId; |
|||
|
|||
public String getAppId() { |
|||
return appId; |
|||
} |
|||
|
|||
public void setAppId(String appId) { |
|||
this.appId = appId; |
|||
} |
|||
|
|||
public String getUrl() { |
|||
return url; |
|||
} |
|||
|
|||
public void setUrl(String url) { |
|||
this.url = url; |
|||
} |
|||
|
|||
public String getSecret() { |
|||
return secret; |
|||
} |
|||
|
|||
public void setSecret(String secret) { |
|||
this.secret = secret; |
|||
} |
|||
|
|||
public String getCustomerId() { |
|||
return customerId; |
|||
} |
|||
|
|||
public void setCustomerId(String customerId) { |
|||
this.customerId = customerId; |
|||
} |
|||
} |
|||
@ -0,0 +1,103 @@ |
|||
package com.elink.esua.epdc.utils; |
|||
|
|||
import cn.hutool.http.HttpRequest; |
|||
import cn.hutool.http.HttpResponse; |
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.elink.esua.epdc.commons.tools.constant.EpmetConstant; |
|||
import com.elink.esua.epdc.commons.tools.constant.NumConstant; |
|||
import com.elink.esua.epdc.commons.tools.exception.RenException; |
|||
import com.elink.esua.epdc.commons.tools.utils.Result; |
|||
import com.elink.esua.epdc.dto.EpmetResultDTO; |
|||
import com.elink.esua.epdc.jwt.JwtUtils; |
|||
import com.elink.esua.epdc.properties.EpmetProperties; |
|||
import org.apache.logging.log4j.LogManager; |
|||
import org.apache.logging.log4j.Logger; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Component |
|||
public class EpmetUtils { |
|||
|
|||
private final Logger logger = LogManager.getLogger(getClass()); |
|||
|
|||
@Autowired |
|||
private EpmetProperties epmetProperties; |
|||
|
|||
@Autowired |
|||
private JwtUtils jwtUtils; |
|||
|
|||
/** |
|||
* 获取产品端数据 |
|||
* |
|||
* @param urlSuffix 接口地址后缀 |
|||
* @param param 入参 |
|||
* @param returnClass 返回类型 |
|||
* @return T |
|||
* @author zhy |
|||
* @date 2021/1/11 11:10 |
|||
*/ |
|||
public <T> T httpPost(String urlSuffix, String param, Class<T> returnClass) { |
|||
// 应用ID
|
|||
String appId = epmetProperties.getAppId(); |
|||
// 生成AccessToken
|
|||
String accessToken = getEpmetAccessToken(); |
|||
// 接口地址
|
|||
String url = epmetProperties.getUrl().concat(urlSuffix); |
|||
HttpResponse response; |
|||
response = HttpRequest.post(url).body(param) |
|||
.header(EpmetConstant.HEARD_KEY_ACCESS_TOKEN, accessToken) |
|||
.header(EpmetConstant.HEARD_KEY_APP_ID, appId) |
|||
.header(EpmetConstant.SCREEN_CONTENT_TYPE, "application/json") |
|||
.header(EpmetConstant.SCREEN_DATA_TYPE, EpmetConstant.SCREEN_DATA_TYPE_REAL) |
|||
.execute(); |
|||
if (NumConstant.TWO_HUNDRED != response.getStatus()) { |
|||
logger.error("数据上报失败, 返回状态:{}, 请求参数:{appId:{}, url:{}, accessToken:{}, param:{}}", |
|||
response.getStatus(), appId, url, accessToken, param); |
|||
throw new RenException("网络开小差了,请稍后再试"); |
|||
} |
|||
JSONObject jsonObject = JSONObject.parseObject(response.body()); |
|||
if (!NumConstant.ZERO_STR.equals(jsonObject.get(EpmetConstant.RESULT_KEY_CODE).toString())) { |
|||
logger.info("数据上报失败:{},请求地址:{},请求参数:{}", JSONObject.toJSONString(response.body()), url, param); |
|||
throw new RenException("网络开小差了,请稍后再试"); |
|||
} |
|||
|
|||
logger.info("请求产品侧接口成功:::请求地址=={}:::接口返回=={}", url, JSONObject.toJSONString(response.body())); |
|||
T result = JSON.parseObject(response.body(), returnClass); |
|||
// 业务处理异常
|
|||
if (result instanceof EpmetResultDTO) { |
|||
EpmetResultDTO epmetResultDto = (EpmetResultDTO) result; |
|||
if (!epmetResultDto.success()) { |
|||
logger.error("产品侧接口返回数据异常:::请求地址=={}:::请求参数=={}:::返回数据=={}", url, param, response.body()); |
|||
throw new RenException(epmetResultDto.getInternalMsg()); |
|||
} |
|||
} else if (result instanceof Result) { |
|||
Result r = (Result) result; |
|||
if (!r.success()) { |
|||
logger.error("产品侧接口返回数据异常:::请求地址=={}:::请求参数=={}:::返回数据=={}", url, param, response.body()); |
|||
throw new RenException(r.getMsg()); |
|||
} |
|||
} |
|||
// json格式化接口返回数据
|
|||
return result; |
|||
} |
|||
|
|||
/** |
|||
* 生成上报接口accessToken |
|||
* |
|||
* @return java.lang.String |
|||
* @author Liuchuang |
|||
* @since 2020/9/7 14:15 |
|||
*/ |
|||
private String getEpmetAccessToken() { |
|||
Map<String, Object> accessTokenMap = new HashMap<>(3); |
|||
accessTokenMap.put(EpmetConstant.ACCESS_TOKEN_KEY_CUSTOMER_ID, epmetProperties.getCustomerId()); |
|||
accessTokenMap.put(EpmetConstant.ACCESS_TOKEN_KEY_APP_ID, epmetProperties.getAppId()); |
|||
accessTokenMap.put(EpmetConstant.ACCESS_TOKEN_KEY_TS, System.currentTimeMillis()); |
|||
|
|||
return jwtUtils.getEpmetAccessToken(accessTokenMap); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue