diff --git a/README.md b/README.md index ce3fb99..bf75963 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,6 @@ eyJhbGciOiJIUzUxMiJ9.eyJhdWQiOiJCRDg0REQ0MkE3MjM0QjA1QjBDNUQxMTYxNjEzMkFDNCIsImp 2、[php-jwt](https://github.com/firebase/php-jwt) -[aa](http://localhost:8090/) \ No newline at end of file +3、[Java读取OpenSSL生成的PEM公钥文件操作](http://www.zzvips.com/article/108821.html) + +4、[java - 如何读取.pem文件以获取私钥和​​公钥](https://mlog.club/article/130323) \ No newline at end of file diff --git a/pom.xml b/pom.xml index b0d5563..5e2dcd1 100644 --- a/pom.xml +++ b/pom.xml @@ -1,130 +1,145 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.8 - - - cn.chjyj - szwh - 0.0.1-SNAPSHOT - szwh - 深圳文化交易所api系统 - - 1.8 - - - - org.springframework.boot - spring-boot-starter-web - - - org.mybatis.spring.boot - mybatis-spring-boot-starter - 2.2.2 - - - org.springframework.boot - spring-boot-starter-data-redis - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.8 + + + cn.chjyj + szwh + 1.0.1 + jar + szwh + 深圳文化交易所api系统 + + 1.8 + + + + org.springframework.boot + spring-boot-starter-web + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + org.springframework.boot + spring-boot-starter-data-redis + - - org.springframework.boot - spring-boot-devtools - runtime - true - - - mysql - mysql-connector-java - 8.0.29 - - - org.springframework.boot - spring-boot-starter-test - test - - - - ch.qos.logback - logback-classic - + + org.springframework.boot + spring-boot-devtools + runtime + true + + + mysql + mysql-connector-java + 8.0.29 + + + org.springframework.boot + spring-boot-starter-test + test + + + + ch.qos.logback + logback-classic + - - - com.github.pagehelper - pagehelper - 5.3.0 - - - - commons-codec - commons-codec - 1.9 - - - commons-io - commons-io - 2.6 - - - commons-lang - commons-lang - 2.6 - - - org.apache.commons - commons-lang3 - - - junit - junit - test - - - - com.alibaba - fastjson - 1.2.83 - - - - com.alibaba - druid - 1.2.6 - - - - org.apache.httpcomponents - httpclient - 4.5.9 - + + + com.github.pagehelper + pagehelper + 5.3.0 + + + + commons-codec + commons-codec + 1.9 + + + commons-io + commons-io + 2.6 + + + commons-lang + commons-lang + 2.6 + + + org.apache.commons + commons-lang3 + + + junit + junit + test + + + + com.alibaba + fastjson + 1.2.83 + + + + com.alibaba + druid + 1.2.6 + + + + org.apache.httpcomponents + httpclient + 4.5.9 + - - - io.jsonwebtoken - jjwt - 0.9.1 - - - - com.auth0 - java-jwt - 3.8.3 - + + + io.jsonwebtoken + jjwt + 0.9.1 + + + + com.auth0 + java-jwt + 3.8.3 + - + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-resources-plugin + + UTF-8 + + + pem + pfx + + + + + + diff --git a/src/main/java/cn/chjyj/szwh/utils/ApiTokenUtils.java b/src/main/java/cn/chjyj/szwh/utils/ApiTokenUtils.java index d431920..fb6e490 100644 --- a/src/main/java/cn/chjyj/szwh/utils/ApiTokenUtils.java +++ b/src/main/java/cn/chjyj/szwh/utils/ApiTokenUtils.java @@ -2,6 +2,7 @@ package cn.chjyj.szwh.utils; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; +import org.apache.commons.codec.binary.Base64; import java.security.Key; import java.security.KeyFactory; @@ -78,12 +79,44 @@ public class ApiTokenUtils { .compact(); } + /** + * 抽取公钥 + * @param publicKey + * @return + * @throws NoSuchAlgorithmException + * @throws InvalidKeySpecException + */ public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { - return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(publicKey.getBytes()))); + String publicKeyPEM = publicKey.replace("-----BEGIN PUBLIC KEY-----\r\n", ""); + publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); + + Base64 b64 = new Base64(); + byte [] decoded = b64.decode(publicKeyPEM); + + X509EncodedKeySpec spec = + new X509EncodedKeySpec(decoded); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return (RSAPublicKey) kf.generatePublic(spec); + //return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(publicKey.getBytes()))); } + /** + * 抽取私钥 + * @param privateKey + * @return + * @throws Exception + */ public static RSAPrivateKey getPrivateKey(String privateKey) throws Exception { - return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(privateKey.getBytes()))); + String privKeyPEM = privateKey.replace("-----BEGIN PRIVATE KEY-----\r\n", ""); + privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); + Base64 b64 = new Base64(); + byte [] decoded = b64.decode(privKeyPEM); + + PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return (RSAPrivateKey)kf.generatePrivate(spec); + + //return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(privateKey.getBytes()))); } public static Key stringToPublickKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException { diff --git a/src/main/java/cn/chjyj/szwh/utils/SzFileUtils.java b/src/main/java/cn/chjyj/szwh/utils/SzFileUtils.java index 497b37f..028c96a 100644 --- a/src/main/java/cn/chjyj/szwh/utils/SzFileUtils.java +++ b/src/main/java/cn/chjyj/szwh/utils/SzFileUtils.java @@ -21,7 +21,9 @@ public class SzFileUtils { String readLine = null; StringBuffer sb = new StringBuffer(); while ((readLine = br.readLine()) != null) { - sb.append(readLine); + //剔除--- + readLine.replace("-----\\w-----"," "); + sb.append(readLine+"\r\n"); } br.close(); ins.close(); diff --git a/src/test/java/cn/chjyj/szwh/JwtRsaTokenTests.java b/src/test/java/cn/chjyj/szwh/JwtRsaTokenTests.java index 171b1a0..74868e7 100644 --- a/src/test/java/cn/chjyj/szwh/JwtRsaTokenTests.java +++ b/src/test/java/cn/chjyj/szwh/JwtRsaTokenTests.java @@ -1,6 +1,8 @@ package cn.chjyj.szwh; import cn.chjyj.szwh.constant.ChConstant; +import cn.chjyj.szwh.utils.ApiTokenUtils; +import cn.chjyj.szwh.utils.JwtUtils; import cn.chjyj.szwh.utils.SzFileUtils; import org.junit.Test; import org.springframework.boot.test.context.SpringBootTest; @@ -29,7 +31,11 @@ public class JwtRsaTokenTests { String entr_prikey=prop.getProperty("entrust.pem.prikey.path");//私钥路径 // 私钥 String prinote = SzFileUtils.getKeyFromFile(ChConstant.WORK_DIR + "/" + entr_prikey); - System.out.println("文件内容"+prinote); +// System.out.println(prinote); + String iss ="test"; + long exp =5 * 60 * 1000L; + String token = ApiTokenUtils.getToken(iss,prinote,exp); + System.out.println(token); }catch (Exception ex){ ex.printStackTrace(); }