RN https 双向认证
一 概述
Rreact Native https双向认证 Android端修改,有两种方案:
一是修改 facebook桥接Android的网络请求
二是自己新建桥接 android和rn,修改android的https请求。
这里用到了第一种方法。第二种方法,有空的话可以试一下,也是可以的。
要想做RN的桥接,首先要先把android原生的 https双向认证搞明白,请看我上篇文章:
https://blog.csdn.net/BingHongChaZuoAn/article/details/93591447
二、实战操作
服务器端和Android端配置 就不讲了,上篇文章已经讲过。
1.首先是 新建 HttpsReactPackage,修改网络为自己的NetworkingModule
public class HttpsReactPackage extends MainReactPackage {private String TAG ="HttpsReactPackage";@Overridepublic List<ModuleSpec> getNativeModules(final ReactApplicationContext context) {List<ModuleSpec> retList = new ArrayList<>();List<ModuleSpec> superList = super.getNativeModules(context);for (ModuleSpec moduleSpec:superList){if (moduleSpec.getProvider().get() instanceof NetworkingModule){continue;}retList.add(moduleSpec);}retList.add(new ModuleSpec(NetworkingModule.class,new Provider<NativeModule>() {@Overridepublic NativeModule get() {return getTestNetWorkingMoudle(context);}}));return retList;}private NativeModule getTestNetWorkingMoudle(ReactApplicationContext context) {NetworkingModule networkingModule = new NetworkingModule(context);try {Field field = networkingModule.getClass().getDeclaredField("mClient");field.setAccessible(true);OkHttpClient okHttpClient2 = OKHttpClientUtils.getOkHttpClient(context);field.set(networkingModule,okHttpClient2);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} return networkingModule;}
}
2.然后 新建 OKHttpClientUtils,做okhttps证书的双向认证,并提供单例 okhttpClient
public class OKHttpClientUtils {private static @NullableOkHttpClient sClient;public static OkHttpClient getOkHttpClient(Context context) {if (sClient == null) {sClient = createClient(context);}return sClient;}// okhttp3 OkHttpClient is immutable// This allows app to init an OkHttpClient with custom settings.public static void replaceOkHttpClient(OkHttpClient client) {sClient = client;}public static OkHttpClient createClient(Context context) {// No timeouts by defaultHttpsUtils.SSLParams sslParams =null;try {sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{context.getAssets().open("server.crt")},context.getAssets().open("test_client.keystore"), "123456");} catch (IOException e) {e.printStackTrace();}OkHttpClient.Builder client = new OkHttpClient.Builder().connectTimeout(0, TimeUnit.MILLISECONDS).readTimeout(0, TimeUnit.MILLISECONDS).writeTimeout(0, TimeUnit.MILLISECONDS).cookieJar(new ReactCookieJarContainer()).sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager).hostnameVerifier(new HostnameVerifier() {@Overridepublic boolean verify(String hostname, SSLSession session) {return true;}});return enableTls12OnPreLollipop(client).build();}/*On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 areavailable but not enabled by default. The following methodenables it.*/public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {try {client.sslSocketFactory(new TLSSocketFactory());ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).tlsVersions(TlsVersion.TLS_1_2).build();List<ConnectionSpec> specs = new ArrayList<>();specs.add(cs);specs.add(ConnectionSpec.COMPATIBLE_TLS);specs.add(ConnectionSpec.CLEARTEXT);client.connectionSpecs(specs);} catch (Exception exc) {FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc);}}return client;}}
3.修改 application MainReactPackage 改为自己新建的package的加载
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {@Overridepublic boolean getUseDeveloperSupport() {return BuildConfig.DEBUG;}@Overrideprotected List<ReactPackage> getPackages() {return Arrays.<ReactPackage>asList(new HttpsReactPackage());}};
4. 到此fetch https双向认证封装已经完成 ,调用服务器代码如下:
fetch('https://192.168.43.238:8443/getUser').then((response) => response.json()).then((responseJson) => {var jsonStr=JSON.stringify(responseJson); console.warn(jsonStr);}).catch((error) => {console.error(error);});
RN https 双向认证相关推荐
- android webview单向认证,android 让webview支持自签名证书https 双向认证(SSL)
最近完成一个项目,安全级别比较高.所以涉及到https双向认证,在网上找了很多资料都没有完美的解决方案.最后参考了org.sandrob.sslexample的实现方式,结合实际情况才完成该技术难题, ...
- Apache httpd设置HTTPS双向认证
一.环境 httpd: 2.4.4 openssl:1.0.1 os:ubuntu 12.04 LTS 二.场景 我准备在httpd上配置一个HTTPS双向认证,既向客户端表明自己的身份,也只允许 ...
- TurboMail邮件系统支持HTTPS双向认证
2019独角兽企业重金招聘Python工程师标准>>> HTTP单向认证已经被普遍应用,而对企业邮箱安全保密要求更加严格的企事业单位,例如国家保密局等单位,为了达到闭环的安全加密要求 ...
- httpd设置HTTPS双向认证
去年用tomcat.jboss配置过HTTPS双向认证,那时候主要用的是JDK自带的keytool工具.这次是用httpd + openssl,区别比较大 在网上搜索了很多文章,发现全面介绍的不多,或 ...
- 用pfx证书java双向认证_把CA证书生成的crt的证书和pem的私钥转换成java能够使用的keystore和pcks12的证书,实现https双向认证...
最近在做一个https双向认证的工作,领导先让我实现,我之前写了一篇文章,把tomcat的生成证书和配置的实现写了出来. 现在领导给了我服务器的CA证书的客户端证书和私钥,服务端信任证书,分别是crt ...
- 巧用 Nginx 快速实现 HTTPS 双向认证
1.原理 双向认证,顾名思义,客户端和服务器端都需要验证对方的身份,在建立 HTTPS 连接的过程中,握手的流程比单向认证多了几步.单向认证的过程,客户端从服务器端下载服务器端公钥证书进行验证,然后建 ...
- tomcat实现https双向认证(在win10系统使用jdk1.8自带工具keytool)
tomcat实现https双向认证(在win10系统使用jdk1.8自带工具keytool) 以下操作我在D:\shiro-cas-ssl进行操作 使用win+R运行certmgr.msc可以查看证书 ...
- HTTPS双向认证(Mutual TLS authentication)
HTTPS双向认证(Mutual TLS authentication) 双向认证,顾名思义,客户端和服务器端都需要验证对方的身份,在建立Https连接的过程中,握手的流程比单向认证多了几步.单向认证 ...
- 证书类型、自签CA证书、https双向认证(一篇就懂系列)
#博学谷IT学习技术支持# 文章目录 1.Linux准备环境 2.证书扩展名 3.自签CA证书 3.1 生成根证书 3.2 生成服务端证书 3.3 生成客户端证书 4.开启https,并校验客户端(双 ...
最新文章
- Java练习 SDUT-2401最大矩形面积
- webapi同一个Controller多个函数
- Web常见攻击手段总结
- 怎么做手机的上下滑动_手机视频恢复怎么做?删除时间较久的找回方法
- [转载] Python Set intersection() 方法
- C#之判断Mysql数据库是否存在
- react的导出是怎么实现的_不到一百行代码,我们来实现一个简简简简简简简简简简版react库...
- Microsoft SQL Server数据库学习(一)
- URL地址相对路径转绝对路径
- 毕设题目:Matlab语音情感识别
- 鸿蒙系统简介ppt,鸿蒙来了!华为到底采用的是什么逆天的研发体系?500页PPT详解...
- 小米max2装鸿蒙,小米Max2最全评测 小米Max2值不值得买?
- (一)CGAL库应用:指定平面切割模型并用openGL显示该层面轮廓
- Linux下安装神通数据库
- 拉钩网招聘信息爬虫项目
- Python实现csv与excel互转
- 这几个画流程图的软件值得你们收藏
- Elasticsearch启动问题:max number of threads [XXX] for user [XX] is too low, increase to at least [4096]
- Android网卡网速测试
- 利用U盘重新安装XP系统