簡體   English   中英

WSS4J中密鑰庫的可配置位置

[英]Configurable location of keystore in WSS4J

我有一個spring上下文初始化CXF Web服務並用簽名檢查包裝它:

<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:my.properties</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="properties" ref="myProperties"/>
    <property name="placeholderPrefix" value="!sp{"/>
    <property name="placeholderSuffix" value="}"/>
</bean>

<bean id="inbound-security" class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
    <constructor-arg>
        <map>
            <entry key="action" value="Signature"/>
            <entry key="signaturePropFile" value="!sp{acq.signature.properties.location}"/>
        </map>
    </constructor-arg>
</bean>

我意識到signaturePropFile必須在classpath上,它無法從文件系統中讀取:-(

Caused by: org.apache.ws.security.WSSecurityException: General security error (Cannot load the resource D:\Dev\Projekty\smartpos-backend-parent\smartpos-backend-acquirer\src\main\resources\signature.properties)
at org.apache.ws.security.components.crypto.CryptoFactory.getProperties(CryptoFactory.java:261) ~[wss4j-1.6.11.jar:1.6.11]
at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:186) ~[wss4j-1.6.11.jar:1.6.11]
at org.apache.cxf.ws.security.wss4j.AbstractWSS4JInterceptor.loadCryptoFromPropertiesFile(AbstractWSS4JInterceptor.java:224) ~[cxf-rt-ws-security-2.7.5.jar:2.7.5]
at org.apache.ws.security.handler.WSHandler.loadCrypto(WSHandler.java:911) ~[wss4j-1.6.11.jar:1.6.11]

沒關系,我讓它成為部署的一部分,但我確實想要外化keystore,它是用以下屬性定義的:

org.apache.ws.security.crypto.merlin.keystore.file=server-keystore.jks

我試圖用一些配置屬性替換路徑!sp{keystore.location}${keystore.location} ,但它不起作用。 事實上,它失敗了同樣的異常,如屬性文件不存在:

Caused by: org.apache.ws.security.WSSecurityException: General security error (Cannot load the resource classpath:signature.properties)
at org.apache.ws.security.components.crypto.CryptoFactory.getProperties(CryptoFactory.java:261) ~[wss4j-1.6.11.jar:1.6.11]
at org.apache.ws.security.components.crypto.CryptoFactory.getInstance(CryptoFactory.java:186) ~[wss4j-1.6.11.jar:1.6.11]
at org.apache.cxf.ws.security.wss4j.AbstractWSS4JInterceptor.loadCryptoFromPropertiesFile(AbstractWSS4JInterceptor.java:224) ~[cxf-rt-ws-security-2.7.5.jar:2.7.5]
at org.apache.ws.security.handler.WSHandler.loadCrypto(WSHandler.java:911) ~[wss4j-1.6.11.jar:1.6.11]

配置WSS4J密鑰庫位置的正確方法是什么? 我不喜歡在部署之前編輯戰爭。 (我用maven來構建它)。

不確定它會有所幫助,但我有一個類似的目標:我想指向我的jar外的密鑰庫,因為它取決於最終的用戶證書。

假設您的Manifest.mf具有此類路徑設置:

類路徑:。

您可以簡單地將外部文件夾定位在與jar相同的目錄中。 就我而言,我做了以下事情:

org.apache.ws.security.crypto.merlin.file =配置文件夾/ mycert.jks

在我的application.jar文件所在的目錄中是config-folder

這個問題最近已經解決了

使用舊版本的wss4j(v2.1.1及更早版本)時,可以通過覆蓋WSHandler.loadCrypto()並通過Spring util注入創建的Property對象來傳遞屬性文件的String引用(例如WSHandlerConstants.SIG_PROP_REF_ID)。 ,例如:

<util:properties id="wss4jCryptoProperties">
    <prop key="org.apache.ws.security.crypto.merlin.keystore.file">!sp{keystore.file}</prop>
    <prop key="org.apache.ws.security.crypto.merlin.keystore.type">!sp{keystore.type}</prop>
    <prop key="org.apache.ws.security.crypto.merlin.keystore.password">!sp{keystore.password}</prop>
</util:properties>

通過一個例子覆蓋WSHandler.loadCrypto():

public class PropertiesWSS4JInInterceptor extends WSS4JInInterceptor {

    private Properties cryptoProperties;

    public PropertiesWSS4JInInterceptor(Map<String, Object> inProps,
                                        Properties cryptoProperties) {
        super(inProps);
        this.cryptoProperties = cryptoProperties;
    }

    @Override
    protected Crypto loadCrypto(String cryptoPropertyFile, String cryptoPropertyRefId,
                                RequestData requestData) throws WSSecurityException {
        return CryptoFactory.getInstance(cryptoProperties);
    }
}

此外,您可以在customBean中注入wss4jCryptoProperties(不要忘記在引用的類和setter中創建名為cryptoProperties的字段):

<bean id="customBean" class="cz.company.CustomBean">
    <property name="cryptoProperties" ref="wss4jCryptoProperties"/>
</bean>

最后,您可以將攔截器添加到端點:

endpoint.getInInterceptors().add(new PropertiesWSS4JInInterceptor(inProps, cryptoProperties));

我正在使用此頁面解決從外部文件夾引用它的問題。 實際上在轉換屬性路徑* org.apache.ws.security.crypto.merlin.keystore.file *時,它正被送到FileSystem。 文件路徑) -

Merlin.java:333

實際上可以從外部文件夾解密文件的路徑。 無需做額外的工作。

org.apache.ws.security.crypto.merlin.keystore.file = / usr / local / path / server-keystore.jks有效!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM