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

目录
相关文章
|
6月前
|
存储 算法 安全
PKCS#1、PKCS#5、PKCS#7、PKCS#8到底是什么?
PKCS#1、PKCS#5、PKCS#7、PKCS#8到底是什么?
857 0
|
2月前
|
存储 安全 数据安全/隐私保护
浅谈对称加密(AES与DES)
浅谈对称加密(AES与DES)
|
2月前
|
算法 JavaScript Java
AES和RSA
AES和RSA
39 0
|
6月前
|
安全 算法 数据库
MD5、SHA、DES、AES、RSA的算法说明
【5月更文挑战第10天】MD5、SHA、DES、AES、RSA的算法说明
254 2
|
C语言
SHA加密算法
SHA加密算法
67 0
|
算法 安全 数据安全/隐私保护
DES加密算法
DES加密算法
48 0
|
算法 安全 Java
[转载]加密解密算法【RSA、AES、DES、MD5】介绍和使用
为了防止我们的数据泄露,我们往往会对数据进行加密,特别是敏感数据,我们要求的安全性更高。下面将介绍几种常用的加密算法使用。这些算法的加密对象都是基于二进制数据,如果要加密字符串就使用统一编码(如:utf8)进行编码后加密。
2556 0
|
网络安全
RSA私钥和公钥文件格式 (pkcs#7, pkcs#8, pkcs#12, pem)
Format Name Description PKCS #7 Cryptographic Message Syntax Standard A PKCS #7 file can be used to store certificates, which is a SignedData structure without data (just the certificates).
3656 0
|
存储 数据安全/隐私保护 Windows
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
PEM是一种常见的保存key或者证书的格式,PEM格式的文件一般来说后缀是以.pem结尾的。那么PEM到底是什么呢?它和常用的证书格式PKCS7和PKCS12有什么关系呢?一起来看看吧。
|
网络安全 Windows