信任所有人的Java类,用于将文件发送到https Web服务 [英] java class to trust all for sending file to https web service

查看:94
本文介绍了信任所有人的Java类,用于将文件发送到https Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写自己的类来告诉m子,与服务(wsdl)的https连接已得到验证.我已经完成了m子项目,但是最后一部分丢失了,它以特定的URL发送文件.

我想要实现的目标:

  1. 建立连接并将xml发送到目标url

  2. 读取同样在xml中的响应

服务器使用带有自签名证书的安全性.到目前为止,我所做的是从该链接获取证书并将其导入.jks.然后,我可能遵循了所有教程"如何使用https连接器以m子方式连接到服务器,但在我的情况下没有任何作用.

我认为最好的办法是,如果有人可以帮助我创建Java类以绕过键检查并返回true(经验证).像这样:

URL url = new URL("https://www.google.com");
HttpsURLConnection conn= (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String arg0, SSLSession arg1) {
        return true;
    }
});

如何在m子中做到这一点?我希望它会类似于. /p>

我正在使用当前的m子版本(3.5.0)

谢谢!

我的配置:

<https:connector name="HttpsConnector" cookieSpec="netscape" validateConnections="true" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="HTTP\HTTPS" dynamicNotification="true" >
    <https:tls-server path="${keystore.path}" storePassword="${keystore.pass}" />
</https:connector>

<sub-flow name="toSOAP" doc:name="toSOAP">
    <cxf:proxy-client payload="body" doc:name="SOAP" enableMuleSoapHeaders="false">
        <cxf:outInterceptors>
            <spring:ref bean="WSS4JOutInterceptorBean"/>
        </cxf:outInterceptors>
    </cxf:proxy-client>
    <https:outbound-endpoint exchange-pattern="one-way" host="${pref.host}" port="${pref.port}" path="${pref.path}" method="POST" connector-ref="HttpsConnector" doc:name="HTTP"/>
</sub-flow>

解决方案

对我有用的是在HTTPS连接器上设置TrustManagerFactory.这是我的方法.

首先,创建一个密钥库,其中包含要信任的SSL服务器的证书.您可以使用JDK(这是一个示例).

然后,创建一个FactoryBean,该工厂为您提供一个具有JKS密钥库和密码的TrustManagerFactory.这是我使用Spring资源制作的,因此我可以从类路径或文件系统中提供密钥库:

public class ExampleFactoryBean implements FactoryBean<TrustManagerFactory> {

    private Resource keystore;
    private String password;

    @Override
    public TrustManagerFactory getObject() throws Exception {
            KeyStore truststore = KeyStore.getInstance("JKS");
            truststore.load(keystore.getInputStream(), password.toCharArray());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(truststore);
            return tmf;
    }

    @Override
    public Class<?> getObjectType() {
        return TrustManagerFactory.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setKeystore(Resource keystore) {
        this.keystore = keystore;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

最后,像下面这样在HTTP连接器上设置TrustManagerFactory:

<https:connector name="myHttpsConnector">
    <spring:property name="trustManagerFactory">
        <spring:bean class="com.mycompany.ssl.ExampleFactoryBean">
            <spring:property name="keystore" value="classpath:mykeystore.keystore" />
            <spring:property name="password" value="mypassword" />
        </spring:bean>
    </spring:property>
</https:connector>

I need to write my own class to tell mule that https connection to service (wsdl) is verified. I already have mule project nearly finnished but last piece is missing, sending file at specific url.

What I want to achieve:

  1. establish connection and send xml to target url

  2. read response that is also in xml

Server uses security with self signed certificate. What I did so far was that I got cert from that link and imported it in .jks. Then I followed probably all "tutorials" how to connect to server in mule with https connector but nothing worked in my case.

I think that the best thing would be if someone can help me create java class to bypass key checking and return true (as verified). Something like:

URL url = new URL("https://www.google.com");
HttpsURLConnection conn= (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String arg0, SSLSession arg1) {
        return true;
    }
});

How can I do that in mule? I expect that it would be something like this.

I am using current mule version (3.5.0)

Thank you!

EDIT:

My configuration:

<https:connector name="HttpsConnector" cookieSpec="netscape" validateConnections="true" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="HTTP\HTTPS" dynamicNotification="true" >
    <https:tls-server path="${keystore.path}" storePassword="${keystore.pass}" />
</https:connector>

<sub-flow name="toSOAP" doc:name="toSOAP">
    <cxf:proxy-client payload="body" doc:name="SOAP" enableMuleSoapHeaders="false">
        <cxf:outInterceptors>
            <spring:ref bean="WSS4JOutInterceptorBean"/>
        </cxf:outInterceptors>
    </cxf:proxy-client>
    <https:outbound-endpoint exchange-pattern="one-way" host="${pref.host}" port="${pref.port}" path="${pref.path}" method="POST" connector-ref="HttpsConnector" doc:name="HTTP"/>
</sub-flow>

解决方案

What worked for me is to set the TrustManagerFactory on the HTTPS connector. Here's how I did it.

First, create a keystore that contains the certificate of the SSL server you want to trust. You can create the keystore using the tools included with the JDK (here's an example).

Then, create a FactoryBean that gives you a TrustManagerFactory given a JKS keystore and password. Here's one I made that uses a Spring resource, so that I can provide the keystore from the classpath or from the filesystem:

public class ExampleFactoryBean implements FactoryBean<TrustManagerFactory> {

    private Resource keystore;
    private String password;

    @Override
    public TrustManagerFactory getObject() throws Exception {
            KeyStore truststore = KeyStore.getInstance("JKS");
            truststore.load(keystore.getInputStream(), password.toCharArray());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(truststore);
            return tmf;
    }

    @Override
    public Class<?> getObjectType() {
        return TrustManagerFactory.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void setKeystore(Resource keystore) {
        this.keystore = keystore;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Finally, set the TrustManagerFactory on the HTTP connector like so:

<https:connector name="myHttpsConnector">
    <spring:property name="trustManagerFactory">
        <spring:bean class="com.mycompany.ssl.ExampleFactoryBean">
            <spring:property name="keystore" value="classpath:mykeystore.keystore" />
            <spring:property name="password" value="mypassword" />
        </spring:bean>
    </spring:property>
</https:connector>

这篇关于信任所有人的Java类,用于将文件发送到https Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆