首页 > 解决方案 > Spring SAML 无法从属性文件中读取属性

问题描述

所以这个难倒我。多年来我一直在使用属性文件,但我从未见过这种情况。

我正在使用带有 SAML 身份验证的 Spring MVC。我的上下文 xml 中有这个:

<context:property-placeholder location="file:/opt/saml.properties" />

<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
    <constructor-arg value="file:/opt/mySamlKeystore.jks"/>
    <constructor-arg type="java.lang.String" value="${keystore.password}"/>
    <constructor-arg>
        <map>
            <entry key="${privateKey.alias}" value="${key.password}"/>
        </map>
    </constructor-arg>
    <constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>

我收到此错误:

java.security.UnrecoverableKeyException: Cannot recover key

我做了一些 SO 研究,他们基本上都说我的密码错误,我确定我没有。因此,为了测试它是否读取了正确的文件,我去替换所有属性 %{} 并对它们进行硬编码。然后一切正常。

当我注意到该文件中的一些其他属性正在工作时,我试图弄清楚这一点!事实上,我什至可以这样做:

<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
    <constructor-arg value="file:/opt/myKeystore.jks"/>
    <constructor-arg type="java.lang.String" value="${keystore.password}"/>
    <constructor-arg>
        <map>
            <entry key="${privateKey.alias}" value="password"/>
        </map>
    </constructor-arg>
    <constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>

所以 spring 从属性文件中获取 ${keystore.password} 和 ${privateKey.alias} (以及其他需要的,如 entityID、metadataProvider 等),但不是 ${key.password} !!!

这是 saml.properties

#keystore stuff
keystore.password=password
key.password=password 
privateKey.alias=mysaml

#SP stuff   (aka,  my side of things)
entity.id=mycompany:me:me:me1
entity.base.url=https://mycompany.com

#IDP stuff   (aka, the SAML server)
metadata.provider=https://saml.mycompany.com/FederationMetadata/2007-06/FederationMetadata.xml

当我硬编码密钥密码时,这一切都有效,但当我使用 ${key.password} 属性时,这一切都有效。这里发生了什么?

标签: javaspringspring-securitysaml

解决方案


文件后还有两个斜杠

例如

<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
    <constructor-arg value="file:///opt/myKeystore.jks"/>
    <constructor-arg type="java.lang.String" value="${keystore.password}"/>
    <constructor-arg>
        <map>
            <entry key="${privateKey.alias}" value="password"/>
        </map>
    </constructor-arg>
    <constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>

推荐阅读