1. 程式人生 > >Springboot讀取祕鑰檔案

Springboot讀取祕鑰檔案

涉及支付需要在資原始檔下resource下放置祕鑰,java程式碼裡讀取

首先在pom檔案裡配置以下配置,不然在不會編譯在class包裡

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration><encoding>UTF-8</encoding>
        <!-- 過濾字尾為pem、pfx的證書檔案 -->
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>cer</nonFilteredFileExtension>
            <nonFilteredFileExtension>pem</nonFilteredFileExtension>
            <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

java讀取方式:

public static InputStream getInputStreamtByPath(String enterpriseCerFilePath) throws IOException {
   try{
      InputStream stream = BhRSAUtil.class.getClassLoader().getResourceAsStream(enterpriseCerFilePath);
      return stream;
   }catch(Exception ioe){
      throw ioe;
   }
}

public static InputStream getSignCertByJarFile2(String enterpriseCerFilePath) throws IOException {
   try{
      ClassPathResource ClassPathResource = new ClassPathResource(enterpriseCerFilePath);
      return ClassPathResource.getInputStream();
   }catch(IOException ioe){
      ioe.printStackTrace();
      throw ioe;
   }
}