存取設定伺服器和服務登錄

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

本文適用於: ✔️基本/標準 ❌ 企業

本文說明如何使用 Microsoft Entra 角色型訪問控制 (RBAC) 存取 Azure Spring Apps 所管理的 Spring Cloud Config Server 和 Spring Cloud Service Registry。

注意

在 Azure Spring Apps 服務內部署和執行的應用程式,在存取受控 Spring Cloud Config Server 和服務登錄時,會自動與憑證式驗證和授權連線。 您不需要遵循這些應用程式的本指南。 相關憑證完全由 Azure Spring Apps 平臺管理,且會在連線到 Config Server 和服務登錄時自動插入應用程式中。

將角色指派給 Microsoft Entra 使用者/群組、MSI 或服務主體

將角色指派給 [user | group | service-principal | managed-identity] 位於 [management-group | subscription | resource-group | resource] 範圍。

角色名稱 描述
Azure Spring Apps 設定伺服器讀取器 允許對 Azure Spring Apps 組態伺服器進行讀取存取。
Azure Spring Apps 設定伺服器參與者 允許讀取、寫入和刪除 Azure Spring Apps 組態伺服器的存取權。
Azure Spring Apps Service Registry 讀取器 允許 Azure Spring Apps Service Registry 的讀取存取權。
Azure Spring Apps Service 登錄參與者 允許讀取、寫入和刪除 Azure Spring Apps Service Registry 的存取權。

如需詳細步驟,請參閱使用 Azure 入口網站指派 Azure 角色

存取設定伺服器和服務登錄端點

指派角色之後,被指派者可以使用下列程式來存取 Spring Cloud Config Server 和 Spring Cloud Service Registry 端點:

  1. 取得存取令牌。 在指派 Microsoft Entra 使用者角色之後,他們可以使用下列命令,透過使用者、服務主體或受控識別來登入 Azure CLI,以取得存取令牌。 如需詳細資訊,請參閱 驗證 Azure CLI

    az login
    az account get-access-token
    
  2. 撰寫端點。 我們支援由 Azure Spring Apps 管理之 Spring Cloud 設定伺服器和 Spring Cloud Service Registry 的預設端點。

    • 'https://SERVICE_NAME.svc.azuremicroservices.io/eureka/{path}'
    • 'https://SERVICE_NAME.svc.azuremicroservices.io/config/{path}'

    注意

    如果您使用由 21Vianet 運作的 Microsoft Azure,請將 取代 *.azuremicroservices.io*.microservices.azure.cn。 如需詳細資訊,請參閱 21Vianet 開發人員指南在 Microsoft Azure 中檢查 Azure 中的端點一節

  3. 使用存取令牌存取撰寫的端點。 將存取令牌放在標頭中以提供授權: --header 'Authorization: Bearer {TOKEN_FROM_PREVIOUS_STEP}'

    例如:

    a. 存取端點,例如 https://SERVICE_NAME.svc.azuremicroservices.io/config/actuator/health 查看 Config Server 的健康情況狀態。

    b. 存取端點,例如 https://SERVICE_NAME.svc.azuremicroservices.io/eureka/eureka/apps 查看 Spring Cloud Service Registry 中已註冊的應用程式 (Eureka 在這裡)。

    如果回應為 401 Unauthorized,請檢查角色是否已成功指派。 角色需要幾分鐘的時間才會生效,或確認存取令牌尚未過期。

如需執行器端點的詳細資訊,請參閱 生產就緒端點

如需 Eureka 端點,請參閱 Eureka-REST-operations

如需設定伺服器端點和詳細的路徑資訊,請參閱 ResourceController.javaEncryptionController.java

向 Azure Spring Apps 管理的 Spring Cloud Config 伺服器和服務登錄註冊 Spring Boot 應用程式

指派角色之後,您可以使用 Microsoft Entra 令牌驗證,將 Spring Boot 應用程式註冊到由 Azure Spring Apps 管理的 Spring Cloud Config Server 和服務登錄。 Config Server 和服務登錄都支援 自定義 REST 範本 ,以插入持有人令牌以進行驗證。

如需詳細資訊,請參閱存取 Azure Spring Apps 受控設定伺服器存取 Azure Spring Apps 受控服務登錄範例。 下列各節說明這些範例中的一些重要詳細數據。

AccessTokenManager.java

AccessTokenManager 負責從 Microsoft Entra ID 取得存取令牌。 在 application.properties 檔案中設定服務主體的登入資訊,並初始化 ApplicationTokenCredentials 以取得令牌。 您可以在這兩個範例中找到此檔案。

prop.load(in);
tokenClientId = prop.getProperty("access.token.clientId");
String tenantId = prop.getProperty("access.token.tenantId");
String secret = prop.getProperty("access.token.secret");
String clientId = prop.getProperty("access.token.clientId");
credentials = new ApplicationTokenCredentials(
    clientId, tenantId, secret, AzureEnvironment.AZURE);

CustomConfigServiceBootstrapConfiguration.java

CustomConfigServiceBootstrapConfiguration 會實作 Config Server 的自定義 REST 範本,並將來自 Microsoft Entra ID 的令牌插入為 Authorization 標頭。 您可以在 Config Server 範例中找到此檔案。

public class RequestResponseHandlerInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        String accessToken = AccessTokenManager.getToken();
        request.getHeaders().remove(AUTHORIZATION);
        request.getHeaders().add(AUTHORIZATION, "Bearer " + accessToken);

        ClientHttpResponse response = execution.execute(request, body);
        return response;
    }

}

CustomRestTemplateTransportClientFactories.java

前兩個類別適用於 Spring Cloud Service Registry 的自定義 REST 範本實作。 元件 intercept 與上述元件與上述元件的相同。 請務必將 新增 factory.mappingJacksonHttpMessageConverter() 至訊息轉換器。 您可以在 Spring Cloud Service Registry 範例中找到此檔案。

private RestTemplate customRestTemplate() {
    /*
     * Inject your custom rest template
     */
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getInterceptors()
        .add(new RequestResponseHandlerInterceptor());
    RestTemplateTransportClientFactory factory = new RestTemplateTransportClientFactory();

    restTemplate.getMessageConverters().add(0, factory.mappingJacksonHttpMessageConverter());

    return restTemplate;
}

如果您要在 Kubernetes 叢集上執行應用程式,建議您使用 IP 位址來註冊 Spring Cloud Service Registry 以進行存取。

eureka.instance.prefer-ip-address=true

下一步