Browse Source

完成签名

master
xyiege 4 years ago
parent
commit
8e8b4f0c00
  1. 5
      conf/szwh.properties
  2. 109
      src/main/java/cn/chjyj/szwh/utils/SignUtils.java

5
conf/szwh.properties

@ -5,12 +5,15 @@ jwt.key=wenhuayun_token_
# 用户认证
user.pem.prikey.path=/conf/cert/user_real/private_key.pem
user.pem.pubkey.path=/conf/cert/user_real/public_key.pem
user.pem.token=FSRES7DTTJS26NJT8IV7WJIW6MNBEB7P
# 委托系统
entrust.pem.prikey.path=/conf/cert/entrust/private_key.pem
entrust.pem.pubkey.path=/conf/cert/entrust/public_key.pem
entrust.pem.token=KHXT0V7NVLOFPS9BZ88R5VLIH5COPULV
#资源下载
distributepem.prikey.path=/conf/cert/distribute/private_key.pem
distribute.pem.prikey.path=/conf/cert/distribute/private_key.pem
distribute.pem.pubkey.path=/conf/cert/distribute/public_key.pem
distribute.pem.token=4b4858543056374e564c4f4650533942
# 分发系统地址
ENTRUST_URL = http://10.24.4.14:51317

109
src/main/java/cn/chjyj/szwh/utils/SignUtils.java

@ -0,0 +1,109 @@
package cn.chjyj.szwh.utils;
import cn.chjyj.szwh.constant.ChConstant;
import cn.chjyj.szwh.exception.ChException;
import com.auth0.jwt.JWT;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 远程请求签名工具
*/
public class SignUtils {
private static Log log = LogFactory.getLog(SignUtils.class);
/**
* 用户认证 证书路径map
*
* @return
*/
private static Map userRealMap() {
Map umap = new HashMap();
umap.put("private_key", ProperUtils.getSzwhProp("user.pem.prikey.path"));
umap.put("public_key", ProperUtils.getSzwhProp("user.pem.pubkey.path"));
umap.put("pem_token", ProperUtils.getSzwhProp("user.pem.token"));
return umap;
}
/**
* 委托系统证书资源
*
* @return
*/
private static Map entrustMap() {
Map umap = new HashMap();
umap.put("private_key", ProperUtils.getSzwhProp("entrust.pem.prikey.path"));
umap.put("public_key", ProperUtils.getSzwhProp("entrust.pem.pubkey.path"));
umap.put("pem_token", ProperUtils.getSzwhProp("entrust.pem.token"));
return umap;
}
/**
* 资源下载map
*
* @return
*/
private static Map distributeMap() {
Map umap = new HashMap();
umap.put("private_key", ProperUtils.getSzwhProp("distribute.pem.prikey.path"));
umap.put("public_key", ProperUtils.getSzwhProp("distribute.pem.pubkey.path"));
umap.put("pem_token", ProperUtils.getSzwhProp("distribute.pem.token"));
return umap;
}
/**
* 输入类型返回对应的信息
*
* @param type
* @return
*/
private static Map getTypeMap(String type) {
Map nmap = new HashMap();
switch (type) {
case "user_real":
nmap = userRealMap();
break;
case "entrust":
nmap = entrustMap();
break;
case "distribute":
nmap = distributeMap();
break;
}
return nmap;
}
/**
* 创建签名
*
* @param type
* @return
*/
public static String createSign(String type) {
String signStr = "";//签名后的字符
long exp = 5 * 60 * 1000l; //5分钟超时
Date date = new Date(System.currentTimeMillis());
Map xmap = getTypeMap(type);
String private_key_path = (String) xmap.get("private_key");//私钥存放位置
try {
// 私钥
String prinote = SzFileUtils.getKeyFromFile(ChConstant.WORK_DIR + "/" + private_key_path);
signStr = Jwts.builder()
.signWith(SignatureAlgorithm.RS256, prinote)
.setIssuer((String) xmap.get("pem_token"))
.setExpiration(new Date(date.getTime() + exp))
.setIssuedAt(date)
.compact();
return signStr;
} catch (Exception ex) {
log.error(ex.getCause());
throw new ChException("私钥证书不存在");
}
}
}
Loading…
Cancel
Save