如何在WebServiceTemplate中检索有关证书的信息?

时间:2018-11-02 15:33:37

标签: java spring spring-security spring-ws webservicetemplate

我想从SOAP通信中的客户端证书中检索公用名(CN)属性。我正在使用Spring WebServiceTemplate 创建我的Web服务端点。我已经在example之后实现了WS相互认证。

是否有任何解决方案可以通过WebServiceTemplate或其他库从客户端请求中获取证书详细信息

1 个答案:

答案 0 :(得分:0)

幸运的是,我设法弄清楚了! Spring WS提供了一种非常方便的方法来检索X509证书。

通常,您有一个这样的端点:

@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";

    ...

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        //method body here
        return response;
    }
}

但是,Spring允许使用注释为 @PayloadRoot 的方法添加其他参数。它可以是 MessageContext 实例。

public GetCountryResponse getCountry(@RequestPayload MessageContext context, @RequestPayload GetCountryRequest request)`

然后您将可以按以下方式获取wsse:Security标头:

WebServiceMessage webServiceMessageRequest = context.getRequest(); 
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) webServiceMessageRequest;
SOAPMessage doc = saajSoapMessage.getSaajMessage();
Element elem = WSSecurityUtil.getSecurityHeader(doc.getSOAPPart(), "");

现在获得BinarySecurityToken标签的正确内容:

String binarySecurityToken = elem.getElementsByTagName("BinarySecurityToken").item(0).getTextContent(); 

最后,您应该通过传递binarySecurityToken作为其构造函数参数来重新创建X509Certificate。稍后,您可以通过多种不同方式(例如,通过LDAP utlis)提取CN。