添加安全相关
This commit is contained in:
parent
9514efe9a6
commit
0cbfc146bc
@ -4,21 +4,22 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>axzo-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>common-common</artifactId>
|
||||
<version>1.0.2</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<!-- tool -->
|
||||
<lombok.version>1.18.18</lombok.version>
|
||||
<commons-codec.version>1.8</commons-codec.version>
|
||||
<slf4j-api.version>1.7.5</slf4j-api.version>
|
||||
<lombok.version>1.18.18</lombok.version>
|
||||
<fastjson.version>1.2.47</fastjson.version>
|
||||
<apm-toolkit-trace.version>6.5.0</apm-toolkit-trace.version>
|
||||
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
|
||||
<spring-context.version>4.3.19.RELEASE</spring-context.version>
|
||||
<javax.validation.version>2.0.0.Final</javax.validation.version>
|
||||
<hibernate.validator.version>6.0.16.Final</hibernate.validator.version>
|
||||
@ -28,41 +29,61 @@
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>${fastjson.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate.validator.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring-context.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring-context.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-trace</artifactId>
|
||||
<version>${apm-toolkit-trace.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -4,10 +4,17 @@ package cn.azxo.framework.common.constatns;
|
||||
* 常量类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see CommonConstants
|
||||
* @see Constants
|
||||
* @since 2021-04-26 17:57
|
||||
*/
|
||||
public class CommonConstants {
|
||||
public class Constants {
|
||||
|
||||
/** 签名*/
|
||||
public static final String SIGN = "sign";
|
||||
/** 签名方式*/
|
||||
public static final String SIGN_TYPE = "sign_type";
|
||||
/** 签名版本号*/
|
||||
public static final String SIGN_VERSION = "sign_version";
|
||||
|
||||
/** MDC 日志 KEY */
|
||||
public static final String CTX_LOG_ID_MDC = "ctxLogId";
|
||||
@ -1,6 +1,6 @@
|
||||
package cn.azxo.framework.common.logger;
|
||||
|
||||
import cn.azxo.framework.common.constatns.CommonConstants;
|
||||
import cn.azxo.framework.common.constatns.Constants;
|
||||
import cn.azxo.framework.common.utils.StringUtils;
|
||||
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
|
||||
import org.slf4j.MDC;
|
||||
@ -24,17 +24,17 @@ public class WebLogMdcHandlerInterceptor extends HandlerInterceptorAdapter {
|
||||
// 首先从 skyWalking 获取
|
||||
String apmTraceId = TraceContext.traceId();
|
||||
if (StringUtils.isNotBlank(apmTraceId)) {
|
||||
MDC.put(CommonConstants.CTX_LOG_ID_MDC, apmTraceId);
|
||||
MDC.put(Constants.CTX_LOG_ID_MDC, apmTraceId);
|
||||
return true;
|
||||
}
|
||||
// 从 Header 中获取
|
||||
String headerTraceId = request.getHeader(CommonConstants.CTX_LOG_ID_MDC);
|
||||
String headerTraceId = request.getHeader(Constants.CTX_LOG_ID_MDC);
|
||||
if (StringUtils.isNotBlank(headerTraceId)) {
|
||||
MDC.put(CommonConstants.CTX_LOG_ID_MDC, headerTraceId);
|
||||
MDC.put(Constants.CTX_LOG_ID_MDC, headerTraceId);
|
||||
return true;
|
||||
}
|
||||
// 自己生成 traceId
|
||||
MDC.put(CommonConstants.CTX_LOG_ID_MDC, UUID.randomUUID().toString());
|
||||
MDC.put(Constants.CTX_LOG_ID_MDC, UUID.randomUUID().toString());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
package cn.azxo.framework.common.security;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
/**
|
||||
* MD 加/验签
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see MD5
|
||||
* @since 2021-04-29 18:01
|
||||
*/
|
||||
public abstract class MD5 {
|
||||
|
||||
public static String sign(String text, String key, String charset) throws Exception {
|
||||
text = text + key;
|
||||
return DigestUtils.md5Hex(getContentBytes(text, charset));
|
||||
}
|
||||
|
||||
public static boolean verify(String text, String sign, String key, String charset) throws Exception {
|
||||
text = text + key;
|
||||
String mysign = DigestUtils.md5Hex(getContentBytes(text, charset));
|
||||
return mysign.equals(sign);
|
||||
}
|
||||
|
||||
private static byte[] getContentBytes(String content, String charset) {
|
||||
if (charset != null && !"".equals(charset)) {
|
||||
try {
|
||||
return content.getBytes(charset);
|
||||
} catch (UnsupportedEncodingException var3) {
|
||||
throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
|
||||
}
|
||||
} else {
|
||||
return content.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,230 @@
|
||||
package cn.azxo.framework.common.security;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.Key;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.Signature;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* RSA 加/验签
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see RSA
|
||||
* @since 2021-04-29 18:03
|
||||
*/
|
||||
public abstract class RSA {
|
||||
|
||||
private static final Integer MAX_ENCRYPT_BLOCK = 245;
|
||||
private static final Integer MAX_DECRYPT_BLOCK = 256;
|
||||
private static final String KEY_ALGORITHM = "RSA";
|
||||
|
||||
public static Map<String, Object> genKeyPair() throws Exception {
|
||||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGen.initialize(1024);
|
||||
KeyPair keyPair = keyPairGen.generateKeyPair();
|
||||
RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
|
||||
RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
|
||||
Map<String, Object> keyMap = new HashMap(2);
|
||||
keyMap.put("RSAPublicKey", publicKey);
|
||||
keyMap.put("RSAPrivateKey", privateKey);
|
||||
return keyMap;
|
||||
}
|
||||
|
||||
public static String sign(String text, String privateKey, String charset) throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(privateKey);
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
||||
Signature signature = Signature.getInstance("SHA1withRSA");
|
||||
signature.initSign(privateK);
|
||||
signature.update(getContentBytes(text, charset));
|
||||
byte[] result = signature.sign();
|
||||
return Base64.encodeBase64String(result);
|
||||
}
|
||||
|
||||
public static boolean verify(String text, String sign, String publicKey, String charset) throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(publicKey);
|
||||
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
PublicKey publicK = keyFactory.generatePublic(keySpec);
|
||||
Signature signature = Signature.getInstance("SHA1withRSA");
|
||||
signature.initVerify(publicK);
|
||||
signature.update(getContentBytes(text, charset));
|
||||
return signature.verify(Base64.decodeBase64(sign));
|
||||
}
|
||||
|
||||
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(publicKey);
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(2, publicK);
|
||||
int inputLen = encryptedData.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
|
||||
for(int i = 0; inputLen - offSet > 0; offSet = i * 128) {
|
||||
byte[] cache;
|
||||
if (inputLen - offSet > 128) {
|
||||
cache = cipher.doFinal(encryptedData, offSet, 128);
|
||||
} else {
|
||||
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
||||
}
|
||||
|
||||
out.write(cache, 0, cache.length);
|
||||
++i;
|
||||
}
|
||||
|
||||
byte[] decryptedData = out.toByteArray();
|
||||
out.close();
|
||||
return decryptedData;
|
||||
}
|
||||
|
||||
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(privateKey);
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(1, privateK);
|
||||
int inputLen = data.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
|
||||
for(int i = 0; inputLen - offSet > 0; offSet = i * 117) {
|
||||
byte[] cache;
|
||||
if (inputLen - offSet > 117) {
|
||||
cache = cipher.doFinal(data, offSet, 117);
|
||||
} else {
|
||||
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
||||
}
|
||||
|
||||
out.write(cache, 0, cache.length);
|
||||
++i;
|
||||
}
|
||||
|
||||
byte[] encryptedData = out.toByteArray();
|
||||
out.close();
|
||||
return encryptedData;
|
||||
}
|
||||
|
||||
private static byte[] getContentBytes(String content, String charset) {
|
||||
if (charset != null && !"".equals(charset)) {
|
||||
try {
|
||||
return content.getBytes(charset);
|
||||
} catch (UnsupportedEncodingException var3) {
|
||||
throw new RuntimeException("签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
|
||||
}
|
||||
} else {
|
||||
return content.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
|
||||
Key key = (Key)keyMap.get("RSAPrivateKey");
|
||||
return Base64.encodeBase64String(key.getEncoded());
|
||||
}
|
||||
|
||||
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
|
||||
Key key = (Key)keyMap.get("RSAPublicKey");
|
||||
return Base64.encodeBase64String(key.getEncoded());
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥加密
|
||||
* @param data
|
||||
* @param publicKey
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(publicKey);
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||
Key publicK = keyFactory.generatePublic(x509KeySpec);
|
||||
// 对数据加密
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicK);
|
||||
int inputLen = data.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
byte[] cache;
|
||||
int i = 0;
|
||||
// 对数据分段加密
|
||||
while (inputLen - offSet > 0) {
|
||||
if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
|
||||
cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
|
||||
} else {
|
||||
cache = cipher.doFinal(data, offSet, inputLen - offSet);
|
||||
}
|
||||
out.write(cache, 0, cache.length);
|
||||
i++;
|
||||
offSet = i * MAX_ENCRYPT_BLOCK;
|
||||
}
|
||||
byte[] encryptedData = out.toByteArray();
|
||||
out.close();
|
||||
return encryptedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
* @param encryptedData
|
||||
* @param privateKey
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)
|
||||
throws Exception {
|
||||
byte[] keyBytes = Base64.decodeBase64(privateKey);
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
|
||||
Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
|
||||
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateK);
|
||||
int inputLen = encryptedData.length;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
byte[] cache;
|
||||
int i = 0;
|
||||
// 对数据分段解密
|
||||
while (inputLen - offSet > 0) {
|
||||
if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
|
||||
cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
|
||||
} else {
|
||||
cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
|
||||
}
|
||||
out.write(cache, 0, cache.length);
|
||||
i++;
|
||||
offSet = i * MAX_DECRYPT_BLOCK;
|
||||
}
|
||||
byte[] decryptedData = out.toByteArray();
|
||||
out.close();
|
||||
return decryptedData;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String pikStr = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2HJ/WyTWxOk8c6CQuS3qp2EjNn/Bo5WxW1/Oa75QT2GSHykcjo9/UxfIwA7FTex8BSZjzkdzWDr6xmkUZ5js0ZEVc5l5aLPlDN88RJrR8+mHZ3tnJO49qgJtmleKmTVXQrrXICsJDBIs8h4n6wUlGyivpjb4n+RWvI8jvN6xrVPPFOr8rS5cUYipZD/rZAiHdBL1ZWiMZwfCOedQd0H67fhYkcBp2+KNaFixyQ6cIC6tuywV//vhvBh1Fzsf+QUd+9Cq689kPfUnmewaNt1Pdn/P+dde8wt8zF8j5SCztN/qPtWZ2PrHZpi529n5Sh4HDfvUFbuXCGjaqkzouMKqXAgMBAAECggEBAI6q0hWkP+n98iiFPIMAIrKYfECKwf6LbVqDh5ajhfAeIHO45g5nvVylTlyLKApsdBfAHpECfm070+yzHNtzYIJk42oqF1Sx0vzz5cZb+h3G0Y2rZhaeEHaxFg1B8vOxtj7sAAEdXJ7AGoxq6o+FiPBqRgiMQKJ0Iqa4dBvkE7Fo4YZcWR2yCq1ObfRb+QSE0U5L1krlbsozPH8Uj/J40jO9YNh1cTz1CN6ksZGiwfpVWjDgNxY8/v20sUkg8/WdvywycInHY9hk5a5wj1vNfm2CvJbKj/AT3lVCs1EBWvyDw5i3dpbK7HnJTNSLnRnxiSlGZSroBkzLVys3o6yqgYECgYEA8sLutay7dm9ce9YsbcDIcAe4jLA+AxgD7sYTZ5uQ6bG34eJKet3Ot4Snwgh0kb6MSXCwG6IunSiZcWgtVSU6tT/p9cyy76c0rripZtXrmghHJCMXfItParKxQjwpDzqUDCgiwCHU3iuJNsDTVn0NyHutxiJZ7ocOQQa0dVmNBXcCgYEAwAr+WF2MfFqA2pCuVPU2mqbj/wEv3KKviykET5Loh74ENZDJlIl3Rw1l9UQ90WZhfjkhIGIL9w89N87657NpVVr7+4deiA71KEBEN5MxbDaw7VPZQv9ATg10QV9jXePzKTWGvwerLeV4groIx8/6QHObnpc57+q2GQyTZLnDS+ECgYAJCrurZqCg26r9Lc//0oNksNTt4I6evOFAnn4QiNnP6n+n2S7e/q35ZeZ9sqTs3MUK4zib/uFvlxiIAdJWbomOQLAtKI+tn3O14kjk/KjG1vIXZXs1d1bg6YDyr5zz5UGoZkJNF7V/BO4abPoyKXw+8GRbkeH5oWwlfeNwz4ISJwKBgEka5j/VPMmJ2ktZkEOfJS4yIdDNLt8avyCGeLGiBwrFduy9NT9qJRHTM+DuHWo9Qxe9v7KIIG3oSzGXhUP2tD5d6FaT7ZR1vTWZYV6WldXP7AIsTSSH7n7H+F16ZmUzhTlGuwi1bqCGHh4Fm5oUvfsTB0aT4uZFvxRCCUnqvUGBAoGAE81dJQvRDmRxdeX96dt0kWz7MBk1g19e3QI0ilRtjKgrSCguM2vNKBsRNdYCmMDJA4FGWzi7GRNc8uda1VyeaZSuWiO+L/GxzCPFvyqYtlMlrzGTREU3z4V+5rxd+G7cHw3PokW1xS5o/8kTLze2Zjxxj6HkJpHo3IbzLUCsPI8=";
|
||||
String pukStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAthyf1sk1sTpPHOgkLkt6qdhIzZ/waOVsVtfzmu+UE9hkh8pHI6Pf1MXyMAOxU3sfAUmY85Hc1g6+sZpFGeY7NGRFXOZeWiz5QzfPESa0fPph2d7ZyTuPaoCbZpXipk1V0K61yArCQwSLPIeJ+sFJRsor6Y2+J/kVryPI7zesa1TzxTq/K0uXFGIqWQ/62QIh3QS9WVojGcHwjnnUHdB+u34WJHAadvijWhYsckOnCAurbssFf/74bwYdRc7H/kFHfvQquvPZD31J5nsGjbdT3Z/z/nXXvMLfMxfI+Ugs7Tf6j7Vmdj6x2aYudvZ+UoeBw371BW7lwho2qpM6LjCqlwIDAQAB";
|
||||
|
||||
String enc = Base64.encodeBase64String(encryptByPublicKey("12345中文啊".getBytes("UTF-8"), pukStr));
|
||||
String plain = new String(decryptByPrivateKey(Base64.decodeBase64(enc.getBytes("UTF-8")), pikStr),"utf-8");
|
||||
System.out.println(enc);
|
||||
System.out.println(plain);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,53 @@
|
||||
package cn.azxo.framework.common.utils;
|
||||
|
||||
import cn.azxo.framework.common.constatns.Constants;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* 签名工具类
|
||||
*
|
||||
* @author zhaoyong_sh
|
||||
* @see SignUtils
|
||||
* @since 2021-04-29 18:15
|
||||
*/
|
||||
public abstract class SignUtils {
|
||||
|
||||
/**
|
||||
* 计算签名字符串
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public static String calculateSignPlain(Map<String, Object> params) {
|
||||
// 字符串进行排序
|
||||
Map<String, Object> sortedParams = new TreeMap<>(params);
|
||||
|
||||
// 删除默认字段
|
||||
defaultRemoveParams(sortedParams);
|
||||
|
||||
// 获取签名 Plain
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Map.Entry<String, Object> e : sortedParams.entrySet()) {
|
||||
String key = e.getKey();
|
||||
Object val = e.getValue();
|
||||
if (val instanceof JSONObject || val instanceof HashMap) {
|
||||
sb.append(key).append("=").append(calculateSignPlain(((Map) val)));
|
||||
} else {
|
||||
sb.append(key).append("=").append(val);
|
||||
}
|
||||
sb.append("&");
|
||||
}
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static void defaultRemoveParams(Map<String, Object> sortedParams) {
|
||||
sortedParams.remove(Constants.SIGN);
|
||||
sortedParams.remove(Constants.SIGN_TYPE);
|
||||
sortedParams.remove(Constants.SIGN_VERSION);
|
||||
}
|
||||
|
||||
}
|
||||
@ -59,4 +59,8 @@ public abstract class StringUtils {
|
||||
return !isBlank(cs);
|
||||
}
|
||||
|
||||
public static int length(String str) {
|
||||
return str == null ? 0 : str.length();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
125
pom.xml
125
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>axzo-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
<version>1.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
@ -17,132 +17,15 @@
|
||||
<module>asyncTool</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<revision>1.0.1</revision>
|
||||
<!-- tool -->
|
||||
<aspectj.version>1.7.3</aspectj.version>
|
||||
<aopalliance.version>1.0</aopalliance.version>
|
||||
<slf4j-api.version>1.7.5</slf4j-api.version>
|
||||
<lombok.version>1.18.18</lombok.version>
|
||||
<apm-toolkit-trace.version>6.5.0</apm-toolkit-trace.version>
|
||||
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
|
||||
<spring.framework.version>4.3.19.RELEASE</spring.framework.version>
|
||||
<javax.validation.version>2.0.0.Final</javax.validation.version>
|
||||
<hibernate.validator.version>6.0.16.Final</hibernate.validator.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- AspectJ -->
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>aopalliance</groupId>
|
||||
<artifactId>aopalliance</artifactId>
|
||||
<version>${aopalliance.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate.validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>apm-toolkit-trace</artifactId>
|
||||
<version>${apm-toolkit-trace.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<distributionManagement>
|
||||
<!-- 两个ID必须与 setting.xml中的<server><id>Releases</id></server>保持一致 -->
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>Nexus Release Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-release-XI7cl5/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>Nexus Snapshot Repository</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2005773-snapshot-V5Gjdf/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>flatten-maven-plugin</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>2.8.2</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>flatten</id>
|
||||
<phase>process-resources</phase>
|
||||
<goals>
|
||||
<goal>flatten</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@ -4,38 +4,48 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>axzo-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<groupId>cn.axzo.framework</groupId>
|
||||
<artifactId>smart-datasource</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<aspectj.version>1.7.3</aspectj.version>
|
||||
<aopalliance.version>1.0</aopalliance.version>
|
||||
<spring.framework.version>4.3.19.RELEASE</spring.framework.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>${aspectj.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>aopalliance</groupId>
|
||||
<artifactId>aopalliance</artifactId>
|
||||
<version>${aopalliance.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>${spring.framework.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user