4.8. PKCS12-阿里云开发者社区

开发者社区> 玄学酱> 正文

4.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 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何做接口测试?postman测试工具的操作使用 及测试webservice接口方法
如何做接口测试?postman测试工具的操作使用 及测试webservice接口方法
6 0
Docker | Docker 简介、安装与入门
Docker | Docker 简介、安装与入门
8 0
ESC使用体验
飞天加速计划·高校学生在家实践”活动使用心得
9 0
软件测试之 【移动端测试】——安装与卸载
软件测试之 【移动端测试】——安装与卸载
5 0
禅道 测试管理工具介绍
禅道 测试管理工具介绍
6 0
使用脚本进行 SAP Spartacus 安装工作
使用脚本进行 SAP Spartacus 安装工作
6 0
软件测试 之【移动端测试】——安全性测试
软件测试 之【移动端测试】——安全性测试
6 0
SAP 电商云 UI State persistence 状态持久化设计的入口 - initSync
SAP 电商云 UI State persistence 状态持久化设计的入口 - initSync
7 0
关于 SAP Spartacus feature library 里的 _index.scss 文件
关于 SAP Spartacus feature library 里的 _index.scss 文件
5 0
+关注
玄学酱
这个时候,玄酱是不是应该说点什么...
17436
文章
438
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载