我需要幫助使用graphql_flutter和http_certificate_pinning在 Flutter 上實作 SSL 固定。到目前為止,這是我的代碼實作:
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:http_certificate_pinning/http_certificate_pinning.dart';
class Service {
final List<String> _allowedSHAFingerprints;
late GraphQLClient _gqlClient;
GraphQLClient get gqlClient => _gqlClient;
late SecureHttpClient _secureHttpClient;
SecureHttpClient get secureHttpClient => _secureHttpClient;
Service(this._allowedSHAFingerprints) {
_secureHttpClient = SecureHttpClient.build(_allowedSHAFingerprints);
final httpLink = HttpLink(
'https://dummy.com/graphql/',
httpClient: _secureHttpClient,
);
_gqlClient = GraphQLClient(
link: httpLink,
cache: GraphQLCache(),
);
}
}
gqlClient即使allowedSHAFingerprints無效,問題總是回傳連接成功(安全)。我嘗試使用 HTTP GET 方法secureHTTPClient,它運行良好。我在這里做錯了嗎?
我是這樣稱呼它的:
Future<void> _gqlCall() async {
try {
final secureClient = GetIt.I<Service>().gqlClient;
final options = QueryOptions(
document: gql(homePageQuery),
);
final result = await secureClient.query(options);
if (!result.hasException) {
_showSnackbar('GQL Success');
} else {
throw Exception();
}
} on Exception catch (_) {
_showSnackbar('GQL Fail');
}
}
下面的代碼按我的預期作業:
Future<void> _apiCall() async {
try {
final url = Uri.parse('https://dummy.com/ping');
final result = await GetIt.I<Service>().secureHttpClient.get(url);
if (result.statusCode == 200) {
_showSnackbar('API Success');
} else {
throw Exception();
}
} on Exception catch (_) {
_showSnackbar('API Fail');
}
}
uj5u.com熱心網友回復:
終于解決了這個問題。
我正在使用dio和另一個名為gql_dio_link 的庫
這是我Service現在的類實作:
import 'package:dio/dio.dart';
import 'package:gql_dio_link/gql_dio_link.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:http_certificate_pinning/http_certificate_pinning.dart';
class Service {
final List<String> _allowedSHAFingerprints;
late GraphQLClient _secureGqlClient;
GraphQLClient get secureGqlClient => _secureGqlClient;
late Dio _secureDioClient;
Dio get secureDioClient => _secureDioClient;
Service(this._allowedSHAFingerprints) {
_secureDioClient = Dio(BaseOptions(baseUrl: 'https://dummy.com'))
..interceptors.add(CertificatePinningInterceptor(
allowedSHAFingerprints: _allowedSHAFingerprints));
final link = Link.from([
DioLink(
'/graphql',
client: _secureDioClient,
),
]);
_secureGqlClient = GraphQLClient(
link: link,
cache: GraphQLCache(),
);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/489975.html
標籤:扑 http 图形 sslpinning
上一篇:AndroidHttpURLConnection:HTTP回應總是403
下一篇:如何模擬.NETHTTP回應?
