• 秒懂HTTPS接口(接口测试篇)


    				版权声明:本文为博主原创文章,未经博主允许不得转载。					https://blog.csdn.net/zuozewei/article/details/84727450				</div>
    							            <div id="content_views" class="markdown_views prism-dracula">
    						<!-- flowchart 箭头图标 勿删 -->
    						<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg>
    						<p></p><div class="toc"><h3><a name="t0"></a>文章目录</h3><ul><ul><li><a href="#_1" rel="nofollow" target="_self">前言</a></li><li><a href="#_9" rel="nofollow" target="_self">具体实现</a></li><ul><li><a href="#_10" rel="nofollow" target="_self">引包</a></li><li><a href="#HTTPS_40" rel="nofollow" target="_self">采用绕过证书验证测试HTTPS接口</a></li><li><a href="#HTTPS_233" rel="nofollow" target="_self">采用设置信任自签名证书测试HTTPS接口</a></li><li><a href="#_381" rel="nofollow" target="_self">验证数据库</a></li><li><a href="#_385" rel="nofollow" target="_self">完整项目结构</a></li></ul></ul></ul></div><p></p>
    

    前言

    下面我们来测试下我们秒懂HTTPS接口(实现篇)写的HTTPS接口(Java版)

    技术选型:

    • HTTP工具包:HttpClient 4.5.5
    • 测试框架:TestNG
    • Json序列化库:fastjson

    具体实现

    引包

    引入相关包

    <!--引入接口测试相关包-->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>6.14.3</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    测试HTTPS接口可以通过以下两种方式:

    • 采用绕过证书验证实现HTTPS
    • 采用设置信任自签名证书实现HTTPS

    采用绕过证书验证测试HTTPS接口

    src/test/util下创建HttpUtil工具类

    实现绕过SSL验证方法

    /**
    	 * 绕过SSL验证
    	 *
    	 * @return
    	 * @throws NoSuchAlgorithmException
    	 * @throws KeyManagementException
    	 */
    	public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {
    		SSLContext sslContext = SSLContext.getInstance("SSLv3");
    
    	<span class="token comment">// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法</span>
    	X509TrustManager trustManager <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">X509TrustManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    		<span class="token annotation punctuation">@Override</span>
    		<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">checkClientTrusted</span><span class="token punctuation">(</span>
    				java<span class="token punctuation">.</span>security<span class="token punctuation">.</span>cert<span class="token punctuation">.</span>X509Certificate<span class="token punctuation">[</span><span class="token punctuation">]</span> paramArrayOfX509Certificate<span class="token punctuation">,</span>
    				String paramString<span class="token punctuation">)</span> <span class="token keyword">throws</span> CertificateException <span class="token punctuation">{</span>
    		<span class="token punctuation">}</span>
    
    		<span class="token annotation punctuation">@Override</span>
    		<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">checkServerTrusted</span><span class="token punctuation">(</span>
    				java<span class="token punctuation">.</span>security<span class="token punctuation">.</span>cert<span class="token punctuation">.</span>X509Certificate<span class="token punctuation">[</span><span class="token punctuation">]</span> paramArrayOfX509Certificate<span class="token punctuation">,</span>
    				String paramString<span class="token punctuation">)</span> <span class="token keyword">throws</span> CertificateException <span class="token punctuation">{</span>
    		<span class="token punctuation">}</span>
    
    		<span class="token annotation punctuation">@Override</span>
    		<span class="token keyword">public</span> java<span class="token punctuation">.</span>security<span class="token punctuation">.</span>cert<span class="token punctuation">.</span>X509Certificate<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">getAcceptedIssuers</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    			<span class="token keyword">return</span> null<span class="token punctuation">;</span>
    		<span class="token punctuation">}</span>
    	<span class="token punctuation">}</span><span class="token punctuation">;</span>
    
    	sslContext<span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span>null<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">TrustManager</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">{</span> trustManager <span class="token punctuation">}</span><span class="token punctuation">,</span> null<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">return</span> sslContext<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    实现绕过SSL证书,发送Get请求方法

    /**
    	 * 绕过SSL证书,发送Get请求
    	 * @param url
    	 * @param params
    	 * @return
    	 * @throws IOException
    	 * @throws KeyManagementException
    	 * @throws NoSuchAlgorithmException
    	 */
    	public static String doIgnoreVerifySSLGet(String url, Map<String,Object> params)
    			throws IOException, KeyManagementException, NoSuchAlgorithmException {
    		//采用绕过验证的方式处理https请求
    		SSLContext sslContext = createIgnoreVerifySSL();
    		final SSLConnectionSocketFactory sslsf;
    
    	<span class="token comment">//设置协议http和https对应的处理socket链接工厂的对象</span>
    	sslsf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SSLConnectionSocketFactory</span><span class="token punctuation">(</span>sslContext<span class="token punctuation">,</span> NoopHostnameVerifier<span class="token punctuation">.</span>INSTANCE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">final</span> Registry<span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span> registry <span class="token operator">=</span> RegistryBuilder<span class="token punctuation">.</span><span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"http"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">PlainConnectionSocketFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"https"</span><span class="token punctuation">,</span> sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">final</span> PoolingHttpClientConnectionManager cm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PoolingHttpClientConnectionManager</span><span class="token punctuation">(</span>registry<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	cm<span class="token punctuation">.</span><span class="token function">setMaxTotal</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">//创建自定义的httpclient对象</span>
    	CloseableHttpClient httpClient <span class="token operator">=</span> HttpClients<span class="token punctuation">.</span><span class="token function">custom</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setSSLSocketFactory</span><span class="token punctuation">(</span>sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setConnectionManager</span><span class="token punctuation">(</span>cm<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	String result <span class="token operator">=</span> null<span class="token punctuation">;</span>
    	<span class="token comment">//装填参数</span>
    	StringBuffer param <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>params <span class="token operator">!=</span> null <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>params<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    		<span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    		<span class="token keyword">for</span> <span class="token punctuation">(</span>String key <span class="token operator">:</span> params<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    			<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    				param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"?"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    				param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"&amp;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			<span class="token punctuation">}</span>
    			param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			i<span class="token operator">++</span><span class="token punctuation">;</span>
    		<span class="token punctuation">}</span>
    		url <span class="token operator">+=</span> param<span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    	<span class="token comment">//创建get方式请求对象</span>
    	HttpGet httpGet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HttpGet</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//执行请求操作,并拿到结果(同步阻塞)</span>
    	CloseableHttpResponse response <span class="token operator">=</span> httpClient<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>httpGet<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getStatusLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getStatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    		<span class="token comment">//获取结果实体</span>
    		HttpEntity httpEntity <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getEntity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    		<span class="token comment">//按指定编码转换结果实体为String类型</span>
    		result <span class="token operator">=</span> EntityUtils<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>httpEntity<span class="token punctuation">,</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    
    	<span class="token comment">//释放链接</span>
    	response<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    实现绕过SSL证书,发送Post请求(Json形式)方法

    /**
    	 * 绕过SSL证书,发送Post请求(Json形式)
    	 * @param url
    	 * @param param
    	 * @return
    	 * @throws IOException
    	 * @throws KeyManagementException
    	 * @throws NoSuchAlgorithmException
    	 */
    	public static String doIgnoreVerifySSLPost(String url, JSONObject param)
    			throws IOException, KeyManagementException, NoSuchAlgorithmException {
    		//采用绕过验证的方式处理https请求
    		SSLContext sslContext = createIgnoreVerifySSL();
    		final SSLConnectionSocketFactory sslsf;
    
    	<span class="token comment">//设置协议http和https对应的处理socket链接工厂的对象</span>
    	sslsf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SSLConnectionSocketFactory</span><span class="token punctuation">(</span>sslContext<span class="token punctuation">,</span> NoopHostnameVerifier<span class="token punctuation">.</span>INSTANCE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">final</span> Registry<span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span> registry <span class="token operator">=</span> RegistryBuilder<span class="token punctuation">.</span><span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"http"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">PlainConnectionSocketFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"https"</span><span class="token punctuation">,</span> sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">final</span> PoolingHttpClientConnectionManager cm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PoolingHttpClientConnectionManager</span><span class="token punctuation">(</span>registry<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	cm<span class="token punctuation">.</span><span class="token function">setMaxTotal</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">//创建自定义的httpclient对象</span>
    	CloseableHttpClient httpClient <span class="token operator">=</span> HttpClients<span class="token punctuation">.</span><span class="token function">custom</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setSSLSocketFactory</span><span class="token punctuation">(</span>sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setConnectionManager</span><span class="token punctuation">(</span>cm<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	String result <span class="token operator">=</span> null<span class="token punctuation">;</span>
    	<span class="token comment">//创建post方式请求对象</span>
    	HttpPost httpPost <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HttpPost</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//装填参数</span>
    	StringEntity entity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringEntity</span><span class="token punctuation">(</span>param<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	entity<span class="token punctuation">.</span><span class="token function">setContentEncoding</span><span class="token punctuation">(</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	entity<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//设置参数到请求对象中</span>
    	httpPost<span class="token punctuation">.</span><span class="token function">setEntity</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">//执行请求操作,并拿到结果(同步阻塞)</span>
    	CloseableHttpResponse response <span class="token operator">=</span> httpClient<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>httpPost<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getStatusLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getStatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    		<span class="token comment">//获取结果实体</span>
    		HttpEntity httpEntity <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getEntity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    		<span class="token comment">//按指定编码转换结果实体为String类型</span>
    		result <span class="token operator">=</span> EntityUtils<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>httpEntity<span class="token punctuation">,</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    
    	<span class="token comment">//释放链接</span>
    	response<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    src/test/cases下创建HttpTest测试类
    实现测试方法

    @Test(enabled = true,description = "测试绕过SSL证书Post方法")
    	public void doIgnoreVerifySSLPostTest() throws IOException, NoSuchAlgorithmException, KeyManagementException {
    		String url = "https://localhost/springboot/person";
    		//装填参数
    		JSONObject param = new JSONObject();
    		param.put("name","doIgnoreVerifySSLPost");
    		param.put("age",20);
    		//调用方法
    		String response = HttpUtil.doIgnoreVerifySSLPost(url,param);
    		//断言返回结果是否为空
    		Assert.assertNotNull(response);
    		System.out.println("【doIgnoreVerifySSLPost】"+response);
    	}
    
    <span class="token annotation punctuation">@Test</span><span class="token punctuation">(</span>enabled <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">,</span>description <span class="token operator">=</span> <span class="token string">"测试绕过SSL证书Get方法"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doIgnoreVerifySSLGetTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException<span class="token punctuation">,</span> NoSuchAlgorithmException<span class="token punctuation">,</span> KeyManagementException <span class="token punctuation">{</span>
    	String url <span class="token operator">=</span> <span class="token string">"https://localhost/springboot/person"</span><span class="token punctuation">;</span>
    	<span class="token comment">//调用方法</span>
    	String response <span class="token operator">=</span> HttpUtil<span class="token punctuation">.</span><span class="token function">doIgnoreVerifySSLGet</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>null<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//断言返回结果是否为空</span>
    	Assert<span class="token punctuation">.</span><span class="token function">assertNotNull</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"【doIgnoreVerifySSLGet】"</span><span class="token operator">+</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行测试结果
    在这里插入图片描述

    采用设置信任自签名证书测试HTTPS接口

    在HttpUtil工具类实现验证SSL证书,发送Get请求方法

    /**
    	 * 验证SSL证书,发送Get请求
    	 * @param url
    	 * @param params
    	 * @return
    	 * @throws IOException
    	 */
    	public static String doVerifySSLGet(String url, Map<String,Object> params) throws IOException {
    		//采用验证的SSL证书方式处理https请求
    		SSLContext sslContext = SSLCustom("./src/main/resources/keystore.p12","zuozewei");
    		final SSLConnectionSocketFactory sslsf;
    
    	<span class="token comment">// 设置协议http和https对应的处理socket链接工厂的对象</span>
    	sslsf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SSLConnectionSocketFactory</span><span class="token punctuation">(</span>sslContext<span class="token punctuation">,</span> NoopHostnameVerifier<span class="token punctuation">.</span>INSTANCE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">final</span> Registry<span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span> registry <span class="token operator">=</span> RegistryBuilder<span class="token punctuation">.</span><span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"http"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">PlainConnectionSocketFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"https"</span><span class="token punctuation">,</span> sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">final</span> PoolingHttpClientConnectionManager cm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PoolingHttpClientConnectionManager</span><span class="token punctuation">(</span>registry<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	cm<span class="token punctuation">.</span><span class="token function">setMaxTotal</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">//创建自定义的httpclient对象</span>
    	CloseableHttpClient httpClient <span class="token operator">=</span> HttpClients<span class="token punctuation">.</span><span class="token function">custom</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setSSLSocketFactory</span><span class="token punctuation">(</span>sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setConnectionManager</span><span class="token punctuation">(</span>cm<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	String result <span class="token operator">=</span> null<span class="token punctuation">;</span>
    	<span class="token comment">//装填参数</span>
    	StringBuffer param <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringBuffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>params <span class="token operator">!=</span> null <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span>params<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    		<span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    		<span class="token keyword">for</span> <span class="token punctuation">(</span>String key <span class="token operator">:</span> params<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    			<span class="token keyword">if</span> <span class="token punctuation">(</span>i <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    				param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"?"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    				param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"&amp;"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			<span class="token punctuation">}</span>
    			param<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    			i<span class="token operator">++</span><span class="token punctuation">;</span>
    		<span class="token punctuation">}</span>
    		url <span class="token operator">+=</span> param<span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    
    	<span class="token comment">//创建get方式请求对象</span>
    	HttpGet httpGet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HttpGet</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//执行请求操作,并拿到结果(同步阻塞)</span>
    	CloseableHttpResponse response <span class="token operator">=</span> httpClient<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>httpGet<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getStatusLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getStatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    		<span class="token comment">//获取结果实体</span>
    		HttpEntity httpEntity <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getEntity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    		<span class="token comment">//按指定编码转换结果实体为String类型</span>
    		result <span class="token operator">=</span> EntityUtils<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>httpEntity<span class="token punctuation">,</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    
    	<span class="token comment">//释放链接</span>
    	response<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    实现验证SSL证书,发送Post请求(Json形式)方法

    /**
    	 * 验证SSL证书,发送Post请求(Json形式)
    	 * @param url
    	 * @param param
    	 * @return
    	 * @throws IOException
    	 */
    	public static String doVerifySSLPost(String url, JSONObject param) throws IOException {
    		//采用验证的SSL证书方式处理https请求
    		SSLContext sslContext = SSLCustom("./src/main/resources/keystore.p12","zuozewei");
    		final SSLConnectionSocketFactory sslsf;
    
    	<span class="token comment">//设置协议http和https对应的处理socket链接工厂的对象</span>
    	sslsf <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SSLConnectionSocketFactory</span><span class="token punctuation">(</span>sslContext<span class="token punctuation">,</span> NoopHostnameVerifier<span class="token punctuation">.</span>INSTANCE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">final</span> Registry<span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span> registry <span class="token operator">=</span> RegistryBuilder<span class="token punctuation">.</span><span class="token generics function"><span class="token punctuation">&lt;</span>ConnectionSocketFactory<span class="token punctuation">&gt;</span></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"http"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">PlainConnectionSocketFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">"https"</span><span class="token punctuation">,</span> sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">final</span> PoolingHttpClientConnectionManager cm <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PoolingHttpClientConnectionManager</span><span class="token punctuation">(</span>registry<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	cm<span class="token punctuation">.</span><span class="token function">setMaxTotal</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token comment">//创建自定义的httpclient对象</span>
    	CloseableHttpClient httpClient <span class="token operator">=</span> HttpClients<span class="token punctuation">.</span><span class="token function">custom</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setSSLSocketFactory</span><span class="token punctuation">(</span>sslsf<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">setConnectionManager</span><span class="token punctuation">(</span>cm<span class="token punctuation">)</span>
    			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	String result <span class="token operator">=</span> null<span class="token punctuation">;</span>
    
    	<span class="token comment">//创建post方式请求对象</span>
    	HttpPost httpPost <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HttpPost</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//装填参数</span>
    	StringEntity entity <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">StringEntity</span><span class="token punctuation">(</span>param<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"utf-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	entity<span class="token punctuation">.</span><span class="token function">setContentEncoding</span><span class="token punctuation">(</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	entity<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"application/json"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//设置参数到请求对象中</span>
    	httpPost<span class="token punctuation">.</span><span class="token function">setEntity</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//执行请求操作,并拿到结果(同步阻塞)</span>
    	CloseableHttpResponse response <span class="token operator">=</span> httpClient<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span>httpPost<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token keyword">if</span> <span class="token punctuation">(</span>response<span class="token punctuation">.</span><span class="token function">getStatusLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getStatusCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    		<span class="token comment">//获取结果实体</span>
    		HttpEntity httpEntity <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getEntity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    		<span class="token comment">//按指定编码转换结果实体为String类型</span>
    		result <span class="token operator">=</span> EntityUtils<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>httpEntity<span class="token punctuation">,</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token punctuation">}</span>
    	<span class="token comment">//释放链接</span>
    	response<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    	<span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在HttpTest测试类,实现测试方法

    @Test(enabled = true,description = "测试验证SSL证书Post方法")
    	public void doVerifySSLPostTest() throws IOException {
    		String url = "https://localhost/springboot/person";
    		//装填参数
    		JSONObject param = new JSONObject();
    		param.put("name","doVerifySSLPost");
    		param.put("age",20);
    		//调用方法
    		String response = HttpUtil.doVerifySSLPost(url,param);
    		//断言返回结果是否为空
    		Assert.assertNotNull(response);
    		System.out.println("【doVerifySSLPost】"+response);
    	}
    
    <span class="token annotation punctuation">@Test</span><span class="token punctuation">(</span>enabled <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">,</span>description <span class="token operator">=</span> <span class="token string">"测试验证SSL证书Get方法"</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">doVerifySSLGetTest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
    	String url <span class="token operator">=</span> <span class="token string">"https://localhost/springboot/person"</span><span class="token punctuation">;</span>
    	<span class="token comment">//调用方法</span>
    	String response <span class="token operator">=</span> HttpUtil<span class="token punctuation">.</span><span class="token function">doVerifySSLGet</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span>null<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	<span class="token comment">//断言返回结果是否为空</span>
    	Assert<span class="token punctuation">.</span><span class="token function">assertNotNull</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    	System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"【doVerifySSLGet】"</span><span class="token operator">+</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行测试结果
    在这里插入图片描述

    验证数据库

    查询数据库结果
    在这里插入图片描述

    完整项目结构

    在这里插入图片描述

    秒懂HTTPS接口系列源码:
    https://github.com/zuozewei/Java-API-Test-Examples

    相关系列:
    秒懂HTTPS接口(原理篇)
    秒懂HTTPS接口(实现篇)
    秒懂HTTPS接口(JMeter压测篇)

            </div>
    					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet">
                </div>
    </article>
  • 相关阅读:
    责任
    人生
    有感
    冲突无处不在
    中科院玉泉路攻略——吃篇(校外)
    兄弟
    (18)[转载]:跳台阶问题
    Excel表格加下列选择框
    (转载)(c#)数据结构与算法分析 数组、向量和表
    [转载] 趣味算法:字符串反转的N种方法
  • 原文地址:https://www.cnblogs.com/wldan/p/10597564.html
Copyright © 2020-2023  润新知