From 8e8b4f0c00a972e7a58451d2a6548560a531ea20 Mon Sep 17 00:00:00 2001 From: xioayue Date: Mon, 4 Jul 2022 14:44:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AD=BE=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/szwh.properties | 5 +- .../java/cn/chjyj/szwh/utils/SignUtils.java | 109 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/chjyj/szwh/utils/SignUtils.java diff --git a/conf/szwh.properties b/conf/szwh.properties index 203dafc..9065b49 100644 --- a/conf/szwh.properties +++ b/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 #资源下载 -distribute。pem.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 diff --git a/src/main/java/cn/chjyj/szwh/utils/SignUtils.java b/src/main/java/cn/chjyj/szwh/utils/SignUtils.java new file mode 100644 index 0000000..5908c5c --- /dev/null +++ b/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("私钥证书不存在"); + } + } +}