跳到正文
LICENTIVO / INTEGRATION

Tauri 軟體授權:整合 Rust 後端

在 Tauri 2 桌面應用程式使用 Licentivo Rust SDK 接入軟體授權。建置 Windows x64 範例並保護匯出命令。

約 8 分鐘更新日期

在 Windows 建立 Tauri 2 專案

安裝 WebView2 執行環境、Rust 及 MinGW-w64 工具鏈。SDK 0.11.9 在 Windows 使用 x86_64-pc-windows-gnu 目標及相符的共享 libcurl 開發套件。按實際位置調整下方路徑。

Windows · PowerShell
npm create tauri-app@4.7.5 licensed-tauri -- --template vanilla --identifier com.example.licensedtauri --manager npm --yes
cd licensed-tauri
npm install
rustup toolchain install stable-x86_64-pc-windows-gnu
$env:RUSTUP_TOOLCHAIN = 'stable-x86_64-pc-windows-gnu'
$MinGW = 'C:\path\to\mingw64'
$env:LICENTIVO_CURL_ROOT = 'C:\path\to\curl-dev'
$env:CC = Join-Path $MinGW "bin\gcc.exe"
$env:CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER = $env:CC
$env:Path = "$MinGW\bin;$env:LICENTIVO_CURL_ROOT\bin;$env:Path"
cargo add --manifest-path src-tauri/Cargo.toml "licentivo@=0.11.8"

在 Rust 命令驗證授權

替換下列三個檔案,並在 lib.rs 填入產品 ID 和所有簽章公鑰。WebView 呼叫 export_report,Rust 在確認 export 權限後才寫到應用程式的本機資料目錄。

Rust · src-tauri/src/lib.rs
use licentivo::Client;
use std::{path::PathBuf, sync::Mutex};
use tauri::{AppHandle, Manager, RunEvent, WebviewWindow};

const PRODUCT_ID: &str = "YOUR_PRODUCT_ID";
const TRUSTED_KEYS: &str = r#"{"YOUR_KEY_ID":"YOUR_PUBLIC_KEY"}"#;

struct Licensing {
    client: Mutex<Option<Client>>,
    report: PathBuf,
}

#[tauri::command]
async fn export_report(app: AppHandle, window: WebviewWindow) -> Result<(), String> {
    if window.label() != "main" {
        return Err("This window cannot export reports.".into());
    }

    tauri::async_runtime::spawn_blocking(move || {
        let state = app.state::<Licensing>();
        let mut guard = state.client.lock().map_err(|_| "License client unavailable.")?;
        let client = guard.as_mut().ok_or("License client is closed.")?;
        client.run_feature("export", || {
            std::fs::write(&state.report, "Licensed export\n")?;
            Ok(())
        }).map_err(|_| "Export denied or unavailable.".to_owned())
    }).await.map_err(|_| "The export task did not complete.")?
}

pub fn run() {
    let app = tauri::Builder::default()
        .setup(|app| {
            let key = std::env::var("LICENTIVO_LICENSE_KEY").unwrap_or_default();
            let client = Client::open_product(PRODUCT_ID, TRUSTED_KEYS, &key)?;
            let directory = app.path().app_local_data_dir()?;
            std::fs::create_dir_all(&directory)?;
            app.manage(Licensing {
                client: Mutex::new(Some(client)),
                report: directory.join("licensed-report.txt"),
            });
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![export_report])
        .build(tauri::generate_context!())
        .expect("Could not start the licensed application");

    app.run(|app, event| {
        if matches!(event, RunEvent::Exit) {
            let state = app.state::<Licensing>();
            if let Ok(mut client) = state.client.lock() {
                client.take();
            };
        }
    });
}
HTML · src/index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Licensed export</title>
    <script src="./main.js" type="module"></script>
  </head>
  <body>
    <h1>Licensed export</h1>
    <button type="button" id="export-report">Export report</button>
    <p id="result" role="status"></p>
  </body>
</html>
JavaScript · src/main.js
const button = document.querySelector("#export-report");
const result = document.querySelector("#result");

button.addEventListener("click", async () => {
  button.disabled = true;
  try {
    await window.__TAURI__.core.invoke("export_report");
    result.textContent = "Report saved in application data.";
  } catch (error) {
    result.textContent = String(error);
  } finally {
    button.disabled = false;
  }
});

建置與驗證 Windows 應用程式

使用允許有限離線且含 export 的固定裝置測試授權。執行 Release 檔案,點選 Export report,關閉後清除授權碼並重啟。另以缺少 export 的授權及新測試目錄驗證拒絕。保留執行檔旁的 WebView2Loader.dll 並提供相符的 libcurl DLL。

2026-11-19 在 Windows x64 以 Tauri 2.12.7、WebView2 和 SDK 0.11.8 驗證。Release 執行檔於啟用後及無授權碼重啟後均可匯出。缺少 export 時顯示拒絕提示且未產生檔案。

Windows · PowerShell
npm run tauri build -- --target x86_64-pc-windows-gnu --no-bundle
if ($LASTEXITCODE -ne 0) { throw 'Tauri build failed.' }
$env:LICENTIVO_LICENSE_KEY = 'YOUR_LICENSE_KEY'
$app = ".\src-tauri\target\x86_64-pc-windows-gnu\release\licensed-tauri.exe"
Start-Process -FilePath (Resolve-Path $app).Path -Wait
Remove-Item Env:LICENTIVO_LICENSE_KEY
Start-Process -FilePath (Resolve-Path $app).Path -Wait

Rust 軟體授權:授權碼啟用與功能驗證

在 Rust 程式整合 Licentivo 軟體授權 SDK,實作授權金鑰驗證、裝置綁定與功能權限檢查。教學涵蓋安裝、啟用、離線驗證及錯誤處理,協助你在實際功能入口檢查授權。

C++ 離線授權啟用:隔離網路整合教學

為隔離網路中的 C++ 軟體實作離線授權啟用。透過申請檔與回應檔交換完成簽章驗證和裝置綁定,目標電腦無須連上網際網路。