跳到内容
Tauri

权限

权限是对命令显式特权的描述。

[[permission]]
identifier = "my-identifier"
description = "This describes the impact and more."
commands.allow = [
"read_file"
]
[[scope.allow]]
my-scope = "$HOME/*"
[[scope.deny]]
my-scope = "$HOME/secret"

它可以使命令在 Tauri 应用程序的前端可访问。它可以将作用域映射到命令,并定义哪些命令被启用。权限可以启用或拒绝某些命令,定义作用域或将两者结合。

要授予或拒绝应用程序的窗口或 webview 的权限,您必须在能力中引用该权限。

权限可以分组为一个集合,并使用新的标识符。这被称为权限集。这允许您将作用域相关的权限与命令相关的权限结合起来。它还允许将特定操作系统的权限分组或捆绑成更易于使用的集合。

作为插件开发者,您可以为您所有公开的命令提供多个预定义的、命名良好的权限。

作为应用程序开发者,您可以扩展现有的插件权限,或者为您自己的命令定义权限。它们可以分组或扩展到一个集合中,以便重用或简化以后的主配置文件。

权限标识符

权限标识符用于确保权限可以重用并具有唯一的名称。

  • <name>:default 表示该权限是插件或应用程序的默认权限
  • <name>:<command-name> 表示该权限是针对单个命令的

插件前缀 tauri-plugin- 将在编译时自动添加到插件的标识符中,不需要手动指定。

标识符仅限于 ASCII 小写字母字符 [a-z],并且由于以下常量,标识符的最大长度目前限制为 116

const IDENTIFIER_SEPARATOR: u8 = b':';
const PLUGIN_PREFIX: &str = "tauri-plugin-";
// https://doc.rust-lang.net.cn/cargo/reference/manifest.html#the-name-field
const MAX_LEN_PREFIX: usize = 64 - PLUGIN_PREFIX.len();
const MAX_LEN_BASE: usize = 64;
const MAX_LEN_IDENTIFIER: usize = MAX_LEN_PREFIX + 1 + MAX_LEN_BASE;

配置文件

简化的 Tauri 插件目录结构示例

终端窗口
tauri-plugin
├── README.md
├── src
└── lib.rs
├── build.rs
├── Cargo.toml
├── permissions
└── <identifier>.json/toml
└── default.json/toml

默认权限以特殊方式处理,因为它会自动添加到应用程序配置中,只要使用 Tauri CLI 将插件添加到 Tauri 应用程序。

对于应用程序开发者,结构是类似的

终端窗口
tauri-app
├── index.html
├── package.json
├── src
├── src-tauri
├── Cargo.toml
├── permissions
└── <identifier>.toml
| ├── capabilities
└── <identifier>.json/.toml
├── src
├── tauri.conf.json

示例

来自文件系统插件的权限示例。

plugins/fs/permissions/autogenerated/base-directories/home.toml
[[permission]]
identifier = "scope-home"
description = """This scope permits access to all files and
list content of top level directories in the `$HOME`folder."""
[[scope.allow]]
path = "$HOME/*"
plugins/fs/permissions/read-files.toml
[[permission]]
identifier = "read-files"
description = """This enables all file read related
commands without any pre-configured accessible paths."""
commands.allow = [
"read_file",
"read",
"open",
"read_text_file",
"read_text_file_lines",
"read_text_file_lines_next"
]
plugins/fs/permissions/autogenerated/commands/mkdir.toml
[[permission]]
identifier = "allow-mkdir"
description = "This enables the mkdir command."
commands.allow = [
"mkdir"
]

在您的应用程序中扩展上述插件权限的示例实现

my-app/src-tauri/permissions/home-read-extends.toml
[[set]]
identifier = "allow-home-read-extended"
description = """ This allows non-recursive read access to files and to create directories
in the `$HOME` folder.
"""
permissions = [
"fs:read-files",
"fs:scope-home",
"fs:allow-mkdir"
]

© 2025 Tauri 贡献者。CC-BY / MIT