mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-11-18 23:47:44 +08:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ccf87e5a57 | |||
| 32675d5d29 | |||
| c7e91286dc | |||
| 1beed4e029 | |||
| 0417621cfe | |||
| 63e3b39c62 | |||
| 43e6ea9045 | |||
| 4b7e86de85 | |||
| 051ddc1eb4 |
13
.idea/libraries/Maven__javax_validation_validation_api_1_1_0_Final.xml
generated
Normal file
13
.idea/libraries/Maven__javax_validation_validation_api_1_1_0_Final.xml
generated
Normal file
@@ -0,0 +1,13 @@
|
||||
<component name="libraryTable">
|
||||
<library name="Maven: javax.validation:validation-api:1.1.0.Final">
|
||||
<CLASSES>
|
||||
<root url="jar://J:/localRepo/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC>
|
||||
<root url="jar://J:/localRepo/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final-javadoc.jar!/" />
|
||||
</JAVADOC>
|
||||
<SOURCES>
|
||||
<root url="jar://J:/localRepo/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final-sources.jar!/" />
|
||||
</SOURCES>
|
||||
</library>
|
||||
</component>
|
||||
33
README.md
Normal file
33
README.md
Normal file
@@ -0,0 +1,33 @@
|
||||
通用工具包
|
||||
|
||||
### 说明
|
||||
>1. 支持环境:java8
|
||||
>2. 该工具包基于springboot提取,按理说适用于所有java工程
|
||||
>3. 其中依赖jjwt、validation-api,但不传递依赖
|
||||
|
||||
|
||||
### 使用
|
||||
>yexuejc.base.version=1.1.2
|
||||
|
||||
pom.xml
|
||||
```
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.yexuejc</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>${yexuejc.base.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
```
|
||||
|
||||
### 工具文档
|
||||
[Wiki](WIKI.md)
|
||||
|
||||
### 更新日志
|
||||
[更新记录](UPDATE.md)
|
||||
30
UPDATE.md
Normal file
30
UPDATE.md
Normal file
@@ -0,0 +1,30 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.1.2
|
||||
**time:** 2018-5-16 15:03:28<br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1.修改依赖
|
||||
|
||||
#
|
||||
#### version :1.1.1
|
||||
**time:** 2018-5-12 22:25:05<br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1.添加RSA
|
||||
#
|
||||
|
||||
##### version :1.1.0
|
||||
**time:** 2018-5-12 22:25:05<br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1.添加支持加密功能
|
||||
#
|
||||
|
||||
#### version :1.0.0
|
||||
**time:** 2018-1-31 12:16:10<br/>
|
||||
**branch:** master <br/>
|
||||
**update:** <br/>
|
||||
>1.基于java8开发的web应用工具包
|
||||
#
|
||||
9
WIKI.md
Normal file
9
WIKI.md
Normal file
@@ -0,0 +1,9 @@
|
||||
yexuejc-base 文档
|
||||
------------------
|
||||
|
||||
##### 1. RespsConsts 网络请求统一返回 常量
|
||||
##### 2. Resps 网络请求统一返回类
|
||||
##### 3. ApiVO 接口交互API
|
||||
##### 4. BaseVO 基类VO
|
||||
|
||||
待完善......
|
||||
25
pom.xml
25
pom.xml
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.yexuejc.base</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.1.2</version>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
<jjwt.version>0.7.0</jjwt.version>
|
||||
<maven.compiler.verbose>true</maven.compiler.verbose>
|
||||
<java.version>1.8</java.version>
|
||||
<validation-api.version>1.1.0.Final</validation-api.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<commons-io.version>2.6</commons-io.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -28,7 +31,25 @@
|
||||
<groupId>io.jsonwebtoken</groupId>
|
||||
<artifactId>jjwt</artifactId>
|
||||
<version>${jjwt.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<!-- 数据校验框架 -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${validation-api.version}</version>
|
||||
</dependency>
|
||||
<!--base64使用到的依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<!--IOUtils使用到的依赖-->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
187
src/main/java/com/yexuejc/base/encrypt/RSA.java
Normal file
187
src/main/java/com/yexuejc/base/encrypt/RSA.java
Normal file
@@ -0,0 +1,187 @@
|
||||
package com.yexuejc.base.encrypt;
|
||||
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.security.*;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.security.spec.InvalidKeySpecException;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* RSA加解密 配置模式
|
||||
*
|
||||
* @ClassName: RSA
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2018/5/15 14:39
|
||||
*/
|
||||
public class RSA {
|
||||
|
||||
public static final String CHARSET = "UTF-8";
|
||||
public static final String RSA_ALGORITHM = "RSA";
|
||||
|
||||
public static Map<String, String> initKeys(int keySize) {
|
||||
//为RSA算法创建一个KeyPairGenerator对象
|
||||
KeyPairGenerator kpg;
|
||||
try {
|
||||
kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
|
||||
}
|
||||
|
||||
//初始化KeyPairGenerator对象,密钥长度
|
||||
kpg.initialize(keySize);
|
||||
//生成密匙对
|
||||
KeyPair keyPair = kpg.generateKeyPair();
|
||||
//得到公钥
|
||||
Key publicKey = keyPair.getPublic();
|
||||
String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
|
||||
//得到私钥
|
||||
Key privateKey = keyPair.getPrivate();
|
||||
String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
|
||||
Map<String, String> keyPairMap = new HashMap<String, String>();
|
||||
keyPairMap.put("publicKey", publicKeyStr);
|
||||
keyPairMap.put("privateKey", privateKeyStr);
|
||||
|
||||
return keyPairMap;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到公钥
|
||||
*
|
||||
* @param publicKey 密钥字符串(经过base64编码)
|
||||
* @throws Exception
|
||||
*/
|
||||
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||
//通过X509编码的Key指令获得公钥对象
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
||||
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
|
||||
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到私钥
|
||||
*
|
||||
* @param privateKey 密钥字符串(经过base64编码)
|
||||
* @throws Exception
|
||||
*/
|
||||
public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
|
||||
//通过PKCS#8编码的Key指令获得私钥对象
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
|
||||
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
|
||||
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param publicKey
|
||||
* @return
|
||||
*/
|
||||
public static String publicEncrypt(String data, RSAPublicKey publicKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
||||
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param privateKey
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String privateDecrypt(String data, RSAPrivateKey privateKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, privateKey);
|
||||
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), privateKey.getModulus().bitLength()), CHARSET);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 私钥加密
|
||||
*
|
||||
* @param data
|
||||
* @param privateKey
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String privateEncrypt(String data, RSAPrivateKey privateKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
|
||||
return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength()));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 公钥解密
|
||||
*
|
||||
* @param data
|
||||
* @param publicKey
|
||||
* @return
|
||||
*/
|
||||
|
||||
public static String publicDecrypt(String data, RSAPublicKey publicKey) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, publicKey);
|
||||
return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), publicKey.getModulus().bitLength()), CHARSET);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize) {
|
||||
int maxBlock = 0;
|
||||
if (opmode == Cipher.DECRYPT_MODE) {
|
||||
maxBlock = keySize / 8;
|
||||
} else {
|
||||
maxBlock = keySize / 8 - 11;
|
||||
}
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
int offSet = 0;
|
||||
byte[] buff;
|
||||
int i = 0;
|
||||
try {
|
||||
while (datas.length > offSet) {
|
||||
if (datas.length - offSet > maxBlock) {
|
||||
buff = cipher.doFinal(datas, offSet, maxBlock);
|
||||
} else {
|
||||
buff = cipher.doFinal(datas, offSet, datas.length - offSet);
|
||||
}
|
||||
out.write(buff, 0, buff.length);
|
||||
i++;
|
||||
offSet = i * maxBlock;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("加解密阀值为[" + maxBlock + "]的数据时发生异常", e);
|
||||
}
|
||||
byte[] resultDatas = out.toByteArray();
|
||||
IOUtils.closeQuietly(out);
|
||||
return resultDatas;
|
||||
}
|
||||
|
||||
}
|
||||
62
src/main/java/com/yexuejc/base/encrypt/RSA2.java
Normal file
62
src/main/java/com/yexuejc/base/encrypt/RSA2.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package com.yexuejc.base.encrypt;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyStore;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.UnrecoverableKeyException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.security.interfaces.RSAPrivateKey;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
|
||||
/**
|
||||
* RSA加解密 证书模式
|
||||
* 依赖 {@link RSA}
|
||||
* @ClassName: RSA2
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2018/5/15 14:37
|
||||
*/
|
||||
public class RSA2 {
|
||||
|
||||
public static final String CHARSET = "UTF-8";
|
||||
public static final String RSA_ALGORITHM = "RSA";
|
||||
|
||||
/**
|
||||
* 得到公钥
|
||||
*
|
||||
* @param filepath 密钥文件路径
|
||||
* @throws Exception
|
||||
*/
|
||||
public static RSAPublicKey getPublicKey(String filepath) throws CertificateException, FileNotFoundException {
|
||||
//通过证书,获取公钥
|
||||
CertificateFactory cf = null;
|
||||
cf = CertificateFactory.getInstance("X.509");
|
||||
Certificate c = cf.generateCertificate(new FileInputStream(filepath));
|
||||
return (RSAPublicKey) c.getPublicKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到私钥
|
||||
*
|
||||
* @param filepath 私钥路径
|
||||
* @param alias 证书别名
|
||||
* @param password 证书密码
|
||||
* @return
|
||||
* @throws NoSuchAlgorithmException
|
||||
* @throws KeyStoreException
|
||||
* @throws IOException
|
||||
* @throws CertificateException
|
||||
* @throws UnrecoverableKeyException
|
||||
*/
|
||||
public static RSAPrivateKey getPrivateKey(String filepath, String alias, String password) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
|
||||
KeyStore ks = KeyStore.getInstance("JKS");
|
||||
ks.load(new FileInputStream(filepath), password.toCharArray());
|
||||
return (RSAPrivateKey) ks.getKey(alias, password.toCharArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,10 @@ public class Resps<T> implements Serializable {
|
||||
* 状态
|
||||
*/
|
||||
private String code;
|
||||
/**
|
||||
* md5码
|
||||
*/
|
||||
private String sign;
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
@@ -133,6 +137,14 @@ public class Resps<T> implements Serializable {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getSign() {
|
||||
return sign;
|
||||
}
|
||||
|
||||
public void setSign(String sign) {
|
||||
this.sign = sign;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.yexuejc.base.util.JsonUtil;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 基类VO
|
||||
* @PackageName: com.yexuejc.util.base.pojo
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
|
||||
49
src/main/java/com/yexuejc/base/pojo/PagerVO.java
Normal file
49
src/main/java/com/yexuejc/base/pojo/PagerVO.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/**
|
||||
* 分页 VO
|
||||
*
|
||||
* @author: maxf
|
||||
* @date: 2018/3/28 14:23
|
||||
*/
|
||||
public class PagerVO extends BaseVO {
|
||||
|
||||
private static final long serialVersionUID = 3490440129554644587L;
|
||||
|
||||
@NotNull
|
||||
@Min(1L)
|
||||
private Integer page = 1;
|
||||
@NotNull
|
||||
@Min(1L)
|
||||
private Integer size = 20;
|
||||
|
||||
public int getOffset() {
|
||||
return (this.page - 1) * this.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
}
|
||||
|
||||
public Integer getPage() {
|
||||
return page;
|
||||
}
|
||||
|
||||
public void setPage(Integer page) {
|
||||
this.page = page;
|
||||
}
|
||||
|
||||
public Integer getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public void setSize(Integer size) {
|
||||
this.size = size;
|
||||
}
|
||||
}
|
||||
45
src/main/java/com/yexuejc/base/pojo/ParamsPO.java
Normal file
45
src/main/java/com/yexuejc/base/pojo/ParamsPO.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import com.yexuejc.base.util.JsonUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 解密前的请求参数格式
|
||||
*
|
||||
* @author: maxf
|
||||
* @date: 2018/5/12 14:52
|
||||
*/
|
||||
public class ParamsPO implements Serializable {
|
||||
private static final long serialVersionUID = 9171765814642105098L;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String data;
|
||||
/**
|
||||
* md5
|
||||
*/
|
||||
private String sign;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonUtil.obj2Json(this);
|
||||
}
|
||||
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getSign() {
|
||||
return sign;
|
||||
}
|
||||
|
||||
public void setSign(String sign) {
|
||||
this.sign = sign;
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ public class JwtUtil {
|
||||
/** token类型 */
|
||||
private static String JWT_HEADER_TYP = "JWT";
|
||||
/** token发行商 */
|
||||
private static String JWT_CLAIMS_ISS = "uselaw.com";
|
||||
private static String JWT_CLAIMS_ISS = "yexuejc.com";
|
||||
|
||||
/**
|
||||
* 加密内容生成token
|
||||
|
||||
@@ -3,37 +3,52 @@ package com.yexuejc.base.util;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static java.util.regex.Pattern.compile;
|
||||
|
||||
/**
|
||||
* @PackageName: com.yexuejc.util.base.util
|
||||
* 字符串工具类
|
||||
*
|
||||
* @ClassName: StrUtil
|
||||
* @Description:
|
||||
* @author: maxf
|
||||
* @date: 2017/12/28 17:34
|
||||
* @date: 2018/5/12 19:13
|
||||
*/
|
||||
public final class StrUtil {
|
||||
public static char[] HEX_CHAR = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
|
||||
'e', 'f'};
|
||||
|
||||
private StrUtil() {
|
||||
}
|
||||
public static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
||||
/**
|
||||
* 判断字符串,数组,集合 是否为空
|
||||
*
|
||||
* @param obj
|
||||
* @return
|
||||
*/
|
||||
public static boolean isEmpty(Object obj) {
|
||||
if (obj instanceof String) {
|
||||
return obj == null || "".equals((String) obj);
|
||||
if (obj == null || "".equals((String) obj)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (obj instanceof Object[]) {
|
||||
return obj == null || ((Object[]) ((Object[]) obj)).length == 0;
|
||||
} else if (obj instanceof Collection) {
|
||||
return obj == null || ((Collection) obj).size() == 0;
|
||||
if (obj == null || ((Object[]) obj).length == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (obj instanceof Collection<?>) {
|
||||
if (obj == null || ((Collection<?>) obj).size() == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return obj == null;
|
||||
if (obj == null) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,10 +56,23 @@ public final class StrUtil {
|
||||
return !isEmpty(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成UUID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String genUUID() {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成11位编号,可以用作订单号,有很小几率出现重复,需要做异常处理<br/>
|
||||
* 左边第一位为正负标识:正数1 负数0<br/>
|
||||
* 剩余位数为UUID的hashcode值<br/>
|
||||
* 可以再生成的编号基础上嵌入其他标识编码
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String genNum() {
|
||||
int hashCode = UUID.randomUUID().toString().hashCode();
|
||||
StringBuffer num = new StringBuffer();
|
||||
@@ -54,131 +82,215 @@ public final class StrUtil {
|
||||
} else {
|
||||
num.append("1");
|
||||
}
|
||||
|
||||
return num.append(String.format("%010d", hashCode)).toString().substring(0, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析aa=bb&cc=dd&ee=ff格式的字符串,返回HashMap
|
||||
*
|
||||
* @param urlencoded
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, String> parseUrlencoded(String urlencoded) {
|
||||
if (isEmpty(urlencoded)) {
|
||||
return null;
|
||||
} else {
|
||||
String[] entrys = urlencoded.split("&");
|
||||
if (isEmpty(entrys)) {
|
||||
return null;
|
||||
}
|
||||
String[] entrys = urlencoded.split("&");
|
||||
if (isEmpty(entrys)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
String[] kv = null;
|
||||
for (String entry : entrys) {
|
||||
if (isEmpty(entry)) {
|
||||
continue;
|
||||
}
|
||||
kv = entry.split("=");
|
||||
if (isEmpty(kv)) {
|
||||
continue;
|
||||
}
|
||||
if (kv.length > 1) {
|
||||
map.put(kv[0], kv[1]);
|
||||
} else {
|
||||
Map<String, String> map = new HashMap();
|
||||
String[] kv = null;
|
||||
String[] var4 = entrys;
|
||||
int var5 = entrys.length;
|
||||
|
||||
for (int var6 = 0; var6 < var5; ++var6) {
|
||||
String entry = var4[var6];
|
||||
if (!isEmpty(entry)) {
|
||||
kv = entry.split("=");
|
||||
if (!isEmpty(kv)) {
|
||||
if (kv.length > 1) {
|
||||
map.put(kv[0], kv[1]);
|
||||
} else {
|
||||
map.put(kv[0], null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
map.put(kv[0], null);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String toHex(byte[] buf) {
|
||||
/**
|
||||
* 字符串转换方法 把字节数组转换成16进制字符串
|
||||
*
|
||||
* @param buf 初始字节数组
|
||||
* @return 转换后字符串
|
||||
*/
|
||||
public static String toHex(byte buf[]) {
|
||||
StringBuffer strbuf = new StringBuffer(buf.length * 2);
|
||||
|
||||
for (int i = 0; i < buf.length; ++i) {
|
||||
if ((buf[i] & 255) < 16) {
|
||||
int i;
|
||||
for (i = 0; i < buf.length; i++) {
|
||||
if (((int) buf[i] & 0xff) < 0x10) {
|
||||
strbuf.append("0");
|
||||
}
|
||||
|
||||
strbuf.append(Long.toString((long) (buf[i] & 255), 16));
|
||||
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
|
||||
}
|
||||
|
||||
return strbuf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字符串的MD5码
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String toMD5(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
} else {
|
||||
MessageDigest md = null;
|
||||
|
||||
try {
|
||||
md = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException var3) {
|
||||
var3.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
md.update(str.getBytes());
|
||||
byte[] tmp = md.digest();
|
||||
return toHex(tmp);
|
||||
}
|
||||
MessageDigest md = null;
|
||||
try {
|
||||
md = java.security.MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
md.update(str.getBytes());
|
||||
byte tmp[] = md.digest();
|
||||
return toHex(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* SHA256加密
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String toSHA256(final String str) {
|
||||
return toSHA(str, "SHA-256");
|
||||
}
|
||||
|
||||
/**
|
||||
* SHA加密
|
||||
*
|
||||
* @param str
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public static String toSHA(final String str, final String key) {
|
||||
// 是否是有效字符串
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
MessageDigest messageDigest = null;
|
||||
try {
|
||||
messageDigest = MessageDigest.getInstance(key);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
messageDigest.update(str.getBytes());
|
||||
byte tmp[] = messageDigest.digest();
|
||||
return toHex(tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用ISO-8859-1解码 再用UFT-8编码
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static String iso2utf(String str) {
|
||||
String utfStr = null;
|
||||
|
||||
try {
|
||||
utfStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
|
||||
} catch (UnsupportedEncodingException var3) {
|
||||
var3.printStackTrace();
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return utfStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断字符串是否是数字
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static boolean isNumeric(String str) {
|
||||
Pattern pattern = compile("[0-9]*");
|
||||
Pattern pattern = Pattern.compile("[0-9]*");
|
||||
Matcher isNum = pattern.matcher(str);
|
||||
return isNum.matches();
|
||||
if (!isNum.matches()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对ID(32位)进行编码
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
public static String codeId(String id) {
|
||||
if (id != null && id.length() == 32) {
|
||||
StringBuilder coded = new StringBuilder();
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 13; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
coded.append(id.substring(0, 11));
|
||||
|
||||
for (i = 0; i < 7; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
coded.append(id.substring(11));
|
||||
|
||||
for (i = 0; i < 12; ++i) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15.0D) + 1]);
|
||||
}
|
||||
|
||||
return coded.toString();
|
||||
} else {
|
||||
if (id == null || id.length() != 32) {
|
||||
return id;
|
||||
}
|
||||
|
||||
StringBuilder coded = new StringBuilder();
|
||||
for (int i = 0; i < 13; i++) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
|
||||
}
|
||||
coded.append(id.substring(0, 11));
|
||||
for (int i = 0; i < 7; i++) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
|
||||
}
|
||||
coded.append(id.substring(11));
|
||||
for (int i = 0; i < 12; i++) {
|
||||
coded.append(HEX_CHAR[(int) (Math.random() * 15L) + 1]);
|
||||
}
|
||||
|
||||
return coded.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 对ID(32位)进行解码
|
||||
*
|
||||
* @param coded
|
||||
* @return
|
||||
*/
|
||||
public static String decodeId(String coded) {
|
||||
if (coded != null && coded.length() == 64) {
|
||||
StringBuilder id = new StringBuilder();
|
||||
id.append(coded.substring(13, 24));
|
||||
id.append(coded.substring(31, 52));
|
||||
return id.toString();
|
||||
} else {
|
||||
if (coded == null || coded.length() != 64) {
|
||||
return coded;
|
||||
}
|
||||
|
||||
StringBuilder id = new StringBuilder();
|
||||
id.append(coded.substring(13, 24));
|
||||
id.append(coded.substring(31, 52));
|
||||
|
||||
return id.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* map parameters 转url parameters
|
||||
*
|
||||
* @param sortedParams
|
||||
* @return
|
||||
*/
|
||||
public static String getSignContent(Map<String, ?> sortedParams) {
|
||||
StringBuffer content = new StringBuffer();
|
||||
List<String> keys = new ArrayList<>(sortedParams.keySet());
|
||||
Collections.sort(keys);
|
||||
int index = 0;
|
||||
|
||||
for (int i = 0; i < keys.size(); ++i) {
|
||||
String key = keys.get(i);
|
||||
Object value = sortedParams.get(key);
|
||||
if (isNotEmpty(key) && isNotEmpty(value)) {
|
||||
content.append((index == 0 ? "" : "&") + key + "=" + value);
|
||||
++index;
|
||||
}
|
||||
}
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换手机号中间4位为*
|
||||
@@ -192,4 +304,38 @@ public final class StrUtil {
|
||||
public static String replaceMobile(String mobile) {
|
||||
return mobile.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
|
||||
}
|
||||
|
||||
/**
|
||||
* map 排序
|
||||
*
|
||||
* @param sortedParams
|
||||
* @return
|
||||
*/
|
||||
public static Map<String, Object> mapSort(Map<String, ?> sortedParams) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
List<String> keys = new ArrayList<>(sortedParams.keySet());
|
||||
Collections.sort(keys);
|
||||
int index = 0;
|
||||
for (int i = 0; i < keys.size(); ++i) {
|
||||
String key = keys.get(i);
|
||||
Object value = sortedParams.get(key);
|
||||
map.put(key, value);
|
||||
++index;
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置Str带默认值
|
||||
*
|
||||
* @param msg
|
||||
* @param defMsg
|
||||
* @return
|
||||
*/
|
||||
public static String setStr(String msg, String defMsg) {
|
||||
if (StrUtil.isEmpty(msg)) {
|
||||
return defMsg;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.yexuejc.base.util;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 系统工具类
|
||||
*
|
||||
@@ -22,4 +24,15 @@ public class SysUtils {
|
||||
public static String getCachePath() {
|
||||
return System.getProperty(PROJECT_ROOT_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从根路径获取文件URL
|
||||
*
|
||||
* @param clazz
|
||||
* @return
|
||||
*/
|
||||
public static URL getRootPath(Class clazz, String filePath) {
|
||||
return clazz.getClass().getResource(StrUtil.setStr(filePath, "/"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user