将使用 Microsoft Entra 帐户登录添加到 Spring Web 应用

本文介绍如何开发支持 Microsoft Entra 帐户登录的 Spring Web 应用。 完成本文中的所有步骤后,Web 应用将在匿名访问时重定向到 Microsoft Entra 登录页面。 以下屏幕截图显示了 Microsoft Entra 登录页:

Screenshot of application 'Sign in' dialog.

先决条件

若要完成本文中的步骤,需要符合以下先决条件:

重要

完成本文中的步骤需要 Spring Boot 2.5 或更高版本。

使用 Spring Initialzr 创建应用

  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. 复制租户 ID。 在本教程中,稍后需使用 ID 值来配置 application.properties 文件

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

添加 Spring Boot 应用的应用程序注册

  1. 在门户菜单中选择“应用注册”,然后选择“注册应用程序”

  2. 指定应用程序,然后选择“注册”

  3. 显示应用注册的页面后,请复制“应用程序(客户端) ID”和“目录(租户) ID”。 在本教程中,稍后需使用这些值来配置 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. 输入“”作为新的“重定向 URI”,然后选择“配置”http://localhost:8080/login/oauth2/code/

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

  9. 如果修改了pom.xml文件以使用低于 3.0.0 的 Microsoft Entra 初学者版本:在隐式授予和混合流,选择 ID 令牌(用于隐式和混合流),然后选择“保存”。

    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“目录 ID”。
    spring.cloud.azure.active-directory.credential.client-id 包含前面填写的、应用注册的“应用程序 ID”。
    spring.cloud.azure.active-directory.credential.client-secret 包含前面填写的、应用注册密钥中的“值”。
  4. 保存并关闭 application.properties 文件。

  5. 在应用程序的 Java 源文件夹中创建名为“controller”的文件夹。 例如:src/main/java/com/wingtiptoys/security/controller

  6. controller 文件夹中创建名为 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 生成并启动应用程序后,请在 Web 浏览器中打开 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”文档中心。