2017-05-31 60 views
1

在spring.application中,我已指定密鑰庫名稱和密碼。 我想簽署一些數據與Java安全Signature.sign(),但要做到這一點,我需要密鑰庫。有沒有辦法讓Spring託管密鑰庫bean,或者我是否必須創建自己的密鑰庫,即使它已經被spring使用?彈簧密鑰庫bean

回答

1

您可以只使用

@Autowired 
private KeyStore keyStore; 

如果你已經在你的XML/Java配置正確配置它像這樣

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:crypt="http://springcryptoutils.com/schema/crypt" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
         http://springcryptoutils.com/schema/crypt http://springcryptoutils.com/schema/crypt.xsd"> 

    <crypt:keystore id="keystore" 
       location="somePath}" 
       password="somePassword"/> 
</beans> 
+0

任何方式與註釋/ java的配置呢? – Sarief

+0

應該是這樣的: @Bean public KeyStore keyStore()throws IOException,KeyStoreException,CertificateException,NoSuchAlgorithmException最終KeyStore keyStore = KeyStore.getInstance(「JKS」); (新路徑)。getInputStream(),「password」.toCharArray()); return keyStore; } 我不完全確定ClassPathResource,但應該很容易找到:) – N4zroth

+0

是的,我實施了這個,它的工作原理,雖然我希望開箱即用。無論如何,非常感謝你,批准。如果你添加這個答案 - 完美:) – Sarief