Java笔记-解决WebServiceTemplate中No subject alternative names matching IP address xxx

Posted IT1995

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java笔记-解决WebServiceTemplate中No subject alternative names matching IP address xxx相关的知识,希望对你有一定的参考价值。

主要的原因是证书有问题,就是程序的IP地址和证书的IP地址不匹配。

这里是客户端报错,使用的是spring-ws

对应的Maven如下;

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-security</artifactId>
            <version>3.0.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>3.0.8.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-support</artifactId>
            <version>3.0.8.RELEASE</version>
        </dependency>

生成的bean

    @Bean
    HttpsUrlConnectionMessageSender messageSender(
            KeyStoreFactoryBean keyStore,
            TrustManagersFactoryBean trustManagers
    ) throws Exception {
        HttpsUrlConnectionMessageSender sender = new HttpsUrlConnectionMessageSender();

        KeyManagersFactoryBean keyManagersFactoryBean = new KeyManagersFactoryBean();
        keyManagersFactoryBean.setKeyStore(keyStore.getObject());
        keyManagersFactoryBean.setPassword(KEYSTORE_PASSWORD);
        keyManagersFactoryBean.afterPropertiesSet();
        sender.setKeyManagers(keyManagersFactoryBean.getObject());
        sender.setTrustManagers(trustManagers.getObject());
        sender.setHostnameVerifier(new TrustAnyHostnameVerifier());

        return sender;
    }

    
    public class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

这里调用代码:

    @Autowired
    HttpsUrlConnectionMessageSender sender;


    public XXXResponse clear(String para){

        XXXRequest request = new XXXRequest();
        request.setValue(para);
        getWebServiceTemplate().setMessageSender(sender);

        JAXBElement<XXXtResponse> o = (JAXBElement<XXXtResponse>)
                getWebServiceTemplate().marshalSendAndReceive("https://xxx.xxx.xxx.xxx:xxx/ws/xxxx", request);
        return o.getValue();
    }

这里要注意,要将生成的bean放到getWebserviceTemplate()的Sender中才能,不然不会生效的。

以上是关于Java笔记-解决WebServiceTemplate中No subject alternative names matching IP address xxx的主要内容,如果未能解决你的问题,请参考以下文章

号称可以解决 90% 以上的 Java 面试神级笔记!

Java学习笔记45(多线程二:安全问题以及解决原理)

Java复习笔记8--内部类

Java学习笔记47(JDBCSQL注入攻击原理以及解决)

11.8-全栈Java笔记:死锁及解决方案

JAVA笔记140-使用this语句解决构造器重载相互调用问题