摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進(jìn)并啟動(dòng),使用訪問(wèn)和鏈接。原理后續(xù)進(jìn)一步研究
1.組件版本信息
apache-tomcat-7.0.75
JDK 1.8.0_91
2.使用jdk自帶的keytool命令生成keystore文件test.keystore
命令:keytool -genkey -alias test123 -keypass test123 -keyalg RSA -keysize 1024 -keystore test.keystore -storepass test123
3.將test.keystore拷貝到apache-tomcat-7.0.75bin目錄下
4.配置tomcat的conf目錄server.xml文件,在配置文件中新增SSL配置
5.將webservice工程添加進(jìn)tomcat并啟動(dòng),使用postman訪問(wèn)http和https鏈接。http可以正常訪問(wèn),https訪問(wèn)不了,由于客戶(hù)端證書(shū)問(wèn)題
6.新建類(lèi)HttpClientTest,用于配置https相關(guān)SSL設(shè)置
import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; public class HttpClientTest { public static CloseableHttpClient createSSLClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial( null, new TrustStrategy() { public boolean isTrusted( X509Certificate[] chain, String authType ) throws CertificateException { return true; } } ).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE ); return HttpClients.custom().setSSLSocketFactory( sslsf ).build(); } }
7.新建類(lèi)HttpClientUtil,用于測(cè)試https的get請(qǐng)求
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import org.apache.commons.logging.Log; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.util.EntityUtils; public class HttpClientUtil { public static void main( String[] args ) throws ClientProtocolException, IOException, URISyntaxException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException { String url = "https://localhost:8443/maven-example/hello"; CloseableHttpClient httpClient = HttpClientTest.createSSLClient(); HttpGet get = new HttpGet(); get.setURI( new URI( url ) ); HttpResponse response = httpClient.execute( get ); String s = streamToString( response.getEntity().getContent() ); System.out.println( s ); } private static String streamToString( InputStream is ) throws IOException { String line = ""; StringBuilder total = new StringBuilder(); BufferedReader rd = new BufferedReader( new InputStreamReader( is ) ); while ( (line = rd.readLine()) != null ) { total.append( line ); } return total.toString(); } }
8.執(zhí)行main方法,正確輸出https的response響應(yīng)
9.操作過(guò)程中遇到一個(gè)問(wèn)題,報(bào)主機(jī)名驗(yàn)證錯(cuò)誤
解決方法:將new SSLConnectionSocketFactory(sslContext)修改為new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)即可。原理后續(xù)進(jìn)一步研究
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslContext, NoopHostnameVerifier.INSTANCE);
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66765.html
摘要:它使用方式,接收和響應(yīng)外部系統(tǒng)的某種請(qǐng)求?;仡櫸覀?cè)趯W(xué)習(xí)基礎(chǔ)網(wǎng)絡(luò)編程章節(jié)已經(jīng)知道了這么一個(gè)連接了。使用指定名稱(chēng)的命名空間。名詞簡(jiǎn)單對(duì)象訪問(wèn)協(xié)議作為一個(gè)基于語(yǔ)言的協(xié)議用于有網(wǎng)上傳輸數(shù)據(jù)。以的根元素出現(xiàn)。代理這么一個(gè)概念就更加清晰了。 WebService介紹 首先我們來(lái)談一下為什么需要學(xué)習(xí)webService這樣的一個(gè)技術(shù)吧.... 問(wèn)題一 如果我們的網(wǎng)站需要提供一個(gè)天氣預(yù)報(bào)這樣一個(gè)需求...
摘要:使支持協(xié)議類(lèi),是自定義私有類(lèi)請(qǐng)求類(lèi)根據(jù)請(qǐng)求報(bào)文,請(qǐng)求服務(wù)地址獲取響應(yīng)報(bào)文請(qǐng)求報(bào)文請(qǐng)求地址字符集類(lèi)型封裝的服務(wù)器響應(yīng)參數(shù)和返回報(bào)文正常響應(yīng)。 使commons httpclient支持https協(xié)議類(lèi),是commons httpclient import java.io.IOException; import java.net.InetAddress; import java.n...
摘要:執(zhí)行結(jié)果如下中華田園犬測(cè)試我認(rèn)為所有使用協(xié)議的,都能使用測(cè)試。下面是我寫(xiě)的測(cè)試代碼旺財(cái)需要增加一個(gè)包測(cè)試結(jié)果返回值如下中華田園犬寫(xiě)法稍微有點(diǎn)麻煩的是,需要拼接請(qǐng)求參數(shù),參數(shù)少的話還好,多的話就很煩不過(guò)這種方法不用生成一大堆客戶(hù)端代碼。 經(jīng)過(guò)前段時(shí)間的學(xué)習(xí),已經(jīng)實(shí)現(xiàn)一個(gè)有返回值的spring-ws服務(wù),那接下來(lái),就要試試能不能通過(guò)不同方式的調(diào)用,要實(shí)現(xiàn)一下幾種方式的測(cè)試: spring...
閱讀 2479·2021-11-16 11:45
閱讀 2450·2021-10-11 10:59
閱讀 2255·2021-10-08 10:05
閱讀 3835·2021-09-23 11:30
閱讀 2375·2021-09-07 09:58
閱讀 806·2019-08-30 15:55
閱讀 778·2019-08-30 15:53
閱讀 1928·2019-08-29 17:00