7.8. PKCS12

简介:

 
package example.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

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.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class TestController {
@Autowired
private OAuth2RestOperations restTemplate;

@GetMapping("/")
@ResponseBody
public String index() {
OAuth2AccessToken token = restTemplate.getAccessToken();
System.out.println(token.getValue());
String tmp = restTemplate.getForObject("http://api.alpha.netkiller.cn/", String.class);
System.out.println(tmp);
return tmp;
}

@GetMapping("/ssl")
@ResponseBody
public String ssl() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
String url = "https://api.alpha.netkiller.cn/";

SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(null, (chain, authType) -> true).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpComponentsClientHttpRequestFactory.setConnectTimeout(60000);
httpComponentsClientHttpRequestFactory.setReadTimeout(180000);

final RestTemplate restTemplate = new RestTemplate(httpComponentsClientHttpRequestFactory);

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + this.restTemplate.getAccessToken().getValue());
HttpEntity<String> entity = new HttpEntity<String>(headers);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
String str = response.getBody();
return str;
}

@GetMapping("/pkcs12")
@ResponseBody
public String PKCS12(String url, String data) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("/opt/xxx.p12"));
keyStore.load(instream, "netkiller".toCharArray());
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContextBuilder.create().loadKeyMaterial(keyStore, "netkiller".toCharArray()).build();
// Allow TLSv1 protocol only
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, hostnameVerifier);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpclient);

RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Connection", "keep-alive");
httpHeaders.add("Accept", "*/*");
httpHeaders.add("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
httpHeaders.add("Host", "api.netkiller.cn");
httpHeaders.add("X-Requested-With", "XMLHttpRequest");
httpHeaders.add("Cache-Control", "max-age=0");
httpHeaders.add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");

HttpEntity<String> httpEntity = new HttpEntity<String>(httpHeaders);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
return response.getBody();

}

}
  
 




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
缓存 Kubernetes API
K8S Pod 停止不掉一直处于Terminating状态问题解决
主要是从pod 停止不掉一直处于Terminating到 发现k8s node处于NotReady状态,在发现为什么处于NotReady , 再到发现node 状态因为PLEG is not healthy: pleg was last seen active 等问题
4589 0
K8S Pod 停止不掉一直处于Terminating状态问题解决
|
负载均衡 网络协议 前端开发
一文快速上手 Nacos 注册中心+配置中心!
一文快速上手 Nacos 注册中心+配置中心!
7121 0
|
SQL 开发框架 .NET
数据库单表查询 - 简单筛选查询
数据库单表查询 - 简单筛选查询
218 0
|
Linux Docker 容器
Docker的Windows容器初体验
最近微软发布了Windows Server 2016,其中最让广大开发者和运维同学期待的就是Docker对Windows容器的支持。如果你手边没有Windows Server 2016的环境,我们也可以在Windows 10 操作系统上,使用Docker for Windows来开始实验。
101754 61
|
Ubuntu Linux Shell
Docker CE 镜像源站
Docker CE 镜像源站
216834 92
|
数据挖掘 定位技术 Python
python开发:空气质量历史数据分析(三)
python开发:空气质量历史数据分析(三)
180 0
|
域名解析 弹性计算 安全
CentOS8 如何安装图形界面
本文主要为大家讲解如何在CentOS8系统中安装图形界面并配置VNC。
2603 1
CentOS8 如何安装图形界面
JavaWeb核心之ServletContext
JavaWeb核心之ServletContext
164 0