matsukaz's blog

Agile, node.js, ruby, AWS, cocos2d-xなどなどいろいろやってます

Spring Web Services まとめ その11(ラスト)

Encryption and Decryption(暗号/復号化)

復号化

SOAPの入力メッセージを復号化するためには、セキュリティ・ポリシー・ファイルにRequireEncryption要素を記述する必要がある。また、メッセージのうち暗号化された部分を表すためのEncryptionTarget要素を記述することもできる。指定可能な子要素は、ここで参照することができる。

<xwss:SecurityConfiguration xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
    <xwss:RequireEncryption />
</xwss:SecurityConfiguration>

もしも入力メッセージが暗号化されていなかった場合は、XwsSecurityInterceptorがSOAPフォルトを送信元に返す。もしも暗号化されていた場合は、DecryptionKeyCallbackを実行し、登録したハンドラが呼ばれる。Spring-WSでは、登録可能なハンドラはKeyStoreCallbackHandlerのみである。
以下は、KeyStoreCallbackHandlerの設定例である。

<beans>
    <bean id="keyStoreHandler" class="org.springframework.ws.soap.security.xwss.callback.KeyStoreCallbackHandler">
        <property name="keyStore" ref="keyStore"/>
        <property name="privateKeyPassword" value="changeit"/>
    </bean>

    <bean id="keyStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean">
        <property name="location" value="classpath:keystore.jks"/>
        <property name="password" value="changeit"/>
    </bean>
</beans>
暗号化

SOAPの出力メッセージを暗号化するためには、セキュリティ・ポリシー・ファイルにEncrypt要素を記述する必要がある。また、メッセージのうち暗号化対象の部分を表すためのEncryptionTarget要素を記述することもできる。指定可能な子要素は、ここで参照することができる。

<xwss:SecurityConfiguration xmlns:xwss="http://java.sun.com/xml/ns/xwss/config">
    <xwss:Encrypt />
</xwss:SecurityConfiguration>

XwsSecurityInterceptorは暗号化された情報を取り出すため、EncryptionKeyCallbackを実行し、登録したハンドラが呼ばれる。Spring-WSでは、登録可能なハンドラはKeyStoreCallbackHandlerのみである。
以下は、KeyStoreCallbackHandlerの設定例である。

<beans>
    <bean id="keyStoreHandler" class="org.springframework.ws.soap.security.xwss.callback.KeyStoreCallbackHandler">
        <property name="trustStore" ref="trustStore"/>
    </bean>

    <bean id="trustStore" class="org.springframework.ws.soap.security.support.KeyStoreFactoryBean">
        <property name="location" value="classpath:truststore.jks"/>
        <property name="password" value="changeit"/>
    </bean>
</beans>