產生存取權杖
[AEM Forms as a Cloud Service]{class="badge informative"}
存取權杖是用於向REST API驗證和授權請求的認證。 它通常在使用者或應用程式成功登入後由驗證伺服器(例如OAuth 2.0或OpenID Connect)發出。 呼叫安全的Forms檔案服務API時,請求標頭中會包含存取權杖以驗證使用者端的身分。
以下是傳送存取權杖的範例要求
POST /api/data HTTP/1.1
Host: example.com
Authorization: Bearer eyJhbGciOiJIUzI1...
下列程式碼已用於產生存取權杖
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AccessTokenService {
private static final Logger logger = LoggerFactory.getLogger(AccessTokenService.class);
public String getAccessToken() {
ClassLoader classLoader = AccessTokenService.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("credentials/server_credentials.json")) {
if (inputStream == null) {
logger.error("File not found: credentials/server_credentials.json");
throw new IllegalArgumentException("Missing credentials file");
}
try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
CloseableHttpClient httpClient = HttpClients.createDefault()) {
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
String clientId = jsonObject.get("clientId").getAsString();
String clientSecret = jsonObject.get("clientSecret").getAsString();
String adobeIMSV3TokenEndpointURL = jsonObject.get("adobeIMSV3TokenEndpointURL").getAsString();
String scopes = jsonObject.get("scopes").getAsString();
HttpPost request = new HttpPost(adobeIMSV3TokenEndpointURL);
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
String requestBody = "grant_type=client_credentials&client_id=" + clientId +
"&client_secret=" + clientSecret +
"&scope=" + scopes;
request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_FORM_URLENCODED));
logger.info("Requesting access token from {}", adobeIMSV3TokenEndpointURL);
try (CloseableHttpResponse response = httpClient.execute(request)) {
String responseString = EntityUtils.toString(response.getEntity());
JsonObject jsonResponse = JsonParser.parseString(responseString).getAsJsonObject();
if (!jsonResponse.has("access_token")) {
logger.error("Access token not found in response: {}", responseString);
return null;
}
String accessToken = jsonResponse.get("access_token").getAsString();
logger.info("Successfully obtained access token.");
return accessToken;
}
}
} catch (Exception e) {
logger.error("Error fetching access token", e);
}
return null;
}
}
AccessTokenService類別負責從Adobe的IMS驗證服務擷取OAuth存取權杖。 它會從JSON檔案(server_credentials.json)讀取使用者端認證、建構驗證請求,並將其傳送至Adobe權杖端點。 接著,系統會剖析回應,擷取用於安全API呼叫的存取權杖。 此類別包含適當的記錄和錯誤處理,以確保可靠性和更輕鬆的偵錯。
4859a77c-7971-4ac9-8f5c-4260823c6f69