- 版本:1.1.15
- GitHub:
- NPM: https://npmjs.net.cn/package/nativescript-ssl-pinning
- 下载
- 昨天:0
- 上周:20
- 上个月:52
NativeScript-SSL-Pinning
默认 http 模块的替代品。
注意:此插件受 nativescript-ssl-pinning 的启发。大部分代码来自此。我添加了 angular 支持,并修复了一些长期问题。向原始创建者表示衷心感谢。
以下是一些已修复的问题:
- 不支持通配符证书。
- nativescript-https 模块未处理任何错误响应(400 - 500)。
- 开箱即用的 angular 支持,无需更新旧应用程序,只需更新 HttpClient 模块导入。
- 添加了对 multipart 表单的支持。
功能
- 现代 TLS & SSL 安全功能
- 共享连接池减少请求延迟
- 静默恢复常见的连接问题
- 所有操作都在原生后台线程上运行
- 透明的 GZIP
- HTTP/2 支持
常见问题解答
SSL 固定是什么?以及所有这些安全术语是什么意思?
我必须使用 SSL 固定吗?
不。 此插件开箱即用,无需任何安全配置即可运行。无论如何,您仍然可以受益于上述所有功能。
演示
git clone https://github.com/sai-gmbh/nativescript-ssl-pinning
cd nativescript-ssl-pinning/src
npm run demo.ios
npm run demo.android
npm run demo-angular.ios
npm run demo-angular.android
安装
将 tns-platform-declarations
添加到您的 references.d.ts
中,以支持 Android 和 iOS!
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
我们还建议将 "skipLibCheck": true,
添加到您的 tsconfig.json
中。有关更多信息,请参阅 此处。
安装插件
tns plugin add nativescript-ssl-pinning
示例
使用 GET
方法调用 API
import { SslPinning } from 'nativescript-ssl-pinning'
SslPinning.request({
url: 'https://httpbin.org/get',
method: 'GET',
})
.then((response) => console.log('response', response))
.catch((error) => console.error('error', error));
Angular 支持
NativescriptSslPinningHttpClientModule 内部覆盖 Angular 的 XHRBackend 以通过我们的 SSL 插件发出请求,并将其转换回 angular 响应。这将保持拦截器功能完好无损。
import {NativescriptSslPinningHttpClientModule} from "nativescript-ssl-pinning/angular"
@NgModule({
imports: [
// ...
NativescriptSslPinningHttpClientModule
],
declarations: [
ExampleComponent,
],
schemas: [
NO_ERRORS_SCHEMA
]
})
export class AppModule {
}
@Component({...})
export class ExampleComponent implements OnInit {
constructor(private http: HttpClient) {}
ngOnInit() {
this.http.get('https://httpbin.org/status/500').subscribe(res => console.log(res), err => console.log(err));
}
}
配置
安装您的 SSL 证书
在项目的 app
文件夹中创建一个名为 assets
的文件夹,例如 <project>/app/assets
。
启用 SSL 固定
import { knownFolders } from 'file-system'
import { SslPinning } from 'nativescript-ssl-pinning'
let dir = knownFolders.currentApp().getFolder('assets')
let certificate = dir.getFile('httpbin.org.cer').path
SslPinning.enableSSLPinning({ host: 'httpbin.org', certificate })
一旦启用了 SSL 固定,您就不能使用不同的 host
或 certificate
文件重新启用。
禁用 SSL 固定
import { SslPinning } from 'nativescript-ssl-pinning'
SslPinning.disableSSLPinning()
调用此方法之后的所有请求将不再使用 SSL 固定,直到再次启用。
选项
export interface HttpsSSLPinningOptions {
host: string
certificate: string
allowInvalidCertificates?: boolean
validatesDomainName?: boolean
}
选项 | 描述 |
---|---|
host: 字符串 |
这必须是顶级域名,例如 httpbin.org 。 |
certificate: 字符串 |
您的 .cer 证书文件的 uri 路径。 |
allowInvalidCertificates?: 布尔值 |
默认值:false 。如果您使用 SSL 固定,则此值应始终为 false 。如果您使用自签名证书,则将其设置为 true 。 |
validatesDomainName?: 布尔值 |
默认值:true 。确定是否应使用您的固定证书验证域名。 |
Webpack / 打包
由于您可能将证书与您的应用程序一起分发,因此请确保它也被 Webpack 打包。您可以通过添加证书(s)与 CopyWebpackPlugin
来完成此操作。
new CopyWebpackPlugin([
{ from: { glob: "fonts/**" } },
{ from: { glob: "**/*.jpg" } },
{ from: { glob: "**/*.png" } },
{ from: { glob: "**/*.cer" } }, // add this line in webpack.config.js
], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] })
iOS
故障排除
在开始加强之前,请先了解一下 iOS 的 App Transport Security。
如果您尝试访问一个未添加到App Transport Security白名单的https
路由,它将无法工作!您可以通过在项目的Info.plist
中添加以下内容来绕过这种行为:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
此插件不会自动将
NSAllowsArbitraryLoads
添加到您的项目的Info.plist
中。
Android
故障排除
如果您的应用程序崩溃并显示它在主线程上执行了过多的网络操作,那么请将具有值true
的选项allowLargeResponse
传递给request
函数。
当前问题
- 插件目前不支持多部分表单请求。对于Angular用户,它们将通过Angular自己的XHR进行,但对于本地用户可能会失败。
感谢
谁 | 为什么 |
---|---|
罗伯特·拉弗蒂 | 在将其转交给我的帮助下,罗伯特·拉弗蒂长时间创建并维护此插件,这得益于杰夫·惠普利的帮助,杰夫是GetHuman的成员。 |
AFNetworking | AFNetworking 一个令人愉悦的iOS、OS X、watchOS和tvOS的网络框架。 |
Square | okhttp Android和Java应用程序的HTTP+HTTP/2客户端。 |