forked from rongchao/epmet-cloud-rizhao
18 changed files with 343 additions and 3 deletions
@ -0,0 +1,22 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<parent> |
||||
|
<artifactId>epmet-commons</artifactId> |
||||
|
<groupId>com.epmet</groupId> |
||||
|
<version>2.0.0</version> |
||||
|
</parent> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<artifactId>epmet-commons-thirdplat</artifactId> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>com.epmet</groupId> |
||||
|
<artifactId>epmet-commons-tools</artifactId> |
||||
|
<version>2.0.0</version> |
||||
|
</dependency> |
||||
|
</dependencies> |
||||
|
|
||||
|
</project> |
@ -0,0 +1,35 @@ |
|||||
|
package com.epmet.commons.thirdplat; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.epmet.commons.thirdplat.dto.form.jcet.SsoTicketFormDTO; |
||||
|
import com.epmet.commons.thirdplat.encrypt.SignUtils; |
||||
|
|
||||
|
import java.io.UnsupportedEncodingException; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class DemoApp { |
||||
|
|
||||
|
private static String appid = "soXDEoM1"; |
||||
|
private static String appsecret = "V7ea0KnlYt7eSyzc"; |
||||
|
|
||||
|
public static void main(String[] args) throws UnsupportedEncodingException { |
||||
|
|
||||
|
//SsoToken ssoToken = new SsoToken();
|
||||
|
//ssoToken.setSsoToken("wxz");
|
||||
|
|
||||
|
SsoTicketFormDTO ssoTicket = new SsoTicketFormDTO(); |
||||
|
ssoTicket.setSsoTicket("wxz"); |
||||
|
|
||||
|
int bodyLength = JSON.toJSONString(ssoTicket).getBytes("utf-8").length; |
||||
|
|
||||
|
Map<String, Object> headers = new HashMap(); |
||||
|
long timestamp = System.currentTimeMillis(); |
||||
|
headers.put("openTimestamp", String.valueOf(timestamp)); |
||||
|
headers.put("openAppId", appid); |
||||
|
String encryptContent = appid + timestamp + bodyLength; |
||||
|
headers.put("openSign", SignUtils.generate(encryptContent, appsecret)); |
||||
|
System.out.println(headers); |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
package com.epmet.commons.thirdplat.apiservice; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.epmet.commons.thirdplat.dto.result.jcet.JcetResult; |
||||
|
import com.epmet.commons.thirdplat.properties.ThirdplatProps; |
||||
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
import com.epmet.commons.tools.utils.Result; |
||||
|
|
||||
|
public class AbstractApiService { |
||||
|
|
||||
|
protected ThirdplatProps thirdplatProps; |
||||
|
|
||||
|
/** |
||||
|
* @Description 解析请求结果 |
||||
|
* @return |
||||
|
* @author wxz |
||||
|
* @date 2021.01.19 09:53 |
||||
|
*/ |
||||
|
protected <R> R parseResult(Result<String> thResult, Class<R> resultType) { |
||||
|
if (!thResult.success()) { |
||||
|
throw new RenException(EpmetErrorCode.THIRD_PLAT_REQUEST_ERROR.getCode(), |
||||
|
EpmetErrorCode.THIRD_PLAT_REQUEST_ERROR.getMsg().concat(":").concat(thResult.getInternalMsg())); |
||||
|
} |
||||
|
|
||||
|
JcetResult jcetResult = JSON.parseObject(thResult.getData(), JcetResult.class); |
||||
|
if (!jcetResult.isSuccess()) { |
||||
|
throw new RenException(EpmetErrorCode.THIRD_PLAT_REQUEST_ERROR.getCode(), |
||||
|
jcetResult.getMsg().concat(",错误码:") + jcetResult.getCode()); |
||||
|
} |
||||
|
|
||||
|
return JSON.parseObject(jcetResult.getMsg(), resultType); |
||||
|
} |
||||
|
} |
@ -0,0 +1,81 @@ |
|||||
|
package com.epmet.commons.thirdplat.apiservice.jcet; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.epmet.commons.thirdplat.apiservice.AbstractApiService; |
||||
|
import com.epmet.commons.thirdplat.constants.JcetApiUrls; |
||||
|
import com.epmet.commons.thirdplat.dto.form.jcet.SsoTicketFormDTO; |
||||
|
import com.epmet.commons.thirdplat.dto.result.jcet.UserInfoResultDTO; |
||||
|
import com.epmet.commons.thirdplat.encrypt.SignUtils; |
||||
|
import com.epmet.commons.thirdplat.properties.JcetThirdplatProps; |
||||
|
import com.epmet.commons.thirdplat.properties.ThirdplatProps; |
||||
|
import com.epmet.commons.tools.utils.HttpClientManager; |
||||
|
import com.epmet.commons.tools.utils.Result; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.io.UnsupportedEncodingException; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Component |
||||
|
public class JcetApiService extends AbstractApiService { |
||||
|
|
||||
|
private JcetThirdplatProps jcetThirdplatProps; |
||||
|
|
||||
|
public JcetApiService(ThirdplatProps props) { |
||||
|
this.thirdplatProps = props; |
||||
|
jcetThirdplatProps = props.getJcet(); |
||||
|
} |
||||
|
|
||||
|
public UserInfoResultDTO login(String ticket) { |
||||
|
|
||||
|
try { |
||||
|
UserInfoResultDTO userInfoByTicket = getUserInfoByTicket(ticket); |
||||
|
// todo
|
||||
|
} catch (UnsupportedEncodingException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
private UserInfoResultDTO getUserInfoByTicket(String ticket) throws UnsupportedEncodingException { |
||||
|
SsoTicketFormDTO ssoTicket = new SsoTicketFormDTO(); |
||||
|
ssoTicket.setSsoTicket(ticket); |
||||
|
Result<String> result = HttpClientManager.getInstance().sendPostByJSONAndHeader( |
||||
|
jcetThirdplatProps.getDomain().concat(JcetApiUrls.GET_USER_BY_TICKET), |
||||
|
JSON.toJSONString(ssoTicket), getHeaders(ssoTicket)); |
||||
|
|
||||
|
UserInfoResultDTO resultDTO = parseResult(result, UserInfoResultDTO.class); |
||||
|
return resultDTO; |
||||
|
} |
||||
|
|
||||
|
private UserInfoResultDTO getUserInfoByToken(String ticket) throws UnsupportedEncodingException { |
||||
|
SsoTicketFormDTO ssoTicket = new SsoTicketFormDTO(); |
||||
|
ssoTicket.setSsoTicket(ticket); |
||||
|
Result<String> result = HttpClientManager.getInstance().sendPostByJSONAndHeader( |
||||
|
jcetThirdplatProps.getDomain().concat(JcetApiUrls.GET_USER_BY_TICKET), |
||||
|
JSON.toJSONString(ssoTicket), getHeaders(ssoTicket)); |
||||
|
|
||||
|
UserInfoResultDTO resultDTO = parseResult(result, UserInfoResultDTO.class); |
||||
|
return resultDTO; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取请求所需要的头信息 |
||||
|
* @param contentObject |
||||
|
* @return |
||||
|
* @throws UnsupportedEncodingException |
||||
|
*/ |
||||
|
private Map<String, String> getHeaders(Object contentObject) throws UnsupportedEncodingException { |
||||
|
int bodyLength = JSON.toJSONString(contentObject).getBytes("utf-8").length; |
||||
|
|
||||
|
Map<String, String> headers = new HashMap(); |
||||
|
long timestamp = System.currentTimeMillis(); |
||||
|
headers.put("openTimestamp", String.valueOf(timestamp)); |
||||
|
headers.put("openAppId", jcetThirdplatProps.getAppkey()); |
||||
|
String encryptContent = jcetThirdplatProps.getAppkey() + timestamp + bodyLength; |
||||
|
headers.put("openSign", SignUtils.generate(encryptContent, jcetThirdplatProps.getAppsecret())); |
||||
|
return headers; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
package com.epmet.commons.thirdplat.config; |
||||
|
|
||||
|
import com.epmet.commons.thirdplat.properties.ThirdplatProps; |
||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
||||
|
import org.springframework.context.annotation.Configuration; |
||||
|
|
||||
|
/** |
||||
|
* 第三方平台的相关配置 |
||||
|
*/ |
||||
|
@Configuration |
||||
|
@EnableConfigurationProperties(ThirdplatProps.class) |
||||
|
public class ThirdplatConfig { |
||||
|
|
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package com.epmet.commons.thirdplat.constants; |
||||
|
|
||||
|
public interface JcetApiUrls { |
||||
|
|
||||
|
// 根据ticket查询用户
|
||||
|
String GET_USER_BY_TICKET = "/openapi-cgw/openapi-login/sso/getUserInfoByTicket"; |
||||
|
|
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package com.epmet.commons.thirdplat.dto.form.jcet; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class SsoTicketFormDTO { |
||||
|
private String ssoTicket; |
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
package com.epmet.commons.thirdplat.dto.form.jcet; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class SsoTokenFormDTO { |
||||
|
private String ssoToken; |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package com.epmet.commons.thirdplat.dto.result.jcet; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class JcetResult { |
||||
|
|
||||
|
private boolean success; |
||||
|
private String msg; |
||||
|
private Object data; |
||||
|
private int code; |
||||
|
|
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package com.epmet.commons.thirdplat.dto.result.jcet; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class UserInfoResultDTO { |
||||
|
|
||||
|
private String name; |
||||
|
private String mobile; |
||||
|
private String uid; |
||||
|
private String account; |
||||
|
|
||||
|
} |
@ -0,0 +1,51 @@ |
|||||
|
package com.epmet.commons.thirdplat.encrypt; |
||||
|
|
||||
|
|
||||
|
import com.epmet.commons.tools.exception.RenException; |
||||
|
|
||||
|
import java.io.UnsupportedEncodingException; |
||||
|
import java.nio.charset.Charset; |
||||
|
import java.security.MessageDigest; |
||||
|
import java.security.NoSuchAlgorithmException; |
||||
|
|
||||
|
public class EncryptUtils { |
||||
|
private final static char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
||||
|
/** |
||||
|
* 32位 MD5加密 |
||||
|
* |
||||
|
* @param s |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String md5Hex(String s) throws UnsupportedEncodingException { |
||||
|
byte[] result = digest("MD5", s.getBytes(Charset.defaultCharset())); |
||||
|
return hex(result); |
||||
|
} |
||||
|
/** |
||||
|
* 32 位 sha256加密 |
||||
|
* |
||||
|
* @param s |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String sha256(String s) { |
||||
|
byte[] result = digest("SHA-256", s.getBytes(Charset.defaultCharset())); |
||||
|
return hex(result); |
||||
|
} |
||||
|
private static byte[] digest(String algorithm, byte[] data) { |
||||
|
try { |
||||
|
MessageDigest digest = MessageDigest.getInstance(algorithm); |
||||
|
digest.update(data, 0, data.length); |
||||
|
return digest.digest(); |
||||
|
} catch (NoSuchAlgorithmException e) { |
||||
|
throw new RenException(algorithm + " error", e); |
||||
|
} |
||||
|
} |
||||
|
private static String hex(byte[] data) { |
||||
|
char[] result = new char[data.length * 2]; |
||||
|
int c = 0; |
||||
|
for (byte b : data) { |
||||
|
result[c++] = HEX_DIGITS[(b >> 4) & 0xf]; |
||||
|
result[c++] = HEX_DIGITS[b & 0xf]; |
||||
|
} |
||||
|
return new String(result); |
||||
|
} |
||||
|
} |
@ -0,0 +1,15 @@ |
|||||
|
package com.epmet.commons.thirdplat.encrypt; |
||||
|
|
||||
|
import java.io.UnsupportedEncodingException; |
||||
|
|
||||
|
public class SignUtils { |
||||
|
public static String generate(String content, String secret) throws UnsupportedEncodingException { |
||||
|
String sign = EncryptUtils.sha256(content); |
||||
|
sign = EncryptUtils.md5Hex(sign + secret); |
||||
|
return sign; |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
System.out.println(777); |
||||
|
} |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package com.epmet.commons.thirdplat.properties; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
/** |
||||
|
* 酒城e通三方平台配置 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class JcetThirdplatProps { |
||||
|
private String domain; |
||||
|
private String appkey; |
||||
|
private String appsecret; |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package com.epmet.commons.thirdplat.properties; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
|
||||
|
@Data |
||||
|
@ConfigurationProperties(prefix = "thirdplat") |
||||
|
public class ThirdplatProps { |
||||
|
|
||||
|
private JcetThirdplatProps jcet; |
||||
|
|
||||
|
} |
Loading…
Reference in new issue