與Adobe Experience Manager (AEM)as a Cloud Service的整合必須能夠安全地向AEM服務進行驗證。 AEM開發人員控制檯授予對服務憑證的存取權,這些憑證用於促進外部應用程式、系統和服務以程式設計方式透過HTTP與AEM作者或發佈服務互動。
服務認證可能顯示類似 本機開發存取權杖 但在幾個關鍵方面不同:
服務憑證及其產生的存取權杖,以及本機開發存取權杖,都應保密。 這三者皆可取得,因此可存取其各自的AEMas a Cloud Service環境。
服務認證產生分為兩個步驟:
服務憑證與本機開發存取權杖不同,需要技術帳戶先由Adobe組織IMS管理員建立,才能下載。 應為需要程式化存取AEM的每個使用者端建立獨立技術帳戶。
技術帳戶只建立一次,不過隨著時間推移,可管理使用私密金鑰來管理與技術帳戶相關聯的服務認證。 例如,新的私密金鑰/服務認證必須在目前私密金鑰到期之前產生,以允許服務認證的使用者不受中斷地存取。
初始化AEM as Cloud Service環境的服務憑證後,您Adobe IMS組織中的其他AEM開發人員就可以下載這些憑證。
下載服務憑證的步驟與初始化類似。
服務憑證提供產生JWT所需的詳細資訊,此資訊會交換用來向AEMas a Cloud Service驗證的存取權杖。 服務認證必須儲存在安全位置,可供外部應用程式、系統或使用此認證來存取AEM的服務存取。 每個客戶管理服務認證的方式和位置都是唯一的。
為簡化起見,本教學課程透過命令列傳入服務認證。 然而,請與您的IT安全團隊合作,瞭解如何根據貴組織的安全方針,儲存及存取這些認證。
service_token.json
在專案的根目錄中
服務憑證是完整格式的JSON物件,與JWT或存取權杖不同。 而是使用服務認證(包含私密金鑰)來產生JWT,再與Adobe IMS API交換存取權杖。
若要使用「服務認證」存取AEMas a Cloud Service,外部應用程式必須以下列三種方式更新:
在本教學課程中,Adobe的 @adobe/jwt-auth
npm模組可用於兩種:(1)從服務憑證產生JWT,以及(2)在單一函式呼叫中將它交換為存取權杖。 如果您的應用程式不是JavaScript,請檢閱 其他語言的程式碼範例 瞭解如何從服務憑證建立JWT,並與Adobe IMS交換存取權杖。
檢閱 getCommandLineParams()
請參閱如何使用讀取本機開發存取權杖JSON時所用的相同程式碼來讀取服務認證JSON檔案。
function getCommandLineParams() {
...
// Read in the credentials from the provided JSON file
// Since both the Local Development Access Token and Service Credentials files are JSON, this same approach can be re-used
if (parameters.file) {
parameters.developerConsoleCredentials = JSON.parse(fs.readFileSync(parameters.file));
}
...
return parameters;
}
讀取服務認證後,即會使用它們產生JWT,然後與Adobe IMS API交換存取權杖。 然後,就可以使用此存取權杖來存取AEMas a Cloud Service。
此範例應用程式以Node.js為基礎,因此最好使用 @adobe/jwt-auth npm模組以利於(1) JWT的產生和(20)與Adobe IMS的交換。 如果您的應用程式是使用其他語言開發的,請檢閱 適當的程式碼範例 ,瞭解如何使用其他程式設計語言來建立向Adobe IMS傳送的HTTP要求。
更新 getAccessToken(..)
檢查JSON檔案內容,並判斷其是否代表本機開發存取權杖或服務認證。 這可透過檢查是否存在 .accessToken
屬性,僅適用於本機開發存取權杖JSON。
如果提供服務認證,應用程式會產生JWT並與Adobe IMS交換存取權杖。 使用 @adobe/jwt-auth的 auth(...)
函式產生JWT並將其交換為單一函式呼叫中的存取權杖。 引數至 auth(..)
方法是 JSON物件包含特定資訊 可從服務認證JSON取得,如下面的程式碼所述。
async function getAccessToken(developerConsoleCredentials) {
if (developerConsoleCredentials.accessToken) {
// This is a Local Development access token
return developerConsoleCredentials.accessToken;
} else {
// This is the Service Credentials JSON object that must be exchanged with Adobe IMS for an access token
let serviceCredentials = developerConsoleCredentials.integration;
// Use the @adobe/jwt-auth library to pass the service credentials generated a JWT and exchange that with Adobe IMS for an access token.
// If other programming languages are used, please see these code samples: https://www.adobe.io/authentication/auth-methods.html#!AdobeDocs/adobeio-auth/master/JWT/samples/samples.md
let { access_token } = await auth({
clientId: serviceCredentials.technicalAccount.clientId, // Client Id
technicalAccountId: serviceCredentials.id, // Technical Account Id
orgId: serviceCredentials.org, // Adobe IMS Org Id
clientSecret: serviceCredentials.technicalAccount.clientSecret, // Client Secret
privateKey: serviceCredentials.privateKey, // Private Key to sign the JWT
metaScopes: serviceCredentials.metascopes.split(','), // Meta Scopes defining level of access the access token should provide
ims: `https://${serviceCredentials.imsEndpoint}`, // IMS endpoint used to obtain the access token from
});
return access_token;
}
}
現在,視透過該「檔案」命令列引數傳入的JSON檔案(本機開發存取權杖JSON或服務認證JSON)而定,應用程式將會衍生存取權杖。
請記住,雖然服務憑證每365天過期一次,但JWT和對應的存取權杖會頻繁過期,而且需要在過期前重新整理。 使用「refresh_token」[由Adobe IMS提供](https://www.adobe.io/authentication/auth-methods.html#!AdobeDocs/adobeio-auth/master/OAuth/OAuth.md#access-tokens)即可完成此操作。
完成這些變更後,已從AEM開發人員控制檯下載服務認證JSON,且為了簡化,已儲存為 service_token.json
在與此相同的資料夾中 index.js
. 現在,讓我們執行應用程式來取代命令列引數 file
替換為 service_token.json
,並更新 propertyValue
變更為新值,因此效果在AEM中更為明顯。
$ node index.js \
aem=https://author-p1234-e5678.adobeaemcloud.com \
folder=/wknd-shared/en/adventures/napa-wine-tasting \
propertyName=metadata/dc:rights \
propertyValue="WKND Restricted Use" \
file=service_token.json
輸出到終端機的方式如下:
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_277654931.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_239751461.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_280313729.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_286664352.jpg.json
此 403 — 禁止 行,指出對AEMas a Cloud Service的HTTP API呼叫中的錯誤。 嘗試更新資產的中繼資料時,會發生這403個禁止錯誤。
原因在於服務認證衍生的存取權杖會使用自動建立的技術帳戶AEM使用者(預設情況下只有讀取存取權)來驗證AEM的要求。 若要提供應用程式對AEM的寫入存取權,與存取權杖相關聯的技術帳戶AEM使用者必須在AEM中取得許可權。
服務憑證衍生的存取權杖使用的技術帳戶AEM User在 貢獻者 AEM使用者群組。
一旦技術帳戶AEM使用者存在於AEM中(具有存取權杖的第一個HTTP請求之後),就可以像管理其他AEM使用者一樣管理此AEM使用者的許可權。
integration.email
值,看起來應類似於: 12345678-abcd-9000-efgh-0987654321c@techacct.adobe.com
.在AEM允許技術帳戶擁有資產的寫入許可權的情況下,請重新執行應用程式:
$ node index.js \
aem=https://author-p1234-e5678.adobeaemcloud.com \
folder=/wknd-shared/en/adventures/napa-wine-tasting \
propertyName=metadata/dc:rights \
propertyValue="WKND Restricted Use" \
file=service_token.json
輸出到終端機的方式如下:
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_277654931.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_286664352.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_239751461.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd-shared/en/adventures/napa-wine-tasting/AdobeStock_280313729.jpg.json
aem
命令列引數)folder
命令列引數,例如 WKND > 英文 > 冒險 > 納帕品酒會metadata/dc:rights
JCR屬性,現在會反映 propertyValue
引數,例如 WKND限制使用現在,我們已使用本機開發存取權杖和產品就緒的服務對服務存取權杖,以程式設計方式存取AEMas a Cloud Service!