跳到内容
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

来自 File System 插件的权限示例。

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