当前位置: 首页 > news >正文

网站的基本结构东莞谷歌推广公司

网站的基本结构,东莞谷歌推广公司,wordpress 多站点教程,网站风格配置怎么做一、方案描述 我这里采用RestTemplate的方式调用https请求&#xff0c;请求第三方接口获取数据&#xff0c;证书由第三方私自签发的证书&#xff0c;我们构建的是一个springboot的API项目。 1.pom文件引入jar <dependencies><dependency><groupId>org.spr…

一、方案描述

我这里采用RestTemplate的方式调用https请求,请求第三方接口获取数据,证书由第三方私自签发的证书,我们构建的是一个springboot的API项目。

1.pom文件引入jar

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Apache HttpClient - Used to request HTTP resources over the network --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
</dependencies>

2.构建一个RestTemplateConfig

构建RestTemplateConfig为了初始化RestTemplate让它具备验证证书功能。

/*** @Author: LongGE* @Date: 2023-08-28* @Description:*/
@Configuration
public class RestTemplateConfig {/*** 1.创建一个KeyStore,并将需要信任的证书加载到KeyStore中。示例代码如下:* @return* @throws CertificateException* @throws IOException* @throws KeyStoreException* @throws NoSuchAlgorithmException*/@Beanpublic KeyStore createKeyStore() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException {CertificateFactory certFactory = CertificateFactory.getInstance("X.509");FileInputStream inputStream =new FileInputStream("D:\\WorkSpace\\local\\online-project\\RequestSpringBoot\\src\\main\\resources\\my-certificate.crt");X509Certificate certificate = (X509Certificate) certFactory.generateCertificate(inputStream);KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());keyStore.load(null, null);keyStore.setCertificateEntry("my-cert", certificate);return keyStore;}/*** 2.创建一个TrustManagerFactory,使用上述创建的KeyStore来初始化它* @return* @throws CertificateException* @throws NoSuchAlgorithmException* @throws KeyStoreException* @throws IOException*/@Beanpublic TrustManagerFactory createTrustManagerFactory() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException {KeyStore keyStore = createKeyStore();TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(keyStore);return trustManagerFactory;}/*** 3.创建一个SSLContext,并使用上述创建的TrustManagerFactory来初始化它。* @return* @throws NoSuchAlgorithmException* @throws CertificateException* @throws KeyStoreException* @throws IOException* @throws KeyManagementException*/@Beanpublic SSLContext createSSLContext() throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, KeyManagementException {TrustManagerFactory trustManagerFactory = createTrustManagerFactory();SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());return sslContext;}/*** 4.创建一个HttpsURLConnectionFactory,使用上述创建的SSLContext来设置HttpsURLConnection的SSLSocketFactory。* @return* @throws CertificateException* @throws NoSuchAlgorithmException* @throws KeyStoreException* @throws KeyManagementException* @throws IOException*/@Beanpublic RestTemplate createRestTemplate() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {SSLContext sslContext = createSSLContext();HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();httpClientBuilder.setSSLContext(sslContext);// 创建HttpComponentsClientHttpRequestFactoryHttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();requestFactory.setHttpClient(httpClientBuilder.build());// 创建RestTemplate,并设置自定义的SSLSocketFactoryRestTemplate restTemplate = new RestTemplate(requestFactory);return restTemplate;}}

二.使用方案

这样构建好的RestTemplate,我们在Controller或者Service就可以通过@Autowried注解引入。

@RestController
@RequestMapping("/TestController")
public class TestController {@Autowiredprivate RestTemplate restTemplate;private String url = "https://www.houpu.com";private String relativePath2 = "/ResponseController/getTestMapping";@GetMapping("/test02")public String test02() {//发起请求String fullUrl2 = UriComponentsBuilder.fromHttpUrl(url).path(relativePath2).toUriString();String response2 = restTemplate.getForObject(fullUrl2, String.class);System.out.println(response2);return response2;}}

http://www.hkea.cn/news/784921/

相关文章:

  • 大片播放网站国外免费推广网站有哪些
  • flash网站cms排名sem优化软件
  • 申请完域名怎么做网站百度链接提交
  • 驻马店市可以做网站的公司百度搜索竞价排名
  • 郑州市做网站吉林百度查关键词排名
  • 济宁网站建设seo抖音seo源码搭建
  • 茂名网站建设方案书简述seo和sem的区别
  • 江西网站做的好的企业文化百度指数在哪里看
  • 山东电商网站建设seo网站排名优化公司
  • 赤峰市做网站公司今日的最新消息
  • 上海最大的贸易公司seo网络推广机构
  • jsp 网站开发广告发布平台
  • b2c网站综合对比评价站长统计幸福宝
  • 网站建设意见做推广app赚钱的项目
  • 哈尔滨营销网站制作做外贸推广
  • 深圳网站建设外贸公司排名搜索热词排名
  • 网络科技公司招聘可靠吗seo多久可以学会
  • 企业网站建设的方案ppt网络营销的特点主要包括什么
  • 如何自行建设网站推广关键词优化
  • 建设学院网站百度收录关键词查询
  • 有关外贸的网站有哪些内容武汉抖音seo搜索
  • 娄底网站建设的话术深圳网站关键词排名优化
  • 福田大型商城网站建设seo营销方法
  • 网站开发专业就业指导企业网站设计与实现论文
  • 网络营销方式的思维导图seo关键词优化系统
  • wordpress访客ip记录福清市百度seo
  • 网站下载速度慢互联网广告推广公司
  • 电影网站空间配置网络营销的工具和方法有哪些
  • 包装设计网站免费百度seo搜索引擎优化厂家
  • 免费做公司网站sem对seo的影响有哪些