首页 > 解决方案 > JHipster 7.1.0 - 结合 Azure AD 身份验证

问题描述

我目前正在尝试为我的单片 JHipster 应用程序设置 Azure AD OIDC 身份验证。

我之前在 Java 1.8 上使用 JHipster 3.9.0 生成了一个单片 JHipster 应用程序,并使用azure-active-directory-spring-boot-starterversion成功合并了 Azure AD 身份验证2.3.5

自从我创建了最新的 JHipster 应用程序后,Azure 发布了一个不同的 Spring Boot 启动器,用于他们的 Azure AD 集成,名为azure-spring-boot-starter-active-directory. 我目前正在使用3.6.1这个启动器的版本以及 JHipster7.1.0和 Java 11。现在,我的 JHipster 应用程序遇到了问题。

在导航到 http://localhost:8080 后单击登录按钮后,URL 会按预期导航到 Azure AD 身份验证 URL (http://localhost:8080/login/oauth2/code/azure),然后是 JHipster立即将我重定向到 404 页面,而不显示 Microsoft 登录屏幕。

我查看了“网络”选项卡,可以看到正在对 /api/account 进行 API 调用并返回 500 响应,因为根据我的控制台输出找不到我的用户。这似乎是 JHipster 没有对 Azure AD 进行身份验证并完全绕过身份验证。任何想法为什么我的应用程序会发生这种行为?

我修改login()了 login.service.ts 中的函数以重定向到 http://localhost:8080/login/oauth2/code/azure 并将以下内容添加到 application.yml 文件中:

spring:
  security:
    oauth2:
      client:
        registration:
          azure:
            client-id: <client_id>
            client-secret: <client_secret>
.....

azure:
  activedirectory:
    tenant-id: <tenant_id>
    client-id: <client_id>
    client-secret: <client_secret>
    user-group:
      allowed-groups: <allowed_group_name>
  b2c:
    reply-url: http://localhost:8080
    logout-success-url: http://localhost:8080

这是我的 pom 文件的片段:

    <properties>
        <!-- Build properties -->
        <maven.version>3.3.9</maven.version>
        <java.version>11</java.version>
        <node.version>v14.17.1</node.version>
        <npm.version>7.18.1</npm.version>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <start-class>com.tmobile.pacbot.AdminApp</start-class>
        <argLine>-Djava.security.egd=file:/dev/./urandom -Xmx256m</argLine>
        <m2e.apt.activation>jdt_apt</m2e.apt.activation>
        <run.addResources>false</run.addResources>
        <!-- These remain empty unless the corresponding profile is active -->
        <profile.no-liquibase />
        <profile.api-docs />
        <profile.tls />

        <!-- Dependency versions -->
        <jhipster-dependencies.version>7.1.0</jhipster-dependencies.version>
        <!-- The spring-boot version should match the one managed by
        https://mvnrepository.com/artifact/tech.jhipster/jhipster-dependencies/${jhipster-dependencies.version} -->
        <spring-boot.version>2.4.7</spring-boot.version>
        <!-- The hibernate version should match the one managed by
        https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/${spring-boot.version} -->
        <hibernate.version>5.4.32.Final</hibernate.version>
        <!-- The javassist version should match the one managed by
        https://mvnrepository.com/artifact/org.hibernate/hibernate-core/${hibernate.version} -->
        <javassist.version>3.27.0-GA</javassist.version>
        <!-- The liquibase version should match the one managed by
        https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/${spring-boot.version} -->
        <liquibase.version>4.3.5</liquibase.version>
        <liquibase-hibernate5.version>4.3.5</liquibase-hibernate5.version>
        <h2.version>1.4.200</h2.version>
        <validation-api.version>2.0.1.Final</validation-api.version>
        <jaxb-runtime.version>2.3.3</jaxb-runtime.version>
        <archunit-junit5.version>0.19.0</archunit-junit5.version>
        <mapstruct.version>1.4.2.Final</mapstruct.version>
        <!-- Plugin versions -->
        <maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
        <maven-site-plugin.version>3.9.1</maven-site-plugin.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <maven-javadoc-plugin.version>3.3.0</maven-javadoc-plugin.version>
        <maven-eclipse-plugin.version>2.10</maven-eclipse-plugin.version>
        <maven-enforcer-plugin.version>3.0.0-M3</maven-enforcer-plugin.version>
        <maven-failsafe-plugin.version>3.0.0-M5</maven-failsafe-plugin.version>
        <maven-idea-plugin.version>2.2.1</maven-idea-plugin.version>
        <maven-resources-plugin.version>3.2.0</maven-resources-plugin.version>
        <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
        <maven-war-plugin.version>3.3.1</maven-war-plugin.version>
        <maven-checkstyle-plugin.version>3.1.2</maven-checkstyle-plugin.version>
        <checkstyle.version>8.43</checkstyle.version>
        <nohttp-checkstyle.version>0.0.8</nohttp-checkstyle.version>
        <frontend-maven-plugin.version>1.12.0</frontend-maven-plugin.version>
        <checksum-maven-plugin.version>1.10</checksum-maven-plugin.version>
        <maven-antrun-plugin.version>3.0.0</maven-antrun-plugin.version>
        <git-commit-id-plugin.version>4.0.5</git-commit-id-plugin.version>
        <jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
        <jib-maven-plugin.version>3.1.1</jib-maven-plugin.version>
        <lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
        <properties-maven-plugin.version>1.0.0</properties-maven-plugin.version>
        <sonar-maven-plugin.version>3.9.0.2155</sonar-maven-plugin.version>
        <!-- jhipster-needle-maven-property -->
        <azure.version>3.6.1</azure.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>tech.jhipster</groupId>
                <artifactId>jhipster-dependencies</artifactId>
                <version>${jhipster-dependencies.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- jhipster-needle-maven-add-dependency-management -->
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>azure-spring-boot-bom</artifactId>
                <version>${azure.version}</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>tech.jhipster</groupId>
            <artifactId>jhipster-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hibernate5</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-hppc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-oas</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-bean-validators</artifactId>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>mysql</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <!-- Inherited version from Spring Boot can't be used because of regressions -->
            <version>${liquibase.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${mapstruct.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader-tools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.tngtech.archunit</groupId>
            <artifactId>archunit-junit5-api</artifactId>
            <version>${archunit-junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- Adding the engine dependency to the surefire-plugin unfortunately does not work in the current version. -->
        <!-- https://www.archunit.org/userguide/html/000_Index.html#_junit_5 -->
        <dependency>
            <groupId>com.tngtech.archunit</groupId>
            <artifactId>archunit-junit5-engine</artifactId>
            <version>${archunit-junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.zalando</groupId>
            <artifactId>problem-spring-web</artifactId>
        </dependency>
        <!-- Spring Security OAuth 2.0 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <!-- Spring Cloud -->

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-data</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <dependency>
            <groupId>io.dropwizard.metrics</groupId>
            <artifactId>metrics-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>azure-spring-boot-starter-active-directory</artifactId>
        </dependency>
    </dependencies>

这是我在单击登录后看到的错误屏幕: jhipster-404-error

标签: jhipster

解决方案


推荐阅读