java androidOKHttp HTTPS 请求证书验证 PFX证书(2)

OKHttp的常用配置如配置Https证书,忽略Https证书,Cookie持久化 Retrofit应该是目前最火的联网框架,但是内部还是使用的OKHttp,在这总结一下常用的配置。 做Cookie的持久化。 OKHttp3.0之后和之前做Cookie持久化有了点区别下面直接上代码: .cookieJar(new CookieJar() { private final HashMap<String, List<Cookie>> cooki 阅读详情

地址:http://blog.csdn.net/doubleping/article/details/53332665
HTTPS 请求证书验证 PFX证书(2)的使用

PFX Load 关键代码

...

KeyStore keyStore = KeyStore.getInstance("PKCS12");
  keyStore.load(in, password);

.....
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
    TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
      SSLContext ssContext = SSLContext.getInstance("TLS");
ssContext.init(null,trustManagers,null);
  ...
client = new OkHttpClient.Builder()
      .sslSocketFactory(sslSocketFactory).hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
              return true;
            }
          })
      .build();
...

完整代码:

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.Arrays;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.CertificatePinner;
import okhttp3.OkHttpClient;

public final class CustomTrust {
  public static final String tag = "CustomTrust";
  private static final String CLIENT_KET_PASSWORD = "23234244224";
  public final OkHttpClient client;
  Context context;
  public CustomTrust(Context context)  {
    this.context = context;
    X509TrustManager trustManager;
    SSLSocketFactory sslSocketFactory=null;

//    trustManager = new X509TrustManager() {
//      @Override
//      public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//      }
//
//      @Override
//      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//      }
//
//      @Override
//      public X509Certificate[] getAcceptedIssuers() {
//        return new X509Certificate[0];
//      }
//    };

    try {
    //  trustManager = trustManagerForCertificates(trustedCertificatesInputStream());
      SSLContext sslContext =  trustManagerForCertificates(trustedCertificatesInputStream()); //SSLContext.getInstance("TLS");
    //  sslContext = SSLContext.getInstance("SSL");
     // sslContext.init(null,new X509TrustManager[]{trustManager},null);
      sslSocketFactory = sslContext.getSocketFactory();
    } catch (GeneralSecurityException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      e.printStackTrace();
    }
    client = new OkHttpClient.Builder()
        .sslSocketFactory(sslSocketFactory).hostnameVerifier(new HostnameVerifier() {
              @Override
              public boolean verify(String hostname, SSLSession session) {
                return true;
              }
            })
        .build();

  }


  /**
   * Returns an input stream containing one or more certificate PEM files. This implementation just
   * embeds the PEM files in Java strings; most applications will instead read this from a resource
   * file that gets bundled with the application.
   */
  private InputStream trustedCertificatesInputStream() {
    // PEM files for root certificates of Comodo and Entrust. These two CAs are sufficient to view
    // https://publicobject.com (Comodo) and https://squareup.com (Entrust). But they aren't
    // sufficient to connect to most HTTPS sites including https://godaddy.com and https://visa.com.
    // Typically developers will need to get a PEM file from their organization's TLS administrator.

    return context.getResources().openRawResource(R.raw.a22323) ;

    /*return new Buffer()
        .writeUtf8(comodoRsaCertificationAuthority)
        .writeUtf8(entrustRootCertificateAuthority)
        .inputStream();*/
  }

  /**
   * Returns a trust manager that trusts {@code certificates} and none other. HTTPS services whose
   * certificates have not been signed by these certificates will fail with a {@code
   * SSLHandshakeException}.
   *
   * <p>This can be used to replace the host platform's built-in trusted certificates with a custom
   * set. This is useful in development where certificate authority-trusted certificates aren't
   * available. Or in production, to avoid reliance on third-party certificate authorities.
   *
   * <p>See also {@link CertificatePinner}, which can limit trusted certificates while still using
   * the host platform's built-in trust store.
   *
   * <h3>Warning: Customizing Trusted Certificates is Dangerous!</h3>
   *
   * <p>Relying on your own trusted certificates limits your server team's ability to update their
   * TLS certificates. By installing a specific set of trusted certificates, you take on additional
   * operational complexity and limit your ability to migrate between certificate authorities. Do
   * not use custom trusted certificates in production without the blessing of your server's TLS
   * administrator.
   */
  private SSLContext trustManagerForCertificates(InputStream in)
          throws GeneralSecurityException, IOException {

    // Put the certificates a key store.
    char[] password = CLIENT_KET_PASSWORD.toCharArray(); // Any password will work.
    KeyStore keyStore = newEmptyKeyStore(password);
    keyStore.load(in,CLIENT_KET_PASSWORD.toCharArray());

    // Use it to build an X509 trust manager.
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(
        KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password);
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
      throw new IllegalStateException("Unexpected default trust managers:"
          + Arrays.toString(trustManagers));
    }

    SSLContext ssContext = SSLContext.getInstance("TLS");
    ssContext.init(keyManagerFactory.getKeyManagers(),trustManagers,null);
    return  ssContext;
  }

  private KeyStore newEmptyKeyStore(char[] password) throws GeneralSecurityException {
    try {
      KeyStore keyStore = KeyStore.getInstance("PKCS12");
      InputStream in = null; // By convention, 'null' creates an empty key store.
      keyStore.load(in, password);
      return keyStore;
    } catch (IOException e) {
      throw new AssertionError(e);
    }
  }

//  public static void main(String... args) throws Exception {
//    new CustomTrust().run();
//  }
}
ZBrush 2021来了 这次给大家带来的是:zbrush2021.1.1 win安装教程 1.下载压缩包之后解压到文件夹当中,然后打开ZBrush_2021_Installer.exe进行软件安装, 如下图 2.在这里我们保持默认的简体中文版即可,然后点击确认,如下图 3.第三步我们点击前进即可,如下图 4.第四步我们来到许可协议,在这里我们点击我接受此协议,然后点击前进,如下图 5.第五步我们来到安装目录,在这里我们可以设置安装路径,如下图 6.在这里我将安装目录修改安装到E盘为例,然后点击下一 阅读详情

相关推荐

从零到一:手把手教你用YonBuilder for NCC搭建NC Cloud 2021.11开发环境(含避坑指南)

本文详细指导如何使用YonBuilder for NCC搭建NC Cloud 2021.11开发环境,涵盖硬件要求、软件安装、YonBuilder配置及常见故障排除。特别提供避坑指南,帮助开发者快速上手NCC开发,确保环境配置的原子化分解和开发思维的体系化建立。

weixin_42523326的博客 360

Android 安全:HTTPS OKHTTP 域名与证书验证

作为前端开发,应该或多或少的都会熟悉https,特别是邻居家IOS,早就必须使用HTTPS了,Android也在9.0的时候增加了这一要求,当我们的targetSdkVersion指到9.0以上时,必须使用HTTPS。 HTTPS 简单来讲,HTTPS可以理解为 HTTP + SSL ,至于具体的HTTPS机制,涉及到一系列的加密,证书。。。。这边就不多做说明了,这里推荐郭神的一篇文章《写一篇最好懂的HTTPS讲解》,写的特别通俗易懂。 跳过HTTPS 这里简单说一句,Android 9.0 之后谷歌要.

阿东 6219

【processing】创意编程

首先接到这个作业的时候,我是很崩溃的,我是真的不会画画QAQ,还画自己,这也太难了,于是我决定!还是祸害我爱豆吧!于是我从微博太太那里找了个图临摹 下面是太太的图 先给太太道歉!都是为了作业! 然后我画出来是这样子的 还可以!!!没有特别丑!(虽然也不是特别像吧) 但是画这个真是画死我了,下面贴上代码吧,没啥特殊的就是各种贝塞尔曲线各种圆各种线 //face stroke(70,27,11...

qq_25700571的博客 1275

OKHttp请求https证书验证

拿到srca.cer证书放入assets文件中 private static SSLSocketFactory sslSocketFactory = null; public static void getSocketFactory() { try { InputStream certificate = MyApplication.mContext.getAssets...

1807

2024最新实战:用1Panel面板+Docker快速部署Frps服务端(含Nginx配置模板)

本文详细介绍了2024年企业级内网穿透方案,通过1Panel面板和Docker技术快速部署Frps服务端,包含Nginx反向代理配置模板。从服务器选型、系统准备到安全加固,提供全栈实战指南,帮助企业和开发者高效实现内网穿透需求。

weixin_27966471的博客 428

最新版Node.js下载安装及环境配置教程(非常详细)

选择对应你系统的Node.js版本。

DN金猿的博客 12万+

Okhttp3.0 添加SSL证书

Okhttp3.0 添加SSL证书

weixin_42602900的博客 2817

iOS自动化之打包-xcodebuild命令行打包.app并安装到ios模拟器

xcode xcodebuild xcrun ios模拟器 iOS UI自动化测试 ios自动打包安装到ios模拟器

z12347891的博客 5087

Android Https相关完全解析 当OkHttp遇到Https

转自: http://blog.csdn.net/lmj623565791/article/details/48129405; 本文出自:【张鸿洋的博客】 一、概述 其实这篇文章理论上不限于okhttp去访问自签名的网站,不过接上篇博文了,就叫这个了。首先要了解的事,okhttp默认情况下是支持https协议的网站的,比如https://www.baidu.com,https://...

iteye_7202的博客 2723

华为od 面试八股文_Python_05_含答案

华为od 面试八股文_Python_05_含答案

MISAYAONE的博客 2603

一文带你了解什么是RDMA

RDMA(Remote Direct Memory Access,远程直接内存访问)是一种起源于高性能计算领域的通信技术,如今已广泛应用于数据中心。该技术使得用户程序能够绕过操作系统内核及CPU,直接与网络接口卡(NIC)交互,实现网络通信。这种方式不仅大幅提升了通信带宽,还显著降低了网络延迟,优化了整体性能。

嵌入式技术开发 6029

Okhttp3 配置Https访问(使用PKCS12)证书

Okhttp3 访问Https,Https使用的证书格式为PKCS12

Lincoln 的专栏 1万+

动态SLAM中的YOLO与ORB-SLAM3融合:实时目标检测与动态点云滤除实战

本文介绍了如何结合YOLOv5目标检测与ORB-SLAM3视觉SLAM系统,实现动态环境下的目标检测与视觉SLAM任务。文章详细阐述了从源码下载、依赖安装(包括libtorch配置)、编译运行步骤(包括ROS环境和非ROS环境下的编译方法),到RGBD-TUM数据集准备与关联处理的全流程。同时针对常见错误提供了解决方案,如libtorch路径配置、ROS环境冲突等。最后展示了系统运行方法,并探讨了未来可能的优化方向,包括增强目标检测能力和改进动态环境下的地图构建。该集成系统为机器人智能导航和环境感知提供了有

LXY_LOVE36的博客 244

OkHttp3 配置Https证书请求

前言:又有一段时间没更新咯,也是自己实在是懒。也想到发布文章的话不知道该发布什么。就时间间隔了这么长的时间,这段时间稍微没那么忙了就抽空写篇文章记录一下。也不是什么高科技高级技术。好了废话不多说直接来个正文吧正文:直接上代码/** * 关联Https请求验证证书 * * @param okHttpClient */ public OkHt...

WL-鬼 1万+

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

OKHttp进行Https认证

OkHttpClient.Builder mOkHttpClient = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(6

就酱的博客 740

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

okhttp3 支持 https的坑

最开始由于第三方切换为https了,会出现 java.io.IOException: Hostname was not verified 所以要支持https了,这里碰到个okhttp3的坑,项目中okhttp的版本为4.2.0 <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> &lt...

luyee2010的专栏 2694

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Okhttp连接https添加自产证书

为什么80%的码农都做不了架构师?>>> ...

weixin_34126215的博客 430

【springboot进阶】RestTemplate 集成 okhttp3 请求带p12证书

以微信支付相关接口为例子,介绍 RestTemplate 集成 okhttp3 请求带p12证书,并针对不同的商户号的扩展使用。

3315

Spring Cloud Open Feign系列【4】集成OkHttp及连接池配置详解

OkHttp作为后期之秀,功能和性能上,可能稍优于HttpClient ,但是几乎没多大区别,实际使用时,都是可以的,不过HttpClient集成起来更方便。在HTTP 通信的过程中,建立连接是一个很复杂的过程,涉及到多个数据包的交换,很耗时间,而且HTTP连接需要3次握手和4次挥手开销都很大。在feign-okhttp-10.12.jar 中,提供了Client 的实现类,可以看到,执行请求时,使用的是OkHttp 中的API。.followRedirects(true) // 是否允许重定向。

竹林幽深 3530

OkHttp3.0 添加SSL证书信任

okhttp作为Android主要的网络请求框架之一,对okhttp的使用介绍网上资料也是一堆一堆的。okhttp一个简单的网络请求:Request request = new Request.Builder().get().url("https://www.baidu.com").build();OkHttpClient.Builder builder = new OkHttpClient.Bu...

NotFound的专栏 2万+

Okhttp 访问自签名证书 HTTPS

Okhttp 访问 HTTPS 链接问题 HTTPS 即以安全为目的的 HTTP 通道,即 HTTP 下加入 SSL 层,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL。一般情况下 CA 颁发的 https 证书是默认受浏览器信任的。okhttp框架也能直接访问这些网站拿到数据,但对于自签名证书,okhttp 默认是拒绝访问通过的。一般能直接访问的网站 Chrome 浏览器打...

zhengxtDgxs的博客 2935

https单向认证和双向认证demo(SpringBoot+okhttp3+keytool自签名)

目录 一、前言 二、软件版本 三、单向认证 四、双向认证 一、前言 HTTPS (全称:Hyper Text Transfer Protocol over SecureSocket Layer),是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性 [1] 。HTTPS 在HTTP 的基础下加入SSL 层,HTTPS 的安全基础是 S...

阿瑟与非 4074

Android Okhttp3.0及其以上版本忽略https证书(就是信任所有证书)

一开始工作做项目的时候,在小公司,都是使用http协议进行开发,后来来了大公司之后,发现他们用的https协议,但是Android使用https协议一般需要配置证书,经探索发现可以忽略https证书进行开发,与之不同的是iOS内置了很多信任的证书,所以iOS不需要做任何操作。2.通过这个类我们可以获得SSLSocketFactory,这个东西就是用来管理证书和信任证书的,然后还需要配置一个HostnameVerifier来忽略host验证,然后我们在Okhttp3中设置一下这两个属性。

UrasakiNakajima的博客 3690

Https系列之四:https的SSL证书在Android端基于okhttp,Retrofit的使用

https的SSL证书在Android端基于okhttp,Retrofit的使用

颜家大少的博客 1万+

当OkHttps遇到https请求

private static SSLSocketFactory getSocketFactory(List certificates) { try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); KeyStore keyStore = KeyStore.getIns

7415

okhttp支持https双向认证

搭建环境 服务端:dropwizard-jetty 准备文件:服务端秘钥d_server.jks,客户端公钥证书d_client_for_server.jks 客户端:retrofit / okhttp 准备文件:需要服务端证书d_server.cer,客户端私钥d_client.jks 生成证书 服务端,准备d_server.jks,d_client_for_server.jk...

u010199493的博客 4754
上一篇: java Android OKHttp HTTPS 请求证书验证 PEM证书(1)
下一篇: OKHttp HTTPS 请求支持所有Https服务器 证书验证忽略客户端证书,(3)
doubleping
博客等级 码龄18年 23粉丝 45原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址