Spring boot 获取TestRestTemplate以使用https
为设置安全cookie的REST端点编写JUnit集成测试无法通过ResourceAccessException错误 要求是做一个请求 已尝试使用customRestTemplate 正在获取以下异常 org.springframework.web.client.ResourceAccessException:获取“”请求时出现I/O错误:连接到本地主机:8443[localhost/127.0.0.1,localhost/0:0:0:0:1]失败:连接被拒绝:连接;嵌套异常是org.apache.http.conn.HttpHostConnectException 下面是代码Spring boot 获取TestRestTemplate以使用https,spring-boot,junit,Spring Boot,Junit,为设置安全cookie的REST端点编写JUnit集成测试无法通过ResourceAccessException错误 要求是做一个请求 已尝试使用customRestTemplate 正在获取以下异常 org.springframework.web.client.ResourceAccessException:获取“”请求时出现I/O错误:连接到本地主机:8443[localhost/127.0.0.1,localhost/0:0:0:0:1]失败:连接被拒绝:连接;嵌套异常是org.apach
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class DcsServiceTests {
@Autowired
RestTemplateBuilder restTemplateBuilder;
@Autowired
private TestRestTemplate testRestTemplate;
@Test
public void testGet_ImageResponse() throws Exception {
//Arrange
//Act
ResponseEntity<byte[]> response = testRestTemplate.getForEntity(url, byte[].class);
//Assert
//Response Status
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
//Response has cookie
assertThat(response.getHeaders().containsKey("Set-Cookie")).isTrue();
}
@PostConstruct
public void initialize() {
// Lambda expression not working, TBD - Java version used.
//TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
final TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws CertificateException {
return true;
}
};
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
try {
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
requestFactory.setHttpClient(httpClient);
}
catch (Exception e) {
System.out.println("Exception occured creating Request Factory");
}
RestTemplate customTemplate = restTemplateBuilder
.requestFactory(requestFactory)
.rootUri("https://localhost:8443")
.build();
this.testRestTemplate = new TestRestTemplate(
customTemplate,
null,
null, // Not using basic auth
TestRestTemplate.HttpClientOption.ENABLE_COOKIES); // Cookie support
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=webEnvironment.RANDOM\u端口)
公共类DcsServiceTests{
@自动连线
RestTemplateBuilder RestTemplateBuilder;
@自动连线
私有TestRestTemplate TestRestTemplate;
@试验
public void testGet_ImageResponse()引发异常{
//安排
//表演
ResponseEntity response=testRestTemplate.getForEntity(url,字节[].class);
//断言
//响应状态
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
//响应有cookie
assertThat(response.getHeaders().containsKey(“Set Cookie”)).isTrue();
}
@施工后
公共无效初始化(){
//Lambda表达式不工作,待定-使用了Java版本。
//TrustStrategy acceptingTrustStrategy=(X509Certificate[]链,字符串authType)->true;
最终信任策略接受信任策略=新信任策略(){
@凌驾
已信任公共布尔值(java.security.cert.X509Certificate[]arg0,字符串arg1)
抛出证书异常{
返回true;
}
};
HttpComponents客户端HttpRequestFactory请求工厂=
新的HttpComponents客户端HttpRequestFactory();
试一试{
SSLContext SSLContext=org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(空,acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf=新的SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient=HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
setHttpClient(httpClient);
}
捕获(例外e){
System.out.println(“创建请求工厂时发生异常”);
}
RestTemplate customTemplate=restTemplateBuilder
.requestFactory(requestFactory)
.rootUri(“https://localhost:8443")
.build();
this.testRestTemplate=新的testRestTemplate(
自定义模板,
无效的
null,//不使用基本身份验证
TestRestTemplate.HttpClientOption.ENABLE_COOKIES);//Cookie支持
}
}
禁用SSL,然后使用带有exchange方法的testRestTemplate有效。安全Cookie也可以工作,只是需要解析头以验证单元测试用例中的结果
@Bean
public Boolean disableSSLValidation() throws Exception {
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return true;
}
public void hostNameVerifier() {
final HostnameVerifier defaultHostnameVerifier = javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier ();
final HostnameVerifier localhostAcceptedHostnameVerifier = new javax.net.ssl.HostnameVerifier () {
public boolean verify ( String hostname, javax.net.ssl.SSLSession sslSession ) {
if ( hostname.equals ( "localhost" ) ) {
return true;
}
return defaultHostnameVerifier.verify ( hostname, sslSession );
}
};
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier ( localhostAcceptedHostnameVerifier );
}
@Test
public void testGet_ImageResponse() throws Exception {
//Arrange
String url = getUrl() + "/xyz?s_action=test&s_type=i";
//Act
ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
//Assert
//Response Status
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
//Response has cookie
assertThat(response.getHeaders().containsKey("Set-Cookie")).isTrue();
//Extract cookie from header
List<String> cookies = response.getHeaders().get("Set-Cookie");
//Construct cookie from RAW Header Response
Cookie cookie = RawCookieParser.constructCookieFromHeaderResponse(response.getHeaders().get("Set-Cookie").toString());
//Cookies name matches
//Cookie value cannot be matched because value is being set from external JAR
assertEquals(cookie.getName(), appConfig.getName());
//Cookie domain matches
assertEquals(cookie.getDomain(), appConfig.getDomain());
}
public class RawCookieParser {
/*
* Construct a cookie object by parsing the HTTP Header response
*/
public static Cookie constructCookieFromHeaderResponse(String input) throws Exception {
String rawCookie = input.replace("[", "").replace("]", "");
String[] rawCookieParams = rawCookie.split(";");
String[] rawCookieNameAndValue = rawCookieParams[0].split("=");
if (rawCookieNameAndValue.length != 2) {
throw new Exception("Invalid cookie: missing name and value.");
}
String cookieName = rawCookieNameAndValue[0].trim();
String cookieValue = rawCookieNameAndValue[1].trim();
Cookie cookie = new Cookie(cookieName, cookieValue);
for (int i = 1; i < rawCookieParams.length; i++) {
String rawCookieParamNameAndValue[] = rawCookieParams[i].trim().split("=");
String paramName = rawCookieParamNameAndValue[0].trim();
if (rawCookieParamNameAndValue.length == 2) {
String paramValue = rawCookieParamNameAndValue[1].trim();
if (paramName.equalsIgnoreCase("secure")) {
cookie.setSecure(true);
} else if (paramName.equalsIgnoreCase("max-age")) {
int maxAge = Integer.parseInt(paramValue);
cookie.setMaxAge(maxAge);
} else if (paramName.equalsIgnoreCase("domain")) {
cookie.setDomain(paramValue);
} else if (paramName.equalsIgnoreCase("path")) {
cookie.setPath(paramValue);
}
}
}
return cookie;
}
}
@Bean
public Boolean disableSSLValidation()引发异常{
最终SSLContext SSLContext=SSLContext.getInstance(“TLS”);
init(null,new TrustManager[]{new X509TrustManager(){
@凌驾
public void checkClientTrusted(X509Certificate[]x509Certificates,字符串s)抛出CertificateException{
}
@凌驾
public void checkServerTrusted(X509Certificate[]x509Certificates,字符串s)抛出CertificateException{
}
@凌驾
公共X509证书[]getAcceptedIssuers(){
返回新的X509证书[0];
}
}},空);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
公共布尔验证(字符串主机名、SSLSession会话){
返回true;
}
});
返回true;
}
公共无效主机名验证程序(){
最终HostnameVerifier defaultHostnameVerifier=javax.net.ssl.HttpsURLConnection.getDefaultHostnameVerifier();
最终HostnameVerifier localhostAcceptedHostnameVerifier=新的javax.net.ssl.HostnameVerifier(){
公共布尔验证(字符串主机名,javax.net.ssl.SSLSession){
if(hostname.equals(“localhost”)){
返回true;
}
返回defaultHostnameVerifier.verify(主机名,sslSession);
}
};
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(localhostAcceptedHostnameVerifier);
}
@试验
public void testGet_ImageResponse()引发异常{
//安排
字符串url=getUrl()+“/xyz?s_action=test&s_type=i”;
//表演
ResponseEntity response=restemplate.getForEntity(url,字节[].class);
//断言
//响应状态
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
//响应有cookie
assertThat(response.getHeaders().containsKey(“Set Cookie”)).isTrue();
//从标题中提取cookie
List cookies=response.getHeaders().get(“设置Cookie”);
//从原始头响应构造cookie
Cookie Cookie=RawCookieParser.constructCookieFromHeaderResponse(response.getHeaders().get(“Set Cookie”).toString());
//Cookies名称匹配
//无法匹配Cookie值,因为正在从外部JAR设置值
assertEquals(cookie.getName(),appConfig.getName());
//Cookie域匹配
assertEquals(cookie.getDomain(),appConfig.getDomain());
}
公共类rawcookie解析器{
/*
*通过解析HTTP头来构造cookie对象