ad8
现象
- 读取不到证书文件
- 证书文件变大
解决方案
读取不到证书文件
由于是springboot项目,部署时打包成jar启动,无法获取到有效的证书文件地址。解决方案:首次访问时从jar中提取证书文件到当前目录中,代码如下:
/*** 获取jar包中证书文件地址** @param fileName 证书名称* @return*/public static synchronized String getDefaultPath(String fileName) {String path;try {// 获取当前项目根目录,兼容不同操作系统path = FileUtils.class.getResource(\"/\").getPath();String osName = System.getProperties().getProperty(\"os.name\");if (osName.startsWith(\"Windows\")) {path = path.replaceFirst(\"file:/\", \"\");} else {path = path.replaceFirst(\"file:\", \"\");}int i = path.indexOf(\"projectName.jar\");// 拼接证书存放位置if (i > 0) {path = path.substring(0, i) + fileName;} else {path = path + fileName;}// 证书不存在,从jar包中提取if (!new File(path).exists()) {InputStream is = FileUtils.class.getResourceAsStream(\"/\" + fileName);try (BufferedInputStream in = new BufferedInputStream(is); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(path))) {int len;byte[] b = new byte[1024];while ((len = in.read(b)) != -1) {out.write(b, 0, len);}}}} catch (Exception e) {path = \"/usr/local/springboot/app/xxx/\" + fileName;System.out.println(String.format(\"file %s is not found,error is %s, use default path %s\", fileName, e.getMessage(), path));}return path;}
证书文件变大
从jar中提取到的证书文件大小与实际不一致,变大了。原因:maven打包对项目进行统一编码,但是部分文件可能不需要进行重新编码,例如: 证书文件,重新编码后可能导致证书不可用。解决方案如下:
<plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><configuration><nonFilteredFileExtensions><nonFilteredFileExtension>jks</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin><56c;/plugins>