java https 文件,信任所有将文件发送到https Web服务的Java类

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:

establish connection and send xml to target url

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:

解决方案

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 {

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值