2 changed files with 113 additions and 1 deletions
@ -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…
Reference in new issue