首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在WebServiceTemplate中检索有关证书的信息?

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

Stack Overflow用户
提问于 2018-11-02 15:33:36
回答 2查看 421关注 0票数 0

我希望在SOAP通信中从客户端证书中检索Common (CN)属性。我正在使用SpringWeb服务端点创建webservice端点。我已经在示例之后实现了WS相互身份验证。

是否有通过或其他库从客户端请求获取证书详细信息的解决方案?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-07 19:28:11

幸运的是,我已经想出了办法!Spring提供了非常方便的检索X509Certificate的方法。

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

代码语言:javascript
复制
@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实例。

代码语言:javascript
复制
public GetCountryResponse getCountry(@RequestPayload MessageContext context, @RequestPayload GetCountryRequest request)`

然后,您将能够获得如下所示的wsse:Security头:

代码语言:javascript
复制
WebServiceMessage webServiceMessageRequest = context.getRequest(); 
SaajSoapMessage saajSoapMessage = (SaajSoapMessage) webServiceMessageRequest;
SOAPMessage doc = saajSoapMessage.getSaajMessage();
Element elem = WSSecurityUtil.getSecurityHeader(doc.getSOAPPart(), "");

现在获取BinarySecurityToken标记的正确内容:

代码语言:javascript
复制
String binarySecurityToken = elem.getElementsByTagName("BinarySecurityToken").item(0).getTextContent(); 

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

票数 0
EN

Stack Overflow用户

发布于 2022-01-31 06:24:04

还有另一种方法。

  1. 使用这个主体创建AbstractSoapInterceptor:
代码语言:javascript
复制
    private final static QName SECURITY_QNAME = new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security", "");
    private static CertificateFactory certFactory;
    public xxx() throws CertificateException {
        super(Phase.PRE_PROTOCOL);
        certFactory = CertificateFactory.getInstance("X.509");
    }
    @SneakyThrows
    @Override
    public void handleMessage(SoapMessage message) throws Fault {
        SoapHeader header = (SoapHeader) message.getHeader(SECURITY_QNAME);
        Node binarySignatureTag = ((Element) header.getObject()).getFirstChild();
        BinarySecurity token = new X509Security((Element) binarySignatureTag, new BSPEnforcer());
        InputStream in = new ByteArrayInputStream(token.getToken());
        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);
    }
  1. 在端点配置中注册它:
代码语言:javascript
复制
    @Bean
    public Endpoint endpoint() throws CertificateException {
        EndpointImpl endpoint = new EndpointImpl(springBus(), xxxPortType());
        endpoint.setServiceName(xxxService().getServiceName());
        endpoint.publish("/xxxx");
        endpoint.getInInterceptors().add(new xxx());
        return endpoint;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53121554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档