Catch up on highlights from Firebase at Google I/O 2023. Learn more

在 Apple 平台上使用 App Check 保護非 Firebase 資源

您可以使用 App Check 保護應用的非 Firebase 資源,例如自託管後端。為此,您需要執行以下兩項操作:

  • 修改您的應用程序客戶端以將 App Check 令牌與每個請求一起發送到您的後端,如本頁所述。
  • 修改您的後端以在每個請求中都需要有效的 App Check 令牌,如從自定義後端驗證 App Check 令牌中所述。

在你開始之前

使用App AttestDeviceCheck自定義提供程序將 App Check 添加到您的應用程序。

使用後端請求發送 App Check 令牌

為確保您的後端請求包含有效、未過期的 App Check 令牌,請將每個請求包裝在對AppCheck.token()的調用中。 App Check 庫將在必要時刷新令牌,您可以在方法的完成塊中訪問令牌。

獲得有效令牌後,將其與請求一起發送到後端。具體如何實現由您決定,但不要將 App Check 令牌作為 URL 的一部分發送,包括在查詢參數中,因為這會使它們容易受到意外洩漏和攔截。以下示例在自定義 HTTP 標頭中發送令牌,這是推薦的方法。

迅速

AppCheck.appCheck().token(forcingRefresh: false) { token, error in
    guard error == nil else {
        // Handle any errors if the token was not retrieved.
        print("Unable to retrieve App Check token: \(error!)")
        return
    }
    guard let token = token else {
        print("Unable to retrieve App Check token.")
        return
    }

    // Get the raw App Check token string.
    let tokenString = token.token

    // Include the App Check token with requests to your server.
    let url = URL(string: "https://yourbackend.example.com/yourApiEndpoint")!
    var request = URLRequest(url: url)
    request.httpMethod = "GET"
    request.setValue(tokenString, forHTTPHeaderField: "X-Firebase-AppCheck")

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        // Handle response from your backend.
    }
    task.resume()
}

目標-C

[[FIRAppCheck appCheck] tokenForcingRefresh:NO
                                 completion:^(FIRAppCheckToken * _Nullable token,
                                              NSError * _Nullable error) {
    if (error != nil) {
        // Handle any errors if the token was not retrieved.
        NSLog(@"Unable to retrieve App Check token: %@", error);
        return;
    }
    if (token == nil) {
        NSLog(@"Unable to retrieve App Check token.");
        return;
    }

    // Get the raw App Check token string.
    NSString *tokenString = token.token;

    // Include the App Check token with requests to your server.
    NSURL *url = [[NSURL alloc] initWithString:@"https://yourbackend.example.com/yourApiEndpoint"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"GET"];
    [request setValue:tokenString forHTTPHeaderField:@"X-Firebase-AppCheck"];

    NSURLSessionDataTask *task =
        [[NSURLSession sharedSession] dataTaskWithRequest:request
                                        completionHandler:^(NSData * _Nullable data,
                                                            NSURLResponse * _Nullable response,
                                                            NSError * _Nullable error) {
        // Handle response from your backend.
    }];
    [task resume];
}];

重播保護(測試版)

向已啟用重放保護的端點發出請求時,將請求包裝在對limitedUseToken()而不是token()調用中:

迅速

AppCheck.appCheck().limitedUseToken() { token, error in
  // ...
}

目標-C

[[FIRAppCheck appCheck] limitedUseTokenWithCompletion:^(FIRAppCheckToken * _Nullable token,
                                                        NSError * _Nullable error) {
    // ...
}];