使用 Microsoft Entra 帳戶將登入新增至 Spring Web 應用程式

本文說明如何開發支援 Microsoft Entra 帳戶登入的 Spring Web 應用程式。 完成本文中的所有步驟之後,Web 應用程式會在匿名存取時重新導向至 Microsoft Entra 登入頁面。 下列螢幕快照顯示 Microsoft Entra 登入頁面:

Screenshot of application 'Sign in' dialog.

必要條件

完成本文中的步驟需要下列必要條件:

  • 支援的 Java 開發工具套件 (JDK)。 如需在 Azure 上開發時可用的 JDK 詳細資訊,請參閱 Azure 和 Azure Stack 上的 Java 支援。
  • Apache Maven 3.0 版或更高版本。
  • Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請建立免費帳戶

重要

需要 Spring Boot 2.5 版或更高版本,才能完成本文中的步驟。

使用 Spring Initializr 建立應用程式

  1. 瀏覽至 https://start.spring.io/

  2. 指定您想要使用 Java 產生 Maven 專案,請輸入應用程式的群組成品名稱。

  3. 新增 Spring WebMicrosoft Entra IDOAuth2 用戶端的相依性

  4. 在頁面底部,選取 [ 產生] 按鈕。

    Screenshot of Spring Initializr with basic options.

  5. 出現提示時,請將專案下載到本機計算機上的路徑。

建立 Microsoft Entra 實例

建立 Active Directory 實例

如果您是現有實例的系統管理員,您可以略過此程式。

  1. https://portal.azure.com登入 。

  2. 選取 [所有服務],然後選取 [身分識別],然後選取 [Microsoft Entra ID]。

  3. 輸入您的 組織名稱和 初始 功能變數名稱。 複製目錄的完整 URL。 您將在本教學課程稍後使用 URL 來新增使用者帳戶。 (例如: azuresampledirectory.onmicrosoft.com.)

    複製目錄的完整 URL。 您將在本教學課程稍後使用 URL 來新增使用者帳戶。 (例如: azuresampledirectory.onmicrosoft.com.)。

    當您完成時,請選取 [ 建立]。 建立新資源需要幾分鐘的時間。

    Screenshot of the Configuration section of the Microsoft Entra ID 'Create a tenant' screen.

  4. 完成時,選取顯示的連結以存取新目錄。

    Screenshot of 'Create a tenant' success message.

  5. 複製租用戶 標識碼。 您將在本教學課程稍後使用標識符值來設定 application.properties 檔案。

    Screenshot of Microsoft Entra tenant screen with 'Tenant ID' highlighted.

新增 Spring Boot 應用程式的應用程式註冊

  1. 從入口網站功能表中,選取 [應用程式註冊],然後選取 [註冊應用程式]。

  2. 指定您的應用程式,然後選取 [ 註冊]。

  3. 當應用程式註冊的頁面出現時,複製您的應用程式 (用戶端)標識碼目錄(租用戶)標識碼。 在本教學課程稍後,您將使用這些值來設定 application.properties 檔案。

    Screenshot of application with 'Application (client) ID' and 'Directory (tenant) ID' highlighted.

  4. 選取 左側瀏覽窗格中的 [憑證和秘密 ]。 然後選取 [ 新增客戶端密碼]。

    Screenshot of application 'Certificates & secrets' screen with 'New client secret' highlighted.

  5. 新增 [描述],然後選取 [到期] 列表中的持續時間。 選取 [新增]。 索引鍵的值會自動填入。

  6. 複製並儲存客戶端密碼的值,以在本教學課程稍後設定 application.properties 檔案。 (您稍後將無法擷取此值。

    Screenshot of application with new client secret highlighted.

  7. 從應用程式註冊的主頁面,選取 [驗證],然後選取 [ 新增平臺]。 然後選取 [Web 應用程式]。

    Screenshot of application Authentication screen with 'Configure platforms' section showing and Web platform highlighted.

  8. 輸入 http://localhost:8080/login/oauth2/code/ 作為新的 重新導向 URI,然後選取 [ 設定]。

    Screenshot of application Authentication screen with 'Configure Web' section showing and 'Redirect URIs' highlighted.

  9. 如果您已修改pom.xml檔案以使用早於 3.0.0 的 Microsoft Entra 入門版本:在 [隱含授與和混合式流程] 底下,選取 [標識符令牌](用於隱含和混合式流程),然後選取 [儲存]。

    Screenshot of application Authentication screen with 'ID tokens' selected.

將用戶帳戶新增至您的目錄,並將該帳戶新增至 appRole

  1. 從 Active Directory 的 [ 概觀 ] 頁面中,選取 [使用者],然後選取 [ 新增使用者]。

  2. 顯示 [使用者] 面板時,輸入 [用戶名稱] 和 [名稱]。 然後選取建立

    Screenshot of 'New user' dialog.

    注意

    當您輸入使用者名稱時,必須指定本教學課程稍早的目錄 URL。 例如:

    test-user@azuresampledirectory.onmicrosoft.com

  3. 從應用程式註冊的主頁面,選取 [應用程式角色],然後選取 [ 建立應用程式角色]。 提供表單域的值,選取 [您要啟用此應用程式角色嗎?],然後選取 [ 套用]。

    Screenshot of application 'App roles' screen with 'Create app role' pane showing.

  4. 從 Microsoft Entra 目錄的 [ 概觀 ] 頁面中,選取 [企業應用程式]。

    Screenshot of Microsoft Entra ID 'Enterprise applications' screen.

  5. 選取 [所有應用程式],然後選取您在上一個步驟中新增應用程式角色的應用程式。

    Screenshot of 'Enterprise applications' screen with 'All applications' list showing.

  6. 選取 [使用者和群組],然後選取 [ 新增使用者/群組]。

  7. 在 [使用者] 底下,選取 [未選取]。 選取您稍早建立的使用者,選取 [ 選取],然後選取 [ 指派]。 如果您稍早建立多個應用程式角色,請選取角色。

    Screenshot of application 'Add Assignment' screen with Users pane showing.

  8. 返回 [ 使用者 ] 面板,選取您的測試用戶,然後選取 [ 重設密碼],然後複製密碼。 在本教學課程稍後登入應用程式時,您將使用密碼。

    Screenshot of user with 'Temporary password' field highlighted.

設定和編譯您的應用程式

  1. 從您稍早在本教學課程中建立和下載的專案封存中擷取檔案到目錄。

  2. 流覽至 專案中的 src/main/resources 資料夾,然後在文字編輯器中開啟 application.properties 檔案。

  3. 使用您稍早建立的值來指定應用程式註冊的設定。 例如:

    # Enable related features.
    spring.cloud.azure.active-directory.enabled=true
    # Specifies your Active Directory ID:
    spring.cloud.azure.active-directory.profile.tenant-id=22222222-2222-2222-2222-222222222222
    # Specifies your App Registration's Application ID:
    spring.cloud.azure.active-directory.credential.client-id=11111111-1111-1111-1111-1111111111111111
    # Specifies your App Registration's secret key:
    spring.cloud.azure.active-directory.credential.client-secret=AbCdEfGhIjKlMnOpQrStUvWxYz==
    

    其中:

    參數 描述
    spring.cloud.azure.active-directory.enabled 啟用 spring-cloud-azure-starter-active-directory 所提供的功能
    spring.cloud.azure.active-directory.profile.tenant-id 包含您稍早的 Active Directory 目錄識別碼
    spring.cloud.azure.active-directory.credential.client-id 包含您稍早完成之應用程式註冊的應用程式識別碼。
    spring.cloud.azure.active-directory.credential.client-secret 包含 您稍早完成的應用程式註冊金鑰中的值
  4. 儲存並關閉 application.properties 檔案。

  5. 在應用程式的 Java 源資料夾中建立名為 controller 的資料夾。 例如: src/main/java/com/wingtiptoys/security/controller

  6. 在控制器資料夾中建立名為 HelloController.java的新 Java 檔案,並在文字編輯器中開啟它。

  7. 輸入下列程式代碼,然後儲存並關閉檔案:

    package com.wingtiptoys.security;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.security.access.prepost.PreAuthorize;
    
    @RestController
    public class HelloController {
         @GetMapping("Admin")
         @ResponseBody
         @PreAuthorize("hasAuthority('APPROLE_Admin')")
         public String Admin() {
             return "Admin message";
         }
    }
    

建置和測試您的應用程式

  1. 開啟命令提示字元,並將目錄變更為應用程式 pom.xml 檔案所在的資料夾。

  2. 使用 Maven 建置 Spring Boot 應用程式並加以執行。 例如:

    mvn clean package
    mvn spring-boot:run
    

    Screenshot of Maven build output.

  3. 在 Maven 建置並啟動您的應用程式之後,請在網頁瀏覽器中開啟 http://localhost:8080/Admin 。 系統應該會提示您輸入使用者名稱和密碼。

    Screenshot of application 'Sign in' dialog.

    注意

    如果這是新用戶帳戶的第一次登入,系統可能會提示您變更密碼。

    Screenshot of application 'Update your password' dialog.

  4. 成功登入之後,您應該會看到控制器中的範例「管理員 訊息」文字。

    Screenshot of application admin message.

摘要

在本教學課程中,您已使用 Microsoft Entra 入門版建立新的 Java Web 應用程式、設定新的 Microsoft Entra 租使用者、在租用戶中註冊新的應用程式,然後將您的應用程式設定為使用 Spring 批注和類別來保護 Web 應用程式。

另請參閱

下一步

若要深入瞭解 Spring 和 Azure,請繼續前往 Azure 上的 Spring 檔中心。