跳到内容
Tauri

生物识别

提示用户在 Android 和 iOS 上进行生物识别身份验证。

支持的平台

此插件需要 Rust 版本至少为 1.77.2

平台 级别 注释
windows
linux
macos
android
ios

设置

安装生物识别插件即可开始使用。

使用项目的包管理器添加依赖项

npm run tauri add biometric

配置

在 iOS 上,生物识别插件需要 NSFaceIDUsageDescription 信息属性列表值,该值应描述您的应用为何需要使用生物识别身份验证。

src-tauri/Info.ios.plist 文件中,添加以下代码段

src-tauri/Info.ios.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSFaceIDUsageDescription</key>
<string>Authenticate with biometric</string>
</dict>
</plist>

使用方法

此插件使您能够验证设备上生物识别身份验证的可用性,提示用户进行生物识别身份验证,并检查结果以确定身份验证是否成功。

检查状态

您可以检查生物识别身份验证的状态,包括其可用性和支持的生物识别身份验证方法的类型。

import { checkStatus } from '@tauri-apps/plugin-biometric';
const status = await checkStatus();
if (status.isAvailable) {
console.log('Yes! Biometric Authentication is available');
} else {
console.log(
'No! Biometric Authentication is not available due to ' + status.error
);
}

身份验证

要提示用户进行生物识别身份验证,请使用 authenticate() 方法。

import { authenticate } from '@tauri-apps/plugin-biometric';
const options = {
// Set true if you want the user to be able to authenticate using phone password
allowDeviceCredential: false,
cancelTitle: "Feature won't work if Canceled",
// iOS only feature
fallbackTitle: 'Sorry, authentication failed',
// Android only features
title: 'Tauri feature',
subtitle: 'Authenticate to access the locked Tauri function',
confirmationRequired: true,
};
try {
await authenticate('This feature is locked', options);
console.log(
'Hooray! Successfully Authenticated! We can now perform the locked Tauri function!'
);
} catch (err) {
console.log('Oh no! Authentication failed because ' + err.message);
}

权限

默认情况下,所有潜在危险的插件命令和作用域都被阻止且无法访问。您必须修改 capabilities 配置中的权限才能启用这些。

有关更多信息,请参阅功能概述,以及使用插件权限的分步指南

src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "main-capability",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": ["biometric:default"]
}

默认权限

此权限集配置默认情况下公开哪些生物识别功能。

已授予的权限

它允许访问所有生物识别命令。

此默认权限集包括以下内容

  • allow-authenticate
  • allow-status

权限表

标识符 描述

biometric:allow-authenticate

启用 authenticate 命令,无需任何预配置的作用域。

biometric:deny-authenticate

拒绝 authenticate 命令,无需任何预配置的作用域。

biometric:allow-status

启用 status 命令,无需任何预配置的作用域。

biometric:deny-status

拒绝 status 命令,无需任何预配置的作用域。


© 2025 Tauri Contributors. CC-BY / MIT