前言最近负责应对第三方服务。 在坞站中,第三方服务是https请求,因此已知当请求https时,它会使用本地证书公钥访问服务SSL证书。 无法请求服务,因为我的本地没有对应的SSL证书。 向接口时报请求类似以下的错误。 资源的发现可以忽略SSL认证。
unabletofindvalidcertificationpathtorequestedtarget
一.创建RestTemplate配置类
@ configurationpublicclassresttemplateconfig { @ beanpublicresttemplateresttemplate () (generatehttplate ) }/***SSL证书* @ return */privatehttpcomponentsclienthttprequestfactorygeneratehttprequestfactory () trustrategyaccccess SSS try { sslcontext=sslcontext s.custom (.loadtrustmaterial (null,acceptingTrustStrategy ).build ); } catch (nosuchalgorithmexceptione ) { e.printStackTrace ); }缓存(keymanagementexceptione ) { e.printStackTrace ); }catch(keystoreexceptione ) { e.printStackTrace ); } sslconnectionsocketfactoryconnectionsocketfactory=newsslconnectionsocketfactory (sslcontext,new NoopHostnameVerifier ) ) httpclientbuilderhttpclientbuilder=http clients.custom (; http客户端构建器. setsslsocketfactory (connectionsocketfactory ); closeablehttpclienthttpclient=http client builder.build (tttpcomponentsclienthttprequestfactoryfactory=newhtttpcomponentscld ) factory.set http客户端(http客户端; 返回工厂; (二)建立测试类
@ springboottestpublicclassignoressltest { @ autowiredprivateresttemplateresttemplate; privatestring URL=' https://XXX.xxxx '; @ testpublicvoidtest ((/rest template用于服务调用responseentitystringresponseentity=rest template.postforentity ) URL,NUE }根据测试结果,验证是否成功请求了接口,并忽略SSL证书。
当然有安装证书等很多方法。