SSLFactory


/**
 * @Author: bcoffman@paypal.com
 * A Secure Socket Layer Factory.
 */

package com.paypal.api.client;
import java.lang.*;
import java.net.*;
import javax.net.*;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.net.ssl.*;

 

/**
 * Default TrustManager checks that a cert is signed by a well known
 * certificate authority, like Verisign or Thawte.
 */
class RelaxedX509TrustManager implements X509TrustManager {
    public boolean checkClientTrusted(java.security.cert.X509Certificate[] chain){ return true; }
    public boolean isServerTrusted(java.security.cert.X509Certificate[] chain){ return true; }
    public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
}

/**
 * The SSL factory pattern (as in design patterns) that produces an SSL channel.
 * We specify a cert, and the passwor, as well as the format of the cert.  The
 * supported types are PCKS12, and JKS.  You must specify
 */
public class SSLFactory extends org.apache.axis.components.net.JSSESocketFactory {
    public SSLFactory(Hashtable attributes) {
 super(attributes);
    }

    protected void initFactory() throws IOException {
 sslFactory = get_SSLFactory();
    }

    protected SSLSocketFactory get_SSLFactory() throws IOException {
 String cert_password = "";
 String cert_format = "";
 String cert_file = "";
 try {
     SimpleConfigFile cfg = new SimpleConfigFile();  // Uses previously instantiated file.
     cert_file    = cfg.val("cert_file");
     cert_password= cfg.val("cert_password");
     cert_format  = cfg.val("cert_format");
            if (cert_format==null) {
                cert_format="PKCS12"; // Default value.
            }
            if (cert_file==null || cert_password==null) {
                throw new IOException("missing cert info");
            }
 }
 catch (Exception e) {
     System.err.println("com.paypal.api.client.SSLFactory: Problems with keystore configuration");
     throw new IOException("Problems with keystore configuration");
 }
 char[] keypass   = cert_password.toCharArray();
 char[] storepass = cert_password.toCharArray();
 try {
     //InputFileStream ifs = getClass().getResourceAsStream(cert_file);
     File file = new File(cert_file);
     if(!file.canRead()) {
  System.err.println("com.paypal.api.client.SSLFactory: Can't find/read cert file: /"" +cert_file+"/".");
  throw new IOException("com.paypal.api.client.SSLFactory: Can't find/read cert file: /"" +cert_file+"/".");
     }
     FileInputStream fin = new FileInputStream(file);
     SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
     random.setSeed(System.currentTimeMillis());
     KeyStore ks = null;
     if (cert_format.toUpperCase() == "JKS") {
         ks = KeyStore.getInstance(cert_format);
     } else {
         ks = KeyStore.getInstance(cert_format, "SunJSSE"); // PKCS12
     }
     KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
     TrustManager[] tm = {new RelaxedX509TrustManager()}; // customized, see above
            try {
         ks.load(fin, storepass);
            } catch (Exception e) {
                System.out.println("com.paypal.api.client.SSLFactory: Bad cert_password");
                throw new IOException("com.paypal.api.client.SSLFactory: Bad cert_password");
            }
     kmf.init(ks, keypass);
     SSLContext ctx = SSLContext.getInstance("SSL"); //TLS, SSLv3, SSL
     ctx.init(kmf.getKeyManagers(), tm, random);
     return ctx.getSocketFactory();
 }
 catch (Exception e) {
     throw new IOException("com.paypal.api.client.SSLFactory: Cannot create SSL factory.");
 }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中使用Hutool访问HTTPS接口的步骤如下: 1. 首先,你需要通过Hutool的HttpUtil类发送请求。HttpUtil类提供了一组静态方法,可以方便地发送HTTP请求和接收HTTP响应。 2. 然后,你需要创建一个SSL连接。由于konachan.net使用HTTPS协议,因此需要创建SSL连接。可以使用Hutool的SSLUtil类创建SSL连接。 3. 接下来,你需要设置请求的参数,如请求方式、请求头、请求体等。在Hutool中,可以使用Request类来设置请求参数。 4. 最后,你需要发送请求,并处理响应结果。HttpUtil类提供了一组静态方法,可以方便地发送HTTP请求和接收HTTP响应。 下面是一个使用Hutool访问https://konachan.net的接口的示例代码: ``` // 创建SSL连接 SSLContext sslContext = SSLUtil.createSSLContext(); SSLFactory sslFactory = new SSLFactory(sslContext, new StrictHostnameVerifier()); // 设置请求参数 Request request = Request.create(Method.GET, "https://konachan.net/post.json"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"); request.setSSLFactory(sslFactory); // 发送请求并处理响应结果 HttpResponse response = HttpUtil.execute(request); String result = response.body(); System.out.println(result); ``` 注意:在实际使用中,你需要替换请求URL和请求头中的User-Agent参数为你自己的信息。此外,还需要处理请求参数和响应结果的异常情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值