導航:首頁 > 證書轉讓 > java請求https證書

java請求https證書

發布時間:2021-09-25 17:55:51

㈠ java https 證書 java 實現https請求

JSSE是一個SSL和TLS的純Java實現,通過JSSE可以很容易地編程實現對HTTPS站點的訪問。但是,如果該站點的證書未經權威機構的驗證,JSSE將拒絕信任該證書從而不能訪問HTTPS站點。建議到權威CA機構去申請一受信任的免費https證書來使用,比如wosign免費多域名https證書等。

㈡ 跪求大神講解一下java模擬https請求時如何使用p12證書!!!

你有用過 KeyManager.init (...)? 和抄 TrustManager.init(...) ?

想要在連接建立過程上互動式的彈出確認對話框來的話需要我們自己提供一個 KeyManager 和 TrustManager 的實現類,這有點復雜,你可以看一個 Sun 的 X509KeyManager 是怎麼做的,默認地情況下它是從自動搜索匹配的 subject ,我們需要用自己提供的方式彈出確認的過程還不是全自動,另外一個賬戶可能有多個數字證書,比如支付寶我們就有多個簽發時間不一樣的數字證書,在連接建立時 IE 會提示我們選擇其中的一個來使用,銀行的 U 盾在安裝多張數字證書時也會提示我們選擇其中一個對應到你正在使用的銀行卡號的那張證書。

㈢ 用java做一個httpClient 發送https 的get請求,需要證書驗證的那種,求大神指點一下!

你那個 SSLSocketFactory(ks) 是自己的類?

你有用過 KeyManager.init (...)? 和 TrustManager.init(...) ?

想要在連接建立過程上互動式的彈出確認對話框來的話需要我們自己提供一個 KeyManager 和 TrustManager 的實現類,這有點復雜,你可以看一個 Sun 的 X509KeyManager 是怎麼做的,默認地情況下它是從自動搜索匹配的 subject ,我們需要用自己提供的方式彈出確認的過程還不是全自動,另外一個賬戶可能有多個數字證書,比如支付寶我們就有多個簽發時間不一樣的數字證書,在連接建立時 IE 會提示我們選擇其中的一個來使用,銀行的 U 盾在安裝多張數字證書時也會提示我們選擇其中一個對應到你正在使用的銀行卡號的那張證書。

㈣ 如何配置Java HTTPS CA證書

這個很復雜(反正我配置了幾次都失敗了,而且本地(windows)就算配置成功了,線上又水土不服(Linux)),所以,我一般都是用httpclient封裝一個工具類,然後繞過https證書驗證,直接發送https請求,至於怎麼封裝,網上例子很多

㈤ Java的HttpClient如何去支持無證書訪問https

項目里需要訪問其他介面,通過http/https協議。我們一般是用HttpClient類來實現具體的http/https協議介面的調用。

// Init a HttpClient
HttpClient client = new HttpClient();
String url=http://www.xxx.com/xxx;

// Init a HttpMethod
HttpMethod get = new GetMethod(url);
get.setDoAuthentication(true);
get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(1, false));

// Call http interface
try {
client.executeMethod(get);

// Handle the response from http interface
InputStream in = get.getResponseBodyAsStream();
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
} finally {
// Release the http connection
get.releaseConnection();
}

以上代碼在通過普通的http協議是沒有問題的,但如果是https協議的話,就會有證書文件的要求了。一般情況下,是這樣去做的。

// Init a HttpClient
HttpClient client = new HttpClient();
String url=https://www.xxx.com/xxx;

if (url.startsWith("https:")) {
System.setProperty("javax.net.ssl.trustStore", "/.sis.cer");
System.setProperty("javax.net.ssl.trustStorePassword", "public");
}

於是,這里就需要事先生成一個.sis.cer的文件,生成這個文件的方法一般是先通過瀏覽器訪問https://,導出證書文件,再用JAVA keytool command 生成證書

# $JAVA_HOME/bin/keytool -import -file sis.cer -keystore .sis.cer

但這樣做,一比較麻煩,二來證書也有有效期,過了有效期之後,又需要重新生成一次證書。如果能夠避開生成證書文件的方式來使用https的話,就比較好了。

還好,在最近的項目里,我們終於找到了方法。

// Init a HttpClient
HttpClient client = new HttpClient();
String url=https://www.xxx.com/xxx;

if (url.startsWith("https:")) {
this.supportSSL(url, client);
}

用到了supportSSL(url, client)這個方法,看看這個方法是如何實現的。

private void supportSSL(String url, HttpClient client) {
if(StringUtils.isBlank(url)) {
return;
}
String siteUrl = StringUtils.lowerCase(url);
if (!(siteUrl.startsWith("https"))) {
return;
}

try {
setSSLProtocol(siteUrl, client);
} catch (Exception e) {
logger.error("setProtocol error ", e);
}
Security.setProperty( "ssl.SocketFactory.provider",
"com.tool.util.DummySSLSocketFactory");
}

private static void setSSLProtocol(String strUrl, HttpClient client) throws Exception {

URL url = new URL(strUrl);
String host = url.getHost();
int port = url.getPort();

if (port <= 0) {
port = 443;
}
ProtocolSocketFactory factory = new SSLSocketFactory();
Protocol authhttps = new Protocol("https", factory, port);
Protocol.registerProtocol("https", authhttps);
// set https protocol
client.getHostConfiguration().setHost(host, port, authhttps);
}

在supportSSL方法里,調用了Security.setProperty( "ssl.SocketFactory.provider",
"com.tool.util.DummySSLSocketFactory");
那麼這個com.tool.util.DummySSLSocketFactory是這樣的:
訪問https 資源時,讓httpclient接受所有ssl證書,在weblogic等容器中很有用
代碼如下:
1. import java.io.IOException;
2. import java.net.InetAddress;
3. import java.net.InetSocketAddress;
4. import java.net.Socket;
5. import java.net.SocketAddress;
6. import java.net.UnknownHostException;
7. import java.security.KeyManagementException;
8. import java.security.NoSuchAlgorithmException;
9. import java.security.cert.CertificateException;
10. import java.security.cert.X509Certificate;
11.
12. import javax.net.SocketFactory;
13. import javax.net.ssl.SSLContext;
14. import javax.net.ssl.TrustManager;
15. import javax.net.ssl.X509TrustManager;
16.
17. import org.apache.commons.httpclient.ConnectTimeoutException;
18. import org.apache.commons.httpclient.params.HttpConnectionParams;
19. import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
20.
21. public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
22. static{
23. System.out.println(">>>>in MySecureProtocolSocketFactory>>");
24. }
25. private SSLContext sslcontext = null;
26.
27. private SSLContext createSSLContext() {
28. SSLContext sslcontext=null;
29. try {
30. sslcontext = SSLContext.getInstance("SSL");
31. sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
32. } catch (NoSuchAlgorithmException e) {
33. e.printStackTrace();
34. } catch (KeyManagementException e) {
35. e.printStackTrace();
36. }
37. return sslcontext;
38. }
39.
40. private SSLContext getSSLContext() {
41. if (this.sslcontext == null) {
42. this.sslcontext = createSSLContext();
43. }
44. return this.sslcontext;
45. }
46.
47. public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
48. throws IOException, UnknownHostException {
49. return getSSLContext().getSocketFactory().createSocket(
50. socket,
51. host,
52. port,
53. autoClose
54. );
55. }
56.
57. public Socket createSocket(String host, int port) throws IOException,
58. UnknownHostException {
59. return getSSLContext().getSocketFactory().createSocket(
60. host,
61. port
62. );
63. }
64.
65.
66. public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
67. throws IOException, UnknownHostException {
68. return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
69. }
70.
71. public Socket createSocket(String host, int port, InetAddress localAddress,
72. int localPort, HttpConnectionParams params) throws IOException,
73. UnknownHostException, ConnectTimeoutException {
74. if (params == null) {
75. throw new IllegalArgumentException("Parameters may not be null");
76. }
77. int timeout = params.getConnectionTimeout();
78. SocketFactory socketfactory = getSSLContext().getSocketFactory();
79. if (timeout == 0) {
80. return socketfactory.createSocket(host, port, localAddress, localPort);
81. } else {
82. Socket socket = socketfactory.createSocket();
83. SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
84. SocketAddress remoteaddr = new InetSocketAddress(host, port);
85. socket.bind(localaddr);
86. socket.connect(remoteaddr, timeout);
87. return socket;
88. }
89. }
90.
91. //自定義私有類
92. private static class TrustAnyTrustManager implements X509TrustManager {
93.
94. public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
95. }
96.
97. public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
98. }
99.
100. public X509Certificate[] getAcceptedIssuers() {
101. return new X509Certificate[]{};
102. }
103. }
104.
105. }

public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
static{
System.out.println(">>>>in MySecureProtocolSocketFactory>>");
}
private SSLContext sslcontext = null;

private SSLContext createSSLContext() {
SSLContext sslcontext=null;
try {
sslcontext = SSLContext.getInstance("SSL");
sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslcontext;
}

private SSLContext getSSLContext() {
if (this.sslcontext == null) {
this.sslcontext = createSSLContext();
}
return this.sslcontext;
}

public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException, UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
socket,
host,
port,
autoClose
);
}

public Socket createSocket(String host, int port) throws IOException,
UnknownHostException {
return getSSLContext().getSocketFactory().createSocket(
host,
port

然後按如下方式使用HttpClient
Protocol myhttps = new Protocol("https", new MySecureProtocolSocketFactory (), 443);
Protocol.registerProtocol("https", myhttps);
HttpClient httpclient=new HttpClient();

㈥ java客戶端怎麼訪問帶有pfx格式證書的https網站(伺服器)呢,

使用 訪問https地址。
以下是導入JKS證書的方式,可以參考。

String keystorefile = "file";
String keystorepw = "password";
String keypw = "password";

KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load( new FileInputStream(keystorefile), keystorepw.toCharArray());
KeyManagerFactory keymanagerfactory = KeyManagerFactory.getInstance("SunX509");
keymanagerfactory.init(keystore, keypw.toCharArray());
KeyManager akeymanager[] = keymanagerfactory.getKeyManagers();
TrustManagerFactory trustmanagerfactory = TrustManagerFactory.getInstance("SunX509");
trustmanagerfactory.init(keystore);
TrustManager atrustmanager[] = trustmanagerfactory.getTrustManagers();
sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(akeymanager, atrustmanager, null);
sslSocketFactory = sslcontext.getSocketFactory();

String url = "asdfdf";
URL testURL = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection) testURL.openConnection();
if (urlConnection instanceof HttpsURLConnection) {
HttpsURLConnection conn = (HttpsURLConnection) urlConnection;
conn.setSSLSocketFactory(sslSocketFactory);
}

㈦ 如何使用JAVA請求HTTPS

1.寫http請求方法
[java] view plain

//處理http請求 requestUrl為請求地址 requestMethod請求方式,值為"GET"或"POST"
public static String httpRequest(String requestUrl,String requestMethod,String outputStr){
StringBuffer buffer=null;
try{
URL url=new URL(requestUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod(requestMethod);
conn.connect();
//往伺服器端寫內容 也就是發起http請求需要帶的參數
if(null!=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
//讀取伺服器端返回的內容
InputStream is=conn.getInputStream();
InputStreamReader isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
buffer=new StringBuffer();
String line=null;
while((line=br.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
2.測試。
[java] view plain

public static void main(String[] args){
String s=httpRequest("","GET",null);
System.out.println(s);
}
輸出結果為的源代碼,說明請求成功。
註:1).第一個參數url需要寫全地址,即前邊的http必須寫上,不能只寫這樣的。

2).第二個參數是請求方式,一般介面調用會給出URL和請求方式說明。
3).第三個參數是我們在發起請求的時候傳遞參數到所要請求的伺服器,要傳遞的參數也要看介面文檔確定格式,一般是封裝成json或xml.
4).返回內容是String類,但是一般是有格式的json或者xml。
二:發起https請求。
1.https是對鏈接加了安全證書SSL的,如果伺服器中沒有相關鏈接的SSL證書,它就不能夠信任那個鏈接,也就不會訪問到了。所以我們第一步是自定義一個信任管理器。自要實現自帶的X509TrustManager介面就可以了。
[java] view plain

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
}
註:1)需要的包都是java自帶的,所以不用引入額外的包。
2.)可以看到裡面的方法都是空的,當方法為空是默認為所有的鏈接都為安全,也就是所有的鏈接都能夠訪問到。當然這樣有一定的安全風險,可以根據實際需要寫入內容。

2.編寫https請求方法。
[java] view plain

/*
* 處理https GET/POST請求
* 請求地址、請求方法、參數
* */
public static String httpsRequest(String requestUrl,String requestMethod,String outputStr){
StringBuffer buffer=null;
try{
//創建SSLContext
SSLContext sslContext=SSLContext.getInstance("SSL");
TrustManager[] tm={new MyX509TrustManager()};
//初始化
sslContext.init(null, tm, new java.security.SecureRandom());;
//獲取SSLSocketFactory對象
SSLSocketFactory ssf=sslContext.getSocketFactory();
URL url=new URL(requestUrl);
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(requestMethod);
//設置當前實例使用的SSLSoctetFactory
conn.setSSLSocketFactory(ssf);
conn.connect();
//往伺服器端寫內容
if(null!=outputStr){
OutputStream os=conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}
//讀取伺服器端返回的內容
InputStream is=conn.getInputStream();
InputStreamReader isr=new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
buffer=new StringBuffer();
String line=null;
while((line=br.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}
return buffer.toString();
}
可見和http訪問的方法類似,只是多了SSL的相關處理。
3.測試。先用http請求的方法訪問,再用https的請求方法訪問,進行對比。

http訪問:
[java] view plain

public static void main(String[] args){
String s=httpRequest("","GET",null);
System.out.println(s);
}
結果為:

https訪問:

[java] view plain

public static void main(String[] args){
String s=httpsRequest("","GET",null);
System.out.println(s);
}
結果為:

可見https的鏈接一定要進行SSL的驗證或者過濾之後才能夠訪問。

三:https的另一種訪問方式——導入服務端的安全證書。
1.下載需要訪問的鏈接所需要的安全證書。 以這個網址為例。
1)在瀏覽器上訪問。

2)點擊上圖的那個打了×的鎖查看證書。

3)選擇復制到文件進行導出,我們把它導入到java項目所使用的jre的lib文件下的security文件夾中去,我的是這個路徑。D:\Program Files (x86)\Java\jre8\lib\security

註:中間需要選導出格式,就選默認的就行,還需要命名,我命名的是12306.

2.打開cmd,進入到java項目所使用的jre的lib文件下的security目錄。
3.在命令行輸入 Keytool -import -alias 12306 -file 12306.cer -keystore cacerts
4.回車後會讓輸入口令,一般默認是changeit,輸入時不顯示,輸入完直接按回車,會讓確認是否信任該證書,輸入y,就會提示導入成功。

5.導入成功後就能像請求http一樣請求https了。

測試:
[java] view plain

public static void main(String[] args){
String s=httpRequest("","GET",null);
System.out.println(s);
}
結果:
現在就可以用http的方法請求https了。
註:有時候這一步還是會出錯,那可能是jre的版本不對,我們右鍵run as——run configurations,選擇證書所在的jre之後再運行。

㈧ https怎麼用java進行訪問

沒有證書認證的,如果想訪問支付寶等,需要配置一個訪問的公鑰
public class HttpClient {

private String charset = "UTF-8";

private boolean safe = false;

private String url;

Map<String, String> headers = null;

public HttpClient(String url) {
this.url = url;

...

public String post(String httpStr) throws IOException {
if (this.safe) {
return this.sendhttpsReq("POST", "", headers);

.....
while ((byteread = in.read(buf)) != -1) {
result.append(buf, 0, byteread);
}
....
conn.setRequestMethod(method);
conn.setDoOutput(true);

}
});
conn.setRequestProperty("Content-Type", "text/html");
.....
}
StringBuilder result = new StringBuilder(100);

} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

private static c

㈨ java系統怎樣配置一個接收https請求的web服務

server.xml里 有一個https的埠可以配置,訪問那個埠就是了

㈩ 用java代碼發生請求https,發生異常!

因為你的異常不是能必定復現的,有時發生,又有時不發生,那麼只能初步的認為是網路連接不穩定造成的
如果想徹底查清問題所在,只能通過網路抓包工具,在復現問題時查看網路通信包,看看到底是網路連接報的錯誤還是代碼邏輯報的錯誤

另外,Remote host closed connection ring handshake這個異常在客戶端/服務端的TLS版本不一致時也會拋出,你可以嘗試在發送請求前在代碼中設置TLS版本和服務端一致後在發送請求,相關問題和解決方案stackoverflow上能查到很多

閱讀全文

與java請求https證書相關的資料

熱點內容
京韻花園糾紛 瀏覽:895
衛生服務站公共衛生考核方案 瀏覽:62
快遞時效投訴 瀏覽:782
世紀創造絕緣有限公司 瀏覽:600
聚投訴珍愛網 瀏覽:47
公共衛生服務協議書2017 瀏覽:805
改革工作成果匯報 瀏覽:49
醫療糾紛管理倫理的主要要求不包括 瀏覽:959
工業光魔創造不可能720p 瀏覽:243
君主立憲制是法國大革命的成果 瀏覽:13
王成果青島科技大學 瀏覽:519
護理品管圈成果匯報書 瀏覽:875
使用權獲取途徑 瀏覽:759
怎麼投訴奧迪4s店 瀏覽:31
美術教師校本研修成果 瀏覽:740
股權轉讓合同模板 瀏覽:638
知識產權部門重點的工作計劃範文 瀏覽:826
用地批准書能證明土地的使用權權嗎 瀏覽:829
拓荒者知識產權 瀏覽:774
商標侵權事宜處理委託書 瀏覽:168