flutter/dart 文件夹中的 Assets 文件夹,用于客户端证书认证
Assets Folder in flutter/ dart folder for client certificate authentication
我正在尝试在 dart 中进行客户端证书验证。但是当我用 flutter 编译应用程序时,我一直找不到 cert.p12。这是我进行证书认证的方式。
HttpClient createHttpClient(SecurityContext context) {
final cert = "assets/cert.p12";
final password = "mittal";
final sc = SecurityContext.defaultContext
..useCertificateChain(cert, password: password)
..usePrivateKey(cert, password: password);
return super.createHttpClient(sc)
我的assets文件夹在项目的根目录下,cert.p12在里面。请帮忙。
这里是android工作室的截图:
The Screenshot
我已经阅读了https://flutter.dev/docs/development/ui/assets-and-images并添加了,但仍然是同样的问题。请帮忙
flutter:
assets:
- assets/
- assets/cert.p12
你居然这么说:
String cert = "assets/cert.p12"
<-- 你不加载证书,你创建一个字符串。
这是一个对我有用的例子:
final clientCert = await rootBundle.load('assets/cert.p12');
context.useCertificateChainBytes(clientCert.buffer.asUint8List(), password: password);
这是一个完整的例子:
class MyHttpOverrides extends HttpOverrides {
ByteData clientCert;
ByteData keystore;
MyHttpOverrides({
required this.clientCert,
required this.keystore,
});
@override
HttpClient createHttpClient(SecurityContext? context) {
context = SecurityContext.defaultContext;
bool _certificateCheck(truststore, String host, int port) {
return host == "test.backend.com";
}
String password = "password";
context.useCertificateChainBytes(clientCert.buffer.asUint8List(), password: password);
context.usePrivateKeyBytes(keystore.buffer.asUint8List(), password: password);
HttpClient client = super.createHttpClient(context)..badCertificateCallback = (_certificateCheck);
client..badCertificateCallback = (_certificateCheck);
return client;
}
}
并像这样使用它:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final clientCert = await rootBundle.load('assets/cert.pem');
final keystore = await rootBundle.load('assets/key.pem');
HttpOverrides.global = MyHttpOverrides(
clientCert: clientCert,
keystore: keystore,
);
runApp(MyApp());
}
这些是进口商品:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
我正在尝试在 dart 中进行客户端证书验证。但是当我用 flutter 编译应用程序时,我一直找不到 cert.p12。这是我进行证书认证的方式。
HttpClient createHttpClient(SecurityContext context) {
final cert = "assets/cert.p12";
final password = "mittal";
final sc = SecurityContext.defaultContext
..useCertificateChain(cert, password: password)
..usePrivateKey(cert, password: password);
return super.createHttpClient(sc)
我的assets文件夹在项目的根目录下,cert.p12在里面。请帮忙。
这里是android工作室的截图:
The Screenshot
我已经阅读了https://flutter.dev/docs/development/ui/assets-and-images并添加了,但仍然是同样的问题。请帮忙
flutter:
assets:
- assets/
- assets/cert.p12
你居然这么说:
String cert = "assets/cert.p12"
<-- 你不加载证书,你创建一个字符串。
这是一个对我有用的例子:
final clientCert = await rootBundle.load('assets/cert.p12');
context.useCertificateChainBytes(clientCert.buffer.asUint8List(), password: password);
这是一个完整的例子:
class MyHttpOverrides extends HttpOverrides {
ByteData clientCert;
ByteData keystore;
MyHttpOverrides({
required this.clientCert,
required this.keystore,
});
@override
HttpClient createHttpClient(SecurityContext? context) {
context = SecurityContext.defaultContext;
bool _certificateCheck(truststore, String host, int port) {
return host == "test.backend.com";
}
String password = "password";
context.useCertificateChainBytes(clientCert.buffer.asUint8List(), password: password);
context.usePrivateKeyBytes(keystore.buffer.asUint8List(), password: password);
HttpClient client = super.createHttpClient(context)..badCertificateCallback = (_certificateCheck);
client..badCertificateCallback = (_certificateCheck);
return client;
}
}
并像这样使用它:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final clientCert = await rootBundle.load('assets/cert.pem');
final keystore = await rootBundle.load('assets/key.pem');
HttpOverrides.global = MyHttpOverrides(
clientCert: clientCert,
keystore: keystore,
);
runApp(MyApp());
}
这些是进口商品:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';