WebSphere应用服务器中https 请求协议的相关注意事项(服务器使用代理上Internet)
最近遇到个需求需要web服务器应用通过https方式请求外部Internet服务器的接口,一开始本地测试时使用以下代码:
String businessCode = "SH30580";
GenerateXml xml = new GenerateXml();
String xmlContent = xml.writeXmlString(businessCode);
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, new TrustManager[] {new X509TrustForMSL()},new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// 创建URL对象
URL myURL = new URL("https://211.144.221.138/mslws/Services/rsa/RsaWebService.svc/security/validateagencyqualification");
System.setProperty("java.protocol.handler.pkgs", "javax.net.ssl");
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.38.194.30", 8080));//因为服务器使用代理上Internet,设置代理的ip和端口号
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setRequestProperty( "Proxy-Authorization", "Basic " + new sun.misc.BASE64Encoder().encode("用户名:密码".getBytes()) );//设置用户名和密码
httpsConn.setSSLSocketFactory(ssf);
// 以POST方式提交
httpsConn.setRequestMethod("POST");
// 设置连接输出
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
// Post 请求不能使用缓存
httpsConn.setUseCaches(false);
httpsConn.setRequestProperty("Content-Type","application/xml");
// httpsConn.connect();
//获取输出流
OutputStream os = httpsConn.getOutputStream();
// // 设置输出流字符集
os.write(xmlContent.getBytes("UTF-8"));
os.flush();
os.close();
// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
public class X509TrustForMSL implements X509TrustManager {
/*
* The default X509TrustManager returned by SunX509. We'll delegate
* decisions to it, and fall back to the logic in this class if the
* default X509TrustManager doesn't trust it.
*/
X509TrustManager sunJSSEX509TrustManager;
X509TrustForMSL() throws Exception {
// create a "default" JSSE X509TrustManager.
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("D:\\jssecacerts"),
"changeit".toCharArray());
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("SunX509");//, "SunJSSE"
tmf.init(ks);
TrustManager tms [] = tmf.getTrustManagers();
/*
* Iterate over the returned trustmanagers, look
* for an instance of X509TrustManager. If found,
* use that as our "default" trust manager.
*/
for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
sunJSSEX509TrustManager = (X509TrustManager) tms[i];
return;
}
}
/*
* Find some other way to initialize, or else we have to fail the
* constructor.
*/
throw new Exception("Couldn't initialize");
}
/*
* Delegate to the default trust manager.
*/
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkClientTrusted(chain, authType);
} catch (CertificateException excep) {
// do any special handling here, or rethrow exception.
}
}
/*
* Delegate to the default trust manager.
*/
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
try {
sunJSSEX509TrustManager.checkServerTrusted(chain, authType);
} catch (CertificateException excep) {
/*
* Possibly pop up a dialog box asking whether to trust the
* cert chain.
*/
}
}
/*
* Merely pass this through.
*/
public X509Certificate[] getAcceptedIssuers() {
return sunJSSEX509TrustManager.getAcceptedIssuers();
}
}
Eclipse测试没问题,但是发布到WebSphere中报错,说不支持SunJSSE,查了半天,原来IBM不支持sun公司的jdk中的jsse.jar包,网上说有自己添加的,也有说把证书加入哪里的,或许因为本人愚笨,搞了大半天,p用没有,最后终于悟得以下方法:
public static void main(String args[]) throws Exception{
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, new TrustManager[] {new TrustAnyTrustManager()},new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
// 创建URL对象
URL myURL = new URL("https://211.144.221.138/mslws/Services/rsa/RsaWebService.svc/security/validateagencyqualification");
String businessCode = "SH30580";
GenerateXml xml = new GenerateXml();
String xmlContent = xml.writeXmlString(businessCode);
// 创建HttpsURLConnection对象,并设置其SSLSocketFactory对象
Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("10.38.194.30", 8080));
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setRequestProperty( "Proxy-Authorization", "Basic " + new sun.misc.BASE64Encoder().encode("dajiang:Abcd1234".getBytes()) );
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new TrustAnyHostnameVerifier());
// 以POST方式提交
httpsConn.setRequestMethod("POST");
// 设置连接输出
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);
// Post 请求不能使用缓存
httpsConn.setUseCaches(false);
httpsConn.setRequestProperty("Content-Type","application/xml");
// httpsConn.connect();
//获取输出流
OutputStream os = httpsConn.getOutputStream();
// // 设置输出流字符集
os.write(xmlContent.getBytes("UTF-8"));
os.flush();
os.close();
// 取得该连接的输入流,以读取响应内容
InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());
// 读取服务器的响应内容并显示
int respInt = insr.read();
while (respInt != -1) {
System.out.print((char) respInt);
respInt = insr.read();
}
}
/*
* 用于连接https使用
*/
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[] {};
}
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
按这种方法,终于链接成功,大石头落地~!
WebSphere应用服务器中https 请求协议的相关注意事项(服务器使用代理上Internet)的更多相关文章
- Javaweb中的请求路径的相关总结
重定向和转发相对路径和绝对路径问题 注意:转发和重定向的URLString前有加 / 为绝对路径 反之为相对路径 1.假设通过表单请求指定的Url资源 action="LoginServ ...
- nodejs中https请求失败,无报错
今天群里一位同学在做练习的时候,采用https例子: // curl -k https://localhost:8000/ const https = require('https'); const ...
- node中https请求 | 实现https的请求,获取图片,然后转成base64字节码
get请求 下面实现https的请求,获取图片,然后转成base64字节码 this.checkCodeUrl = 'https://www.test.com/kaptcha.jsp'; var ht ...
- python中Requests模块中https请求在设置为忽略有效性验证,屏蔽告警信息的方式
增加下面的就ok了from requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.ur ...
- WebSphere应用服务器内存泄漏探测与诊断工具选择最佳实践
内存泄漏是比较常见的一种应用程序性能问题,一旦发生,则系统的可用内存和性能持续下降:最终将导致内存不足(OutOfMemory),系统彻底宕掉,不能响应任何请求,其危害相当严重.同时,Java堆(He ...
- 项目通过https访问的tomcat相关配置
开发项目已经完成,那么就是要部署项目到服务器上面.我最近把刚完成的项目部署到服务器上面,内网通过http协议进行访问一切正常,但是测试外网通过https协议进行访问的时候就出现了一些js文档找不到的b ...
- Charles如何抓取https请求-移动端+PC端
Charles安装完成,默认只能抓取到http请求,如果查看https请求,会显示unkonw或其它之类的响应.所以需要先进行一些配置,才能抓取到完整的https请求信息.下面针对PC端和手机端抓包的 ...
- HttpClient中post请求http、https示例
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...
- C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)
这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全部采用手工操作工作量大而且繁琐,因此周公决定利用C#来实现.在很多地方都需要验证用户身份才可 ...
随机推荐
- Intellij Idea 13 vmoptions (Mac版本)
-ea -server -Xms1g -Xmx1g -Xss16m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DoEscapeAnalysis -XX:+ ...
- oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断
oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...
- 介绍TableView非常不错的一篇文章
原文:http://blog.csdn.net/fanxiaochuan/article/details/11332775 介绍TableView非常不错的一篇文章: http://www.cocoa ...
- 解决ubuntu下的文本编辑器gedit的乱码问题
缺省配置下,用 Ubuntu 的文本编辑器(Gedit)打开GB18030(繁体中文用户请将这里的出现的GB18030替换成BIG5或BIG5-HKSCS)类型的中文编码文本文件时,将会出现乱码. 出 ...
- ajax jsonp 原理 以及对数据的处理
ajax请求 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xml ...
- 在Thinkphp3.2 中使用PHPMailer 发送邮件
phpmailer发送邮件是php开发者首选的一个邮件发送插件了,下面我来介绍怎么集成phpmailer到thinkphp框架了,有需要了解的朋友可参考. phpmailer发送邮件功能很强大,今天真 ...
- objective-c基础教程
command+shift+c foundation框架包含的头文件 /system/library/frameworkks/foundation.framework/headers/
- github的访问变慢了
以下个人观点:把操作系统的自主研究还有处理器自主研究列入重点,还有互联网上的种种动作,我发现里面似乎揭示了某些迹象,科研真的不应该以牺牲大部分人的河法全益为代价甚至目的.当某一天win不可能出现在出厂 ...
- 董的博客 hadoop
董的博客 https://issues.apache.org/jira/browse/MAPREDUCE 很重要,把MAPREDUCE改为YARN即可 直接下载patch即可 http://horto ...
- 基于LeanCloud云引擎的Web全栈方案
LeanEngine-Full-Stack The FULL STACK DEVELOPER 复杂的项目, 协作分工, 自动化流程,代码组织结构,框架选择,国际化方案等 Generator 或者See ...