у нас есть веб-сервис, который получает следующее сообщение SOAP. Я публикую только заголовок, который является важной частью нашей проблемы.
<SOAP-ENV:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" SOAP-ENV:mustUnderstand="1">
<wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="X509-65E18DC0CA7D9A38B214168992655731">THE_CERTIFICATE</wsse:BinarySecurityToken>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="SIG-65E18DC0CA7D9A38B214168992656685">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList="SOAP-ENV"/>
</ds:CanonicalizationMethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/>
<ds:Reference URI="#id-65E18DC0CA7D9A38B214168992656044">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
<ec:InclusiveNamespaces xmlns:ec="http://www.w3.org/2001/10/xml-exc-c14n#" PrefixList=""/>
</ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
<ds:DigestValue>DIGEST_VALUE</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>THE_SIGNATURE</ds:SignatureValue>
<ds:KeyInfo Id="KI-65E18DC0CA7D9A38B214168992655892">
<wsse:SecurityTokenReference wsu:Id="STR-65E18DC0CA7D9A38B214168992655893">
<wsse:Reference URI="#X509-65E18DC0CA7D9A38B214168992655731" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3"/>
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
</wsse:Security>
Сертификат, используемый для подписания петиции, проверяется весной со следующей конфигурацией (на стороне сервера):
<sws:interceptors>
<bean class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="validationActions" value="Signature" />
<property name="validationSignatureCrypto">
<bean
class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
<property name="keyStorePassword" value="passtrustore" />
<property name="keyStoreLocation" value="classpath:/ts-webservice.jks" />
</bean>
</property>
<property name="securementActions" value="Signature" />
<property name="securementUsername" value="user" />
<property name="securementPassword" value="pass" />
<property name="securementSignatureKeyIdentifier" value="DirectReference" />
<property name="securementSignatureCrypto">
<bean
class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
<property name="keyStorePassword" value="passkeystore" />
<property name="keyStoreLocation" value="classpath:/ks-webservice.jks" />
</bean>
</property>
</bean>
</sws:interceptors>
Все работает нормально, но мы пытаемся извлечь binarySecurityToken в конечной точке, потому что мы используем этот сертификат, чтобы получить идентификатор подписывающего лица и вернуть в ответ некоторую личную информацию. Мы могли бы добавить его снова в качестве входного параметра для этого метода, но мы не хотим отправлять тот же сертификат дважды, если у нас уже есть его в заголовке.
Метод, который возвращает запрос, следующий:
@PayloadRoot(localPart = "ValidateUserRequest", namespace = GET_TARGET_NAMESPACE)
public @ResponsePayload
ValidateUserResponse validateUser(@RequestPayload ValidateUserRequest request, MessageContext messageContext) throws WSSecurityException,
CertificateException {
// read SOAP Header
SoapMessage mc = (SoapMessage) messageContext.getRequest();
String soapNamespace = WSSecurityUtil.getSOAPNamespace(mc.getDocument().getDocumentElement());
Element elem = WSSecurityUtil.getDirectChildElement(mc.getDocument().getDocumentElement(), WSConstants.ELEM_HEADER, soapNamespace);
// get the BinarySignature tag
// FIRST getFirstChild() is NULL if we have validated the request
Node binarySignatureTag = elem.getFirstChild().getFirstChild();
BinarySecurity token = new X509Security((Element) binarySignatureTag);
InputStream in = new ByteArrayInputStream(token.getToken());
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);
// do stuff with the certificate and return values
}
Когда Spring проверяет подпись запроса, кажется, что заголовки удалены, поэтому мы не можем получить доступ к первому дочернему элементу заголовка. Если мы прокомментируем часть проверки в нашем контексте приложения, предыдущий код работает как шарм, и мы получим сертификат.
Как мы можем избежать такого поведения? Почему заголовок из запроса удаляется после проверки?
Благодарю!
После долгих ночных исследований и чтения документации Spring я нашел обходное решение. Я не понимаю, почему Spring потребляет заголовки, или почему моя конечная точка не получает заголовки, однако мы могли бы извлечь сертификат с помощью следующего кода (с активированной проверкой подписи):
@PayloadRoot(localPart = "ValidateUserRequest", namespace = GET_TARGET_NAMESPACE)
public @ResponsePayload ValidateUserResponse validateUser(@RequestPayload ValidateUserRequest request, MessageContext messageContext) throws WSSecurityException, CertificateException {
List<WSHandlerResult> handlerResults = (List<WSHandlerResult>) messageContext.getProperty(WSHandlerConstants.RECV_RESULTS);
WSHandlerResult rResult = handlerResults.get(0);
List<WSSecurityEngineResult> results = rResult.getResults();
WSSecurityEngineResult actionResult = WSSecurityUtil.fetchActionResult(results, WSConstants.SIGN);
X509Certificate returnCert = null;
if (actionResult != null) {
returnCert = (X509Certificate) actionResult.get(WSSecurityEngineResult.TAG_X509_CERTIFICATE);
}
// do stuff with the certificate and return values
}
После некоторых исследований я обнаружил, что Spring сохраняет в MessageContext результаты, обработанные в валидации, которые содержат сертификат (см. Wss4jSecurityInterceptor#updateContextWithResults
).
messageContext
?