Flutter安全防护插件siarashield_flutter的使用
Flutter安全防护插件siarashield_flutter的使用
siarashield_flutter
是一个在 Flutter 应用程序中使用 SiaraShield 进行认证的包。
开始使用
要使用此包,在您的 pubspec.yaml
文件中添加 siarashield_flutter
作为依赖项。
安装
在您的 pubspec.yaml
文件中添加以下行:
dependencies:
siarashield_flutter: latest_version
然后运行:
flutter pub get
使用
要使用 SiaraShield 进行认证,您需要一个 MasterUrlId
,该 ID 可以从 SiaraShield Portal 注册您的包名后获得。
最小化示例
import 'package:siarashield_flutter/siarashield_flutter.dart';
SaraShieldWidget(
loginTap: (bool isSuccess) {
if (isSuccess) {
// 处理成功的认证
print("认证成功: \$isSuccess");
} else {
// 处理认证失败
print("认证失败");
}
},
cieraModel: CyberCieraModel(
masterUrlId: 'TEST-CYBERSIARA', // 主URL ID
requestUrl: 'com.app.testapp', // 包名称
privateKey: 'TEST-CYBERSIARA', // 私钥
),
),
参数
参数名 | 类型 | 描述 |
---|---|---|
loginTap |
函数 | 返回 true 表示认证成功,返回 false 表示认证失败的回调函数。 |
masterUrlId |
字符串 | 从 SiaraShield Portal 获得的主 URL ID。 |
requestUrl |
字符串 | 在 SiaraShield 中注册的包名称。 |
privateKey |
字符串 | 用于认证的私钥。 |
其他信息
更多详细信息,请访问 SiaraShield Portal 或参考官方文档。
注意: 确保您安全地处理认证响应,并遵循存储应用程序敏感凭据的最佳实践。
以下是完整的示例代码,展示了如何在 Flutter 应用程序中使用 siarashield_flutter
。
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siarashield_flutter/common/common_widgets.dart';
import 'package:siarashield_flutter/common/extension_widget.dart';
import 'package:siarashield_flutter/constants/app_constant.dart';
import 'package:siarashield_flutter/siarashield_flutter.dart';
import 'package:siarashield_flutter_example/second_screen.dart';
import 'app_colors.dart';
void main() {
runApp(const GetMaterialApp(home: MyApp()));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final TextEditingController _txtUsername = TextEditingController();
final TextEditingController _password = TextEditingController();
final border = OutlineInputBorder(borderRadius: BorderRadius.circular(5), borderSide: const BorderSide(color: AppColors1.greyColor));
final TextStyle _hintStyle = const TextStyle(color: AppColors1.greyColor, fontSize: 16, fontWeight: FontWeight.w500);
final TextStyle _labelStyle = const TextStyle(color: AppColors1.blueColor, fontSize: 16, fontWeight: FontWeight.w500);
final TextStyle _subtitle = const TextStyle(color: AppColors1.greyColor, fontSize: 15, fontWeight: FontWeight.w400);
bool isCheck = false;
bool isSlide = false;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: AppColors1.whiteColor,
body: Container(
margin: EdgeInsets.only(top: screenHeight(context) * 0.15, right: 15, left: 15),
decoration: const BoxDecoration(
color: AppColors1.whiteColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
topRight: Radius.circular(5),
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5),
)),
child: Column(
children: [
Center(
child: Image.asset(
ImageAssets.logo,
scale: 1,
package: 'siarashield_flutter',
)),
const SizedBox(
height: 15,
),
TextFormField(
controller: _txtUsername,
cursorColor: AppColors1.blackColor,
style: _labelStyle,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(left: 10),
disabledBorder: border,
errorBorder: border,
focusedBorder: border,
enabledBorder: border,
hintText: "Example@email.com",
hintStyle: _hintStyle,
labelText: "Email",
labelStyle: _labelStyle,
suffixIcon: Icon(
Icons.email_outlined,
color: AppColors1.yellowColor,
))).putPadding(15, 15),
const SizedBox(
height: 15,
),
TextFormField(
controller: _password,
obscureText: true,
cursorColor: AppColors1.blackColor,
style: _labelStyle,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
contentPadding: const EdgeInsets.only(left: 10),
disabledBorder: border,
errorBorder: border,
focusedBorder: border,
enabledBorder: border,
hintText: "*******",
hintStyle: _hintStyle,
labelText: "Password",
labelStyle: _labelStyle,
suffixIcon: Icon(
Icons.lock,
color: AppColors1.yellowColor,
))).putPadding(15, 15),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Checkbox(
value: isCheck,
onChanged: (val) {
isCheck = !isCheck;
setState(() {});
}),
Text(
"记住密码",
style: _subtitle,
)
],
).putPadding(15, 15),
Padding(
padding: const EdgeInsets.all(15.0),
child: SaraShieldWidget(
loginTap: (bool isSuccess) {
if (isSuccess) {
// 处理成功的认证
log("认证成功: \$isSuccess");
if (_txtUsername.text.isEmpty) {
toast("邮箱不能为空");
return;
}
if (_password.text.isEmpty) {
toast("密码不能为空");
return;
}
Get.to(() => const SecondScreen());
} else {
// 处理认证失败
log("认证失败");
}
},
cieraModel: CyberCieraModel(
masterUrlId: 'VYz433DfqQ5LhBcgaamnbw4Wy4K9CyQT', // 主 URL ID
requestUrl: 'com.app.cyber_ceiara', // 包名称
privateKey: "1AnVf4WsYsbyDRflfBInOe42vTnnMxbu", // 私钥
),
),
),
],
),
),
);
}
}
更多关于Flutter安全防护插件siarashield_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter安全防护插件siarashield_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
Siarashield 是一个用于 Flutter 应用的安全防护插件,旨在帮助开发者保护他们的应用免受各种安全威胁。siarashield_flutter
插件提供了一系列功能,如防篡改、防调试、防注入等,以确保应用的安全性。
以下是使用 siarashield_flutter
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 siarashield_flutter
插件的依赖:
dependencies:
flutter:
sdk: flutter
siarashield_flutter: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化插件
在应用启动时,初始化 siarashield_flutter
插件。通常可以在 main()
函数中进行初始化:
import 'package:siarashield_flutter/siarashield_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Siarashield
await Siarashield.init();
runApp(MyApp());
}
3. 配置安全选项
你可以根据应用的需求配置不同的安全选项。例如,启用防调试、防篡改等功能:
await Siarashield.configure(
enableAntiDebug: true,
enableAntiTamper: true,
enableAntiInject: true,
);
4. 检测安全威胁
你可以在应用的各个阶段检测是否存在安全威胁。例如,在用户登录时检查应用是否被篡改:
bool isTampered = await Siarashield.isTampered();
if (isTampered) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Security Alert'),
content: Text('The app has been tampered with.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'),
),
],
),
);
}
5. 处理安全事件
你还可以监听安全事件并做出相应的处理。例如,当检测到调试器附加时,可以立即终止应用:
Siarashield.onDebugDetected(() {
// 处理调试器检测到的事件
print('Debugger detected!');
// 终止应用
exit(0);
});
6. 其他功能
siarashield_flutter
还提供了其他一些功能,如检测设备是否越狱、检测应用是否运行在模拟器等。你可以根据需要使用这些功能来增强应用的安全性。
bool isJailbroken = await Siarashield.isJailbroken();
bool isEmulator = await Siarashield.isEmulator();