添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

具有 Microsoft Entra 識別符的 Spring Security

當您建置 Web 應用程式時,身分識別和存取管理一律會是基本部分。

Azure 提供絕佳的平臺來將應用程式開發旅程大眾化,因為它不僅提供雲端式身分識別服務,還能與 Azure 生態系統的其餘部分進行深度整合。

Spring Security 可讓您輕鬆地使用功能強大的抽象和可延伸介面來保護 Spring 型應用程式。 不過,與 Spring 架構一樣強大,它並非針對特定身分識別提供者量身打造。

spring-cloud-azure-starter-active-directory 提供將 Web 應用程式連線到 Microsoft Entra ID (Microsoft Entra ID for short) 租使用者的最佳方式,並使用 Microsoft Entra ID 保護您的資源伺服器。 它會使用 Oauth 2.0 通訊協定來保護 Web 應用程式和資源伺服器。

存取 Web 應用程式

此案例會使用 OAuth 2.0 授權碼授與 流程,以使用Microsoft帳戶登入使用者。

  • 請閱讀 快速入門:向 Microsoft 身分識別平台註冊應用程式,

  • 建立應用程式註冊。 取得 AZURE_TENANT_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET

  • redirect URI 設定為 APPLICATION_BASE_URI/login/oauth2/code/ ,例如 http://localhost:8080/login/oauth2/code/ 。 尾 / 是必要的。

    新增必要的相依性

    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-active-directory</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
    </dependencies>
    

    新增必要的屬性

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    現在,啟動您的應用程式,並透過瀏覽器存取您的應用程式。 系統會將您重新導向至Microsoft登入頁面。

    進階使用方式

    新增額外的安全性設定
    @Bean SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.apply(AadWebApplicationHttpSecurityConfigurer.aadWebApplication()) .and() .authorizeHttpRequests() .anyRequest().authenticated(); // Do some custom configuration. return http.build();
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class AadOAuth2LoginSecurityConfig extends AadWebSecurityConfigurerAdapter {
         * Add configuration logic as needed.
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http.authorizeRequests()
                    .anyRequest().authenticated();
            // Do some custom configuration
    
    依應用程式角色授權存取權

    在 Azure 中建立必要的資源:

  • 讀取 將應用程式角色新增至您的應用程式,並在令牌中接收它們。

  • 使用下列參數建立應用程式角色:

  • 顯示名稱:系統管理員
  • 允許的成員類型:使用者/群組
  • 值:系統管理員
  • 您要啟用此應用程式角色嗎: 是
  • @PreAuthorize("hasAuthority('APPROLE_Admin')") public String admin() { return "Admin message";
    依組名或組標識碼授權存取權

    新增相關的組態屬性。

    spring:
     cloud:
       azure:
         active-directory:
           enabled: true
           user-group:
             allowed-group-names: group1_name_1, group2_name_2
             # 1. If allowed-group-ids == all, then all group ID will take effect.
             # 2. If "all" is used, we should not configure other group ids.
             # 3. "all" is only supported for allowed-group-ids, not supported for allowed-group-names.
             allowed-group-ids: group_id_1, group_id_2
    

    保護特定方法。

    @Controller
    public class RoleController {
       @GetMapping("group1")
       @ResponseBody
       @PreAuthorize("hasRole('ROLE_group1')")
       public String group1() {
           return "group1 message";
       @GetMapping("group2")
       @ResponseBody
       @PreAuthorize("hasRole('ROLE_group2')")
       public String group2() {
           return "group2 message";
       @GetMapping("group1Id")
       @ResponseBody
       @PreAuthorize("hasRole('ROLE_<group1-id>')")
       public String group1Id() {
           return "group1Id message";
       @GetMapping("group2Id")
       @ResponseBody
       @PreAuthorize("hasRole('ROLE_<group2-id>')")
       public String group2Id() {
           return "group2Id message";
    
    使用國家 Azure 而非全域 Azure

    除了全域 Azure 雲端之外,Microsoft Entra ID 部署在下列國家/地區雲端:

  • Azure 政府雲

  • Azure 中國世紀互聯

  • Azure 德國

    以下是使用 Azure China 21Vianet 的範例。

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            base-uri: https://login.partner.microsoftonline.cn
            graph-base-uri: https://microsoftgraph.chinacloudapi.cn
    

    如需詳細資訊,請參閱國家雲端部署。

    設定重新導向 URI 範本

    開發人員可以自定義 redirect-uri。

    @Bean public SecurityFilterChain htmlFilterChain(HttpSecurity http) throws Exception { // @formatter:off http.apply(AadWebApplicationHttpSecurityConfigurer.aadWebApplication()) .and() .oauth2Login() .loginProcessingUrl("${REDIRECT-URI-TEMPLATE}") .and() .authorizeHttpRequests() .anyRequest().authenticated(); // @formatter:on return http.build();
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class AadOAuth2LoginSecurityConfig extends AadWebSecurityConfigurerAdapter {
         * Add configuration logic as needed.
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http.oauth2Login()
                    .loginProcessingUrl("${REDIRECT-URI-TEMPLATE}")
                    .and()
                .authorizeRequests()
                    .anyRequest().authenticated();
    
    透過 Proxy 連線到 Microsoft Entra ID

    若要透過 Proxy 連線Microsoft Entra ID,請提供 RestTemplateCustomizer 豆,如下列範例所示:

    @Configuration
    class DemoConfiguration {
        @Bean
        public RestTemplateCustomizer proxyRestTemplateCustomizer() {
            return (RestTemplate restTemplate) -> {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_SERVER_HOST, PROXY_SERVER_PORT));
                SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
                requestFactory.setProxy(proxy);
                restTemplate.setRequestFactory(requestFactory);
    

    範例專案:aad-web-application

    存取資源伺服器的 Web 應用程式

  • 請閱讀 快速入門:向 Microsoft 身分識別平台註冊應用程式,

  • 建立應用程式註冊。 取得 AZURE_TENANT_IDAZURE_CLIENT_IDAZURE_CLIENT_SECRET

  • redirect URI 設定為 APPLICATION_BASE_URI/login/oauth2/code/,例如 http://localhost:8080/login/oauth2/code/。 尾 / 是必要的。

    新增必要的相依性

    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-active-directory</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
    </dependencies>
    

    新增必要的屬性

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
            authorization-clients:
              graph:
                scopes: https://graph.microsoft.com/Analytics.Read, email
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    在這裡,graphOAuth2AuthorizedClient的名稱,scopes 表示登入時需要同意的範圍。

    在應用程式中使用 OAuth2AuthorizedClient

    public class Demo {
        @GetMapping("/graph")
        @ResponseBody
        public String graph(
        @RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient graphClient) {
            // toJsonString() is just a demo.
            // oAuth2AuthorizedClient contains access_token. We can use this access_token to access resource server.
            return toJsonString(graphClient);
    

    現在,啟動您的應用程式,並在瀏覽器中存取您的應用程式。 然後,系統會將您重新導向至Microsoft登入頁面。

    進階使用方式

    用戶端認證流程

    如果您想要使用 用戶端認證流程,則預設流程 授權碼流程,您可以如下所示進行設定:

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
            authorization-clients:
              graph:
                authorization-grant-type: client_credentials # Change type to client_credentials
                scopes: https://graph.microsoft.com/Analytics.Read, email
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    存取多個資源伺服器

    在一個 Web 應用程式中,您可以藉由設定如下來存取多個資源伺服器:

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
            authorization-clients:
              resource-server-1:
                scopes: # Scopes for resource-server-1
              resource-server-2:
                scopes: # Scopes for resource-server-2
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    然後,您可以在應用程式中使用 OAuth2AuthorizedClient,如下所示

    public class Demo {
        @GetMapping("/resource-server-1")
        @ResponseBody
        public String graph(
        @RegisteredOAuth2AuthorizedClient("resource-server-1") OAuth2AuthorizedClient client) {
            return callResourceServer1(client);
        @GetMapping("/resource-server-2")
        @ResponseBody
        public String graph(
        @RegisteredOAuth2AuthorizedClient("resource-server-2") OAuth2AuthorizedClient client) {
            return callResourceServer2(client);
    

    範例專案:aad-web-application

    存取資源伺服器

    此案例不支援登入,只要藉由驗證存取令牌來保護伺服器。 如果存取令牌有效,伺服器會提供要求。

    <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-active-directory</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> </dependencies>

    新增必要的屬性

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            credential:
              client-id: ${AZURE_CLIENT_ID}
    

    現在啟動您的應用程式並存取應用程式的 Web API。

  • 您將取得 401,而不需要存取令牌。

  • 使用存取令牌存取您的應用程式。 存取權杖中的下列宣告將會經過驗證:

    iss:存取令牌必須由Microsoft Entra ID 發出。

    nbf:目前的時間不能在 nbf之前。

    exp:目前的時間不能在 exp之後。

    aud:如果 spring.cloud.azure.active-directory.credential.client-idspring.cloud.azure.active-directory.credential.app-id-uri 設定,對象必須等於設定的 client-idapp-id-uri。 如果未設定這兩個屬性,將不會驗證此宣告。

    @EnableWebSecurity @EnableMethodSecurity public class AadOAuth2ResourceServerSecurityConfig { * Add configuration logic as needed. @Bean public SecurityFilterChain htmlFilterChain(HttpSecurity http) throws Exception { // @formatter:off http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer()) .and() .authorizeHttpRequests() .anyRequest().authenticated(); // @formatter:on return http.build();

    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class AadOAuth2ResourceServerSecurityConfig extends AadResourceServerWebSecurityConfigurerAdapter {
         * Add configuration logic as needed.
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
            http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
    
    依範圍驗證許可權
  • 在 Azure 中建立必要的資源。

  • 閱讀 快速入門:設定應用程式以公開 web API

  • 使用名為 Scope1的範圍公開 Web API。

  • 保護特定方法。

    class Demo {
        @GetMapping("scope1")
        @ResponseBody
        @PreAuthorize("hasAuthority('SCOPE_Scope1')")
        public String scope1() {
            return "Congratulations, you can access `scope1` endpoint.";
    

    如此一來,當存取 /scope1 端點時,將會驗證存取令牌中的下列宣告:

    scp:值必須包含 Scope1
  • 依應用程式角色驗證許可權
  • 在 Azure 中建立必要的資源。

  • 讀取 將應用程式角色新增至您的應用程式,並在令牌中接收它們。

  • 使用下列參數建立應用程式角色:

  • 顯示名稱:AppRole1
  • 允許的成員類型:使用者/群組
  • 值:AppRole1
  • 您要啟用此應用程式角色嗎: 是
  • @PreAuthorize("hasAuthority('APPROLE_AppRole1')") public String appRole1() { return "Congratulations, you can access `app-role1` endpoint.";

    如此一來,當存取 /app-role1 端點時,將會驗證存取令牌中的下列宣告:

    roles:值必須包含 AppRole1
  • 使用 JWT 用戶端驗證

    若要使用 JSON Web 令牌 (JWT) 進行客戶端驗證,請使用下列步驟:

  • 請參閱 Microsoft 身分識別 平臺應用程式驗證憑證認證向Microsoft身分識別平臺註冊憑證一節。
  • .pem 憑證上傳至 Azure 入口網站中註冊的應用程式。
  • 設定 的憑證路徑和密碼。PFX。P12 憑證。
  • 將屬性 spring.cloud.azure.active-directory.authorization-clients.azure.client-authentication-method=private_key_jwt 組態新增至用戶端,以透過 JWT 用戶端驗證進行驗證。
  • 下列範例組態檔適用於 Web 應用程式案例。 憑證資訊是在全域屬性中設定的。

    spring:
      cloud:
        azure:
          credential:
            client-id: ${AZURE_CLIENT_ID}
            client-certificate-path: ${AZURE_CERTIFICATE_PATH}
            client-certificate-password: ${AZURE_CERTIFICATE_PASSWORD}
          profile:
            tenant-id: <tenant>
          active-directory:
            enabled: true
            user-group:
              allowed-group-names: group1,group2
              allowed-group-ids: <group1-id>,<group2-id>
            post-logout-redirect-uri: http://localhost:8080
            authorization-clients:
              azure:
                client-authentication-method: private_key_jwt
                client-authentication-method: private_key_jwt
                scopes: https://management.core.windows.net/user_impersonation
              graph:
                client-authentication-method: private_key_jwt
                scopes:
                  - https://graph.microsoft.com/User.Read
                  - https://graph.microsoft.com/Directory.Read.All
              webapiA:
                client-authentication-method: private_key_jwt
                scopes:
                  - ${WEB_API_A_APP_ID_URL}/Obo.WebApiA.ExampleScope
              webapiB:
                client-authentication-method: private_key_jwt
                scopes:
                  - ${WEB_API_B_APP_ID_URL}/.default
                authorization-grant-type: client_credentials
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    您也可以在 active-directory 服務屬性中設定憑證資訊,如下列範例所示:

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-certificate-path: ${AZURE_CERTIFICATE_PATH}
              client-certificate-password: ${AZURE_CERTIFICATE_PASSWORD}
            profile:
              tenant-id: <tenant>
            user-group:
              allowed-group-names: group1,group2
              allowed-group-ids: <group1-id>,<group2-id>
            post-logout-redirect-uri: http://localhost:8080
            authorization-clients:
              azure:
                client-authentication-method: private_key_jwt
                client-authentication-method: private_key_jwt
                scopes: https://management.core.windows.net/user_impersonation
              graph:
                client-authentication-method: private_key_jwt
                scopes:
                  - https://graph.microsoft.com/User.Read
                  - https://graph.microsoft.com/Directory.Read.All
              webapiA:
                client-authentication-method: private_key_jwt
                scopes:
                  - ${WEB_API_A_APP_ID_URL}/Obo.WebApiA.ExampleScope
              webapiB:
                client-authentication-method: private_key_jwt
                scopes:
                  - ${WEB_API_B_APP_ID_URL}/.default
                authorization-grant-type: client_credentials
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    透過 Proxy 連線到 Microsoft Entra ID

    若要透過 Proxy 連線Microsoft Entra ID,請提供 RestTemplateCustomizer 豆。 如需詳細資訊,請參閱 透過 proxy 連線到 Microsoft Entra ID 一節。

    範例專案:aad-resource-server

    造訪其他資源伺服器的資源伺服器

  • 請閱讀 快速入門:向 Microsoft 身分識別平台註冊應用程式,

  • 建立應用程式註冊。 取得 AZURE_TENANT_IDAZURE_CLIENT_IDAZURE_CLIENT_SECRET

    新增必要的相依性

    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-active-directory</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
    </dependencies>
    

    新增必要的屬性

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
            authorization-clients:
              graph:
                scopes:
                  - https://graph.microsoft.com/User.Read
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    在應用程式中使用 OAuth2AuthorizedClient

    public class SampleController {
        @GetMapping("call-graph")
        public String callGraph(@RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient graph) {
            return callMicrosoftGraphMeEndpoint(graph);
    

    範例專案:aad-resource-server-obo

    一個應用程式中的 Web 應用程式和資源伺服器

    在 Azure 中建立必要的資源

  • 請閱讀 快速入門:向 Microsoft 身分識別平台註冊應用程式,

  • 建立應用程式註冊。 取得 AZURE_TENANT_IDAZURE_CLIENT_IDAZURE_CLIENT_SECRET

    新增必要的相依性

    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-active-directory</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
    </dependencies>
    

    新增必要的屬性

    將屬性 spring.cloud.azure.active-directory.application-type 設定為 web_application_and_resource_server,併為每個授權用戶端指定授權類型。

    spring:
      cloud:
        azure:
          active-directory:
            enabled: true
            profile:
              tenant-id: <tenant>
            credential:
              client-id: ${AZURE_CLIENT_ID}
              client-secret: ${AZURE_CLIENT_SECRET}
            app-id-uri: ${WEB_API_ID_URI}
            application-type: web_application_and_resource_server  # This is required.
            authorization-clients:
              graph:
                authorizationGrantType: authorization_code # This is required.
                scopes:
                  - https://graph.microsoft.com/User.Read
                  - https://graph.microsoft.com/Directory.Read.All
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    定義 SecurityFilterChain

    設定多個 SecurityFilterChain 實例。 AadWebApplicationAndResourceServerConfig 包含兩個資源伺服器和 Web 應用程式的安全性篩選鏈結組態。

    @EnableWebSecurity @EnableMethodSecurity public class AadWebApplicationAndResourceServerConfig { @Bean @Order(1) public SecurityFilterChain apiFilterChain(HttpSecurity http) throws Exception { http.apply(AadResourceServerHttpSecurityConfigurer.aadResourceServer()) .and() // All the paths that match `/api/**`(configurable) work as the resource server. Other paths work as the web application. .securityMatcher("/api/**") .authorizeHttpRequests() .anyRequest().authenticated(); return http.build(); @Bean public SecurityFilterChain htmlFilterChain(HttpSecurity http) throws Exception { // @formatter:off http.apply(AadWebApplicationHttpSecurityConfigurer.aadWebApplication()) .and() .authorizeHttpRequests() .requestMatchers("/login").permitAll() .anyRequest().authenticated(); // @formatter:on return http.build();
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class AadWebApplicationAndResourceServerConfig {
        @Order(1)
        @Configuration
        public static class ApiWebSecurityConfigurationAdapter extends AadResourceServerWebSecurityConfigurerAdapter {
            protected void configure(HttpSecurity http) throws Exception {
                super.configure(http);
                // All the paths that match `/api/**`(configurable) work as `Resource Server`, other paths work as `Web application`.
                http.antMatcher("/api/**")
                    .authorizeRequests().anyRequest().authenticated();
        @Configuration
        public static class HtmlWebSecurityConfigurerAdapter extends AadWebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                super.configure(http);
                // @formatter:off
                http.authorizeRequests()
                        .antMatchers("/login").permitAll()
                        .anyRequest().authenticated();
                // @formatter:on
    

    spring-cloud-azure-starter-active-directory 的可設定屬性:

    以下是如何使用這些屬性的一些範例:

    應用程式類型

    應用程式類型可以從相依性推斷:spring-security-oauth2-clientspring-security-oauth2-resource-server。 如果推斷的值不是您想要的值,您可以指定應用程式類型。 以下是有效值和推斷值資料表:

    spring-cloud-azure-starter-active-directory的應用程式類型:

    具有相依性:spring-security-oauth2-client 具有相依性:spring-security-oauth2-resource-server 應用程式類型的有效值 web_applicationresource_serverresource_server_with_oboweb_application_and_resource_server resource_server_with_obo

    使用 Azure Active Directory B2C 的 Spring Security

    Azure Active Directory (Azure AD) B2C 是一項身分識別管理服務,可讓您自定義及控制客戶在使用應用程式時如何註冊、登入及管理其配置檔。 Azure AD B2C 可啟用這些動作,同時保護客戶的身分識別。

    相依性設定

    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-active-directory-b2c</artifactId>
        </dependency>
    </dependencies>
    

    spring-cloud-azure-starter-active-directory-b2c 的可設定屬性:

    Web 應用程式 是任何 Web 型應用程式,可讓使用者使用 Microsoft Entra 識別元登入,而 資源伺服器 在驗證從 Microsoft Entra ID 取得的access_token之後,將會接受或拒絕存取。 我們將涵蓋本指南中的 4 個案例:

  • 存取 Web 應用程式。

  • 存取資源伺服器的 Web 應用程式。

  • 存取資源伺服器。

  • 存取其他資源伺服器的資源伺服器。

    使用方式 1:存取 Web 應用程式

    此案例使用 OAuth 2.0 授權碼授與 流程,以使用您的 Azure AD B2C 使用者登入使用者。

    從入口網站功能表中選取 [Azure AD B2C],選取 [應用程式],然後選取 [新增]。

    指定您的應用程式 名稱(例如 webapp),為 http://localhost:8080/login/oauth2/code/新增 ,將 應用程式識別碼 記錄為 WEB_APP_AZURE_CLIENT_ID,然後選取 [儲存]。

    從您的應用程式選取 [金鑰],選取 [產生密鑰 以產生 ],然後選取 [儲存]。

    選擇左側 使用者流程,然後選取 [[新增使用者流程]

    選擇 [註冊] 或 []、配置檔編輯,以及 [密碼重設] 分別建立使用者流程。 指定您的使用者流程 名稱使用者屬性與宣告,然後選取 [建立]

    選取 [API 許可權],>[新增許可權>Microsoft API]、選取 [Microsoft Graph]、選取 [委派的許可權]、選取 [offline_access] 和 [openid 許可權],然後選取 [新增許可權 以完成此程式。

    授與管理員同意 Graph 許可權。

    <dependency> <groupId>com.azure.spring</groupId> <artifactId>azure-spring-boot-starter-active-directory-b2c</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> </dependency> </dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>azure-spring-boot-starter-active-directory-b2c</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency> </dependencies> enabled: true authenticate-additional-parameters: domain_hint: xxxxxxxxx # optional login_hint: xxxxxxxxx # optional prompt: [login,none,consent] # optional base-uri: ${BASE_URI} credential: client-id: ${WEBAPP_AZURE_CLIENT_ID} client-secret: ${WEBAPP_AZURE_CLIENT_SECRET} login-flow: ${LOGIN_USER_FLOW_KEY} # default to sign-up-or-sign-in, will look up the user-flows map with provided key. logout-success-url: ${LOGOUT_SUCCESS_URL} user-flows: ${YOUR_USER_FLOW_KEY}: ${USER_FLOW_NAME} user-name-attribute-name: ${USER_NAME_ATTRIBUTE_NAME}

    撰寫 Java 程式代碼。

    針對控制器程式代碼,您可以參考下列範例:

    @Controller
    public class WebController {
       private void initializeModel(Model model, OAuth2AuthenticationToken token) {
           if (token != null) {
               final OAuth2User user = token.getPrincipal();
               model.addAllAttributes(user.getAttributes());
               model.addAttribute("grant_type", user.getAuthorities());
               model.addAttribute("name", user.getName());
       @GetMapping(value = { "/", "/home" })
       public String index(Model model, OAuth2AuthenticationToken token) {
           initializeModel(model, token);
           return "home";
    

    針對安全性設定程式碼,您可以參考下列範例:

    @Configuration(proxyBeanMethods = false)
    @EnableWebSecurity
    public class WebSecurityConfiguration {
        private final AadB2cOidcLoginConfigurer configurer;
        public WebSecurityConfiguration(AadB2cOidcLoginConfigurer configurer) {
            this.configurer = configurer;
        @Bean
        SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
            // @formatter:off
            http.authorizeHttpRequests()
                    .anyRequest().authenticated()
                    .and()
                .apply(configurer);
            // @formatter:on
            return http.build();
    
    @EnableWebSecurity
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
       private final AadB2cOidcLoginConfigurer configurer;
       public WebSecurityConfiguration(AadB2cOidcLoginConfigurer configurer) {
           this.configurer == configurer;
       @Override
       protected void configure(HttpSecurity http) throws Exception {
           // @formatter:off
           http.authorizeRequests()
                   .anyRequest().authenticated()
                   .and()
               .apply(configurer);
           // @formatter:off
    

    複製 aad-b2c-web-application,並將 PROFILE_EDIT_USER_FLOW 取代為您先前使用的使用者流程名稱。

    建置及測試您的應用程式。 讓 Webapp 在埠 8080上執行。

    在 Maven 建置並啟動您的應用程式之後,請在網頁瀏覽器中開啟 http://localhost:8080/。 您應該重新導向至登入頁面。

    選取具有登入使用者流程的連結。 您應該將 Azure AD B2C 重新導向以啟動驗證程式。

    成功登入之後,您應該會看到來自瀏覽器的範例 home page

    使用方式 2:存取資源伺服器的 Web 應用程式

    此案例是以 存取 Web 應用程式 案例為基礎,以允許應用程式存取其他資源。 此案例 OAuth 2.0 用戶端認證授與 流程。

    從入口網站功能表中選取 [Azure AD B2C],選取 [應用程式],然後選取 [新增]。

    指定您的應用程式 名稱(例如 webApiA),將 應用程式識別碼 記錄為 WEB_API_A_AZURE_CLIENT_ID,然後選取 [儲存]。

    從您的應用程式選取 [金鑰],選取 [產生密鑰 以產生 ],然後選取 [儲存]。

    從瀏覽窗格中選取 [[公開 API],然後選取 [設定]。 將 應用程式識別碼 URI 記錄為 ,然後選取 [儲存]。

    從瀏覽窗格中選取 指令清單,然後將下列 JSON 區段貼到 appRoles 陣列中。 將 應用程式識別碼 URI 記錄為 WEB_API_A_APP_ID_URL,將應用程式角色的值記錄為 WEB_API_A_ROLE_VALUE,然後選取 [儲存]。

    "allowedMemberTypes": [ "Application" "description": "WebApiA.SampleScope", "displayName": "WebApiA.SampleScope", "id": "04989db0-3efe-4db6-b716-ae378517d2b7", "isEnabled": true, "value": "WebApiA.SampleScope"

    選取 [API 許可權],>[新增許可權>My API]、選取 [WebApiA 應用程式名稱]、選取 [應用程式許可權]、選取 [WebApiA.SampleScope 許可權],然後選取 [新增許可權 以完成此程式。

    授與系統管理員同意 WebApiA 許可權。

    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    

    根據 存取 Web 應用程式 案例,新增下列設定。

    spring:
     cloud:
       azure:
         active-directory:
             enabled: true
             base-uri: ${BASE_URI}             # Such as: https://xxxxb2c.b2clogin.com
             profile:
               tenant-id: <tenant>
             authorization-clients:
               ${RESOURCE_SERVER_A_NAME}:
                 authorization-grant-type: client_credentials
                 scopes: ${WEB_API_A_APP_ID_URL}/.default
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    撰寫 Webapp Java 程式代碼。

    針對控制器程式代碼,您可以參考下列範例:

    class Demo {
        * Access to protected data from Webapp to WebApiA through client credential flow. The access token is obtained by webclient, or
        * <p>@RegisteredOAuth2AuthorizedClient("webApiA")</p>. In the end, these two approaches will be executed to
        * DefaultOAuth2AuthorizedClientManager#authorize method, get the access token.
        * @return Respond to protected data from WebApi A.
       @GetMapping("/webapp/webApiA")
       public String callWebApiA() {
           String body = webClient
               .get()
               .uri(LOCAL_WEB_API_A_SAMPLE_ENDPOINT)
               .attributes(clientRegistrationId("webApiA"))
               .retrieve()
               .bodyToMono(String.class)
               .block();
           LOGGER.info("Call callWebApiA(), request '/webApiA/sample' returned: {}", body);
           return "Request '/webApiA/sample'(WebApi A) returned a " + (body != null ? "success." : "failure.");
    

    安全性設定程式代碼與存取 Web 應用程式 案例中的 相同。 新增另一個豆 webClient,如下所示:

    public class SampleConfiguration {
       @Bean
       public WebClient webClient(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
           ServletOAuth2AuthorizedClientExchangeFilterFunction function =
               new ServletOAuth2AuthorizedClientExchangeFilterFunction(oAuth2AuthorizedClientManager);
           return WebClient.builder()
                           .apply(function.oauth2Configuration())
                           .build();
    

    若要撰寫 Java 程式代碼,請參閱存取資源伺服器 一節

    建置及測試您的應用程式。 讓 WebappWebApiA 分別在埠 80808081 上執行。 啟動 WebappWebApiA 應用程式。 成功登入之後返回首頁。 然後,您可以存取 http://localhost:8080/webapp/webApiA 以取得 WebApiA 資源回應。

    使用方式 3:存取資源伺服器

    此案例不支援登入。 只要驗證存取令牌來保護伺服器,如果有效,則會提供要求。

    若要建置您的 WebApiA 許可權,請參閱 Usage 2:Web 應用程式存取資源伺服器

    新增 WebApiA 許可權,並授與 Web 應用程式的管理員同意。

    將下列相依性新增至 pom.xml 檔案。

    <dependencies>
       <dependency>
           <groupId>com.azure.spring</groupId>
           <artifactId>azure-spring-boot-starter-active-directory-b2c</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
    </dependencies>
    

    新增下列設定。

    spring:
     cloud:
       azure:
         active-directory:
             enabled: true
             base-uri: ${BASE_URI}             # Such as: https://xxxxb2c.b2clogin.com
             profile:
               tenant-id: <tenant>
             app-id-uri: ${APP_ID_URI}         # If you're using v1.0 token, configure app-id-uri for `aud` verification
             credential:
               client-id: ${AZURE_CLIENT_ID}           # If you're using v2.0 token, configure client-id for `aud` verification
             user-flows:
               sign-up-or-sign-in: ${SIGN_UP_OR_SIGN_IN_USER_FLOW_NAME}
                  tenant-id 允許的值包括:commonorganizationsconsumers或租用戶標識碼。 如需這些值的詳細資訊,請參閱 錯誤AADSTS50020 - 來自身分識別提供者的用戶帳戶不存在於租使用者使用錯誤的端點(個人和組織帳戶)一節。 如需轉換單一租使用者應用程式的資訊,請參閱 將單一租使用者應用程式轉換成多租使用者Microsoft Entra ID

    撰寫 Java 程式代碼。

    針對控制器程式代碼,您可以參考下列範例:

    class Demo {
        * webApiA resource api for web app
        * @return test content
       @PreAuthorize("hasAuthority('APPROLE_WebApiA.SampleScope')")
       @GetMapping("/webApiA/sample")
       public String webApiASample() {
           LOGGER.info("Call webApiASample()");
           return "Request '/webApiA/sample'(WebApi A) returned successfully.";
    

    針對安全性設定程式碼,您可以參考下列範例:

    @Bean public SecurityFilterChain htmlFilterChain(HttpSecurity http) throws Exception { JwtAuthenticationConverter authenticationConverter = new JwtAuthenticationConverter(); JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter(); jwtGrantedAuthoritiesConverter.setAuthorityPrefix("APPROLE_"); authenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter); // @formatter:off http.authorizeHttpRequests((requests) -> requests.anyRequest().authenticated()) .oauth2ResourceServer() .jwt() .jwtAuthenticationConverter(authenticationConverter); // @formatter:on return http.build();
    @EnableWebSecurity
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public class ResourceServerConfiguration extends WebSecurityConfigurerAdapter {
       @Override
       protected void configure(HttpSecurity http) throws Exception {
           http.authorizeRequests((requests) -> requests.anyRequest().authenticated())
               .oauth2ResourceServer()
               .jwt()
                   .jwtAuthenticationConverter(new AadJwtBearerTokenAuthenticationConverter());
    

    建置及測試您的應用程式。 讓 WebApiA 在埠 8081上執行。 取得 webApiA 資源的存取令牌,然後存取 http://localhost:8081/webApiA/sample 作為持有人授權標頭。

    使用方式 4:存取其他資源伺服器的資源伺服器

    此案例是 存取資源伺服器升級,並支持根據OAuth2用戶端認證流程存取其他應用程式資源。

    在參考先前的步驟時,我們會建立 WebApiB 應用程式,並公開應用程式許可權 WebApiB.SampleScope

    "allowedMemberTypes": [ "Application" "description": "WebApiB.SampleScope", "displayName": "WebApiB.SampleScope", "id": "04989db0-3efe-4db6-b716-ae378517d2b7", "isEnabled": true, "lang": null, "origin": "Application", "value": "WebApiB.SampleScope"

    授與系統管理員同意 WebApiB 許可權。

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    

    存取資源伺服器 案例組態的基礎上,新增下列組態。

    spring:
     cloud:
       azure:
         active-directory:
             enabled: true
             credential:
               client-secret: ${WEB_API_A_AZURE_CLIENT_SECRET}
             authorization-clients:
               ${RESOURCE_SERVER_B_NAME}:
                 authorization-grant-type: client_credentials
                 scopes: ${WEB_API_B_APP_ID_URL}/.default
    

    撰寫 Java 程式代碼。

    針對您的 WebApiA 控制器程式代碼,您可以參考下列範例:

    public class SampleController {
        * Access to protected data from WebApiA to WebApiB through client credential flow. The access token is obtained by webclient, or
        * <p>@RegisteredOAuth2AuthorizedClient("webApiA")</p>. In the end, these two approaches will be executed to
        * DefaultOAuth2AuthorizedClientManager#authorize method, get the access token.
        * @return Respond to protected data from WebApi B.
       @GetMapping("/webApiA/webApiB/sample")
       @PreAuthorize("hasAuthority('APPROLE_WebApiA.SampleScope')")
       public String callWebApiB() {
           String body = webClient
               .get()
               .uri(LOCAL_WEB_API_B_SAMPLE_ENDPOINT)
               .attributes(clientRegistrationId("webApiB"))
               .retrieve()
               .bodyToMono(String.class)
               .block();
           LOGGER.info("Call callWebApiB(), request '/webApiB/sample' returned: {}", body);
           return "Request 'webApiA/webApiB/sample'(WebApi A) returned a " + (body != null ? "success." : "failure.");
    

    針對您的 WebApiB 控制器程式代碼,您可以參考下列範例:

    public class SampleController {
        * webApiB resource api for other web application
        * @return test content
       @PreAuthorize("hasAuthority('APPROLE_WebApiB.SampleScope')")
       @GetMapping("/webApiB/sample")
       public String webApiBSample() {
           LOGGER.info("Call webApiBSample()");
           return "Request '/webApiB/sample'(WebApi B) returned successfully.";
    

    安全性設定程式代碼與 存取資源伺服器 案例相同,另一個豆 webClient 會新增,如下所示

    public class SampleConfiguration {
       @Bean
       public WebClient webClient(OAuth2AuthorizedClientManager oAuth2AuthorizedClientManager) {
           ServletOAuth2AuthorizedClientExchangeFilterFunction function =
               new ServletOAuth2AuthorizedClientExchangeFilterFunction(oAuth2AuthorizedClientManager);
           return WebClient.builder()
                           .apply(function.oauth2Configuration())
                           .build();
    

    建置及測試您的應用程式。 讓 WebApiAWebApiB 分別在埠 80818082 上執行。 啟動 WebApiAWebApiB 應用程式、取得 webApiA 資源的存取令牌,以及存取 http://localhost:8081/webApiA/webApiB/sample 作為持有人授權標頭。

    如需詳細資訊,請參閱 spring-cloud-azure-starter-active-directory-b2c 範例

  •