接口自动化之几种登陆情况
登陆脚本
背景:企业一般接口调用,出于对系统的安全性和客户信息安全性保障,很多操作需要为登陆态【即接口调用入参要有登陆的cookie信息】才能成功调用。
如果同学要做接口自动化,很多接口,必然先写登陆脚本。
ps:本文只整理我遇到过的情况,脚本实现方式 java语言,开发工具可以用 idea /eclipse …
1、需要证书认证的登陆
我比较懒, 忽略证书,直接通过 x509Certificates
的认证方式,那么如何知道你的登陆脚本需要这个呢:写完脚本调试,会出现如下报错:
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
....
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
....
Cannot invoke "org.apache.http.client.methods.CloseableHttpResponse.close()" because "response" is null
....
废话不多说,直接抛解决方案:
首先定义一个类RestTemplateConfiguration,忽略证书
package com.opr.oders.common.httpclient;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.