摘要:組件版本信息使用自帶的命令生成文件命令將拷貝到目錄下配置的目錄文件,在配置文件中新增配置將工程添加進(jì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并啟動,使用postman訪問http和https鏈接。http可以正常訪問,https訪問不了,由于客戶端證書問題
6.新建類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.新建類HttpClientUtil,用于測試https的get請求
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.操作過程中遇到一個問題,報主機(jī)名驗證錯誤
解決方法:將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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/66765.html
摘要:它使用方式,接收和響應(yīng)外部系統(tǒng)的某種請求?;仡櫸覀冊趯W(xué)習(xí)基礎(chǔ)網(wǎng)絡(luò)編程章節(jié)已經(jīng)知道了這么一個連接了。使用指定名稱的命名空間。名詞簡單對象訪問協(xié)議作為一個基于語言的協(xié)議用于有網(wǎng)上傳輸數(shù)據(jù)。以的根元素出現(xiàn)。代理這么一個概念就更加清晰了。 WebService介紹 首先我們來談一下為什么需要學(xué)習(xí)webService這樣的一個技術(shù)吧.... 問題一 如果我們的網(wǎng)站需要提供一個天氣預(yù)報這樣一個需求...
摘要:使支持協(xié)議類,是自定義私有類請求類根據(jù)請求報文,請求服務(wù)地址獲取響應(yīng)報文請求報文請求地址字符集類型封裝的服務(wù)器響應(yīng)參數(shù)和返回報文正常響應(yīng)。 使commons httpclient支持https協(xié)議類,是commons httpclient import java.io.IOException; import java.net.InetAddress; import java.n...
摘要:執(zhí)行結(jié)果如下中華田園犬測試我認(rèn)為所有使用協(xié)議的,都能使用測試。下面是我寫的測試代碼旺財需要增加一個包測試結(jié)果返回值如下中華田園犬寫法稍微有點麻煩的是,需要拼接請求參數(shù),參數(shù)少的話還好,多的話就很煩不過這種方法不用生成一大堆客戶端代碼。 經(jīng)過前段時間的學(xué)習(xí),已經(jīng)實現(xiàn)一個有返回值的spring-ws服務(wù),那接下來,就要試試能不能通過不同方式的調(diào)用,要實現(xiàn)一下幾種方式的測試: spring...
閱讀 2550·2021-11-16 11:45
閱讀 2534·2021-10-11 10:59
閱讀 2328·2021-10-08 10:05
閱讀 3993·2021-09-23 11:30
閱讀 2435·2021-09-07 09:58
閱讀 941·2019-08-30 15:55
閱讀 828·2019-08-30 15:53
閱讀 1983·2019-08-29 17:00