簡體   English   中英

使用 Active Directory 在 Azure 上重定向 Spring OAuth2 應用程序的 URL:無效的重定向 URI 參數

[英]Redirect URL for Spring OAuth2 app on Azure with Active Directory: Invalid Redirect URI Parameter

我正在關注 Azure 文檔的以下兩個教程: https : //docs.microsoft.com/en-us/azure/java/spring-framework/deploy-spring-boot-java-app-with-maven-plugin其中展示了如何將簡單的 Spring Boot 應用程序部署到 Azure 和https://docs.microsoft.com/en-us/azure/java/spring-framework/configure-spring-boot-starter-java-app-with-azure- active-directory使用 Spring Security OAuth2 客戶端設置和使用活動目錄作為 OAuth2 服務器。 基本上我只是將 OAuth2 依賴項添加到 Maven,一個 WebSecurityConfig 類,如第二個文檔中所示,另外還有 azure.activedirectoy 和 spring.security 屬性。

當應用程序剛從我的本地計算機運行時,登錄和重定向工作正常。 但是當應用程序部署到 Azure 時,我收到一個應用程序錯誤消息:無效的重定向 URI 參數。 我想我已經正確地將重定向 uri 設置為

https://{baseHost}{basePort}{basePath}/login/oauth2/code/azure

在應用程序屬性中以及在我的 Active Directory 中的應用程序注冊中。

據我所知,授權請求使用了正確的參數:

response_type: code
client_id: 4b5fbcfd-c35f-4bab-bc45-374c7e1dead8
scope: openid https://graph.microsoft.com/user.read
state: yMvo62R-6vgjETSGr_mnh4iIMZimVnFYZRyiGFaOPtE=
redirect_uri: https://myappname.azurewebsites.net/login/oauth2/code/azure
nonce: FUXJ5GoJ2NuNVx2ORU70YCqnJkEj8FRYHEJYMutEQzo

那么,無效的重定向 URI 參數可能是什么,我該如何更改?

我遵循了這兩個教程,它在本地環境中運行良好,在 Azure webapp 上,我遇到了重定向 url 不匹配錯誤。

原因是redirect_uri 總是以http 開頭。 server.forward-headers-strategy=native添加server.forward-headers-strategy=native后,它可以工作。 (我使用的是彈簧靴 2.2)

這里是 pom.xml 供您參考。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <parent> 
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.2.4.RELEASE</version>  
    <relativePath/>  
    <!-- lookup parent from repository --> 
  </parent>  
  <groupId>com.example.ad</groupId>  
  <artifactId>ad</artifactId>  
  <version>0.0.1-SNAPSHOT</version>  
  <name>ad</name>  
  <description>Demo project for Spring Boot</description>  
  <properties> 
    <java.version>1.8</java.version>  
    <azure.version>2.2.0</azure.version> 
  </properties>  
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.boot</groupId>  
      <artifactId>spring-boot-starter-security</artifactId> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework.boot</groupId>  
      <artifactId>spring-boot-starter-web</artifactId> 
    </dependency>  
    <dependency> 
      <groupId>com.microsoft.azure</groupId>  
      <artifactId>azure-active-directory-spring-boot-starter</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.security</groupId>  
      <artifactId>spring-security-test</artifactId>  
      <scope>test</scope> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework.security</groupId>  
      <artifactId>spring-security-oauth2-client</artifactId> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework.security</groupId>  
      <artifactId>spring-security-oauth2-jose</artifactId> 
    </dependency> 
  </dependencies>  
  <dependencyManagement> 
    <dependencies> 
      <dependency> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-spring-boot-bom</artifactId>  
        <version>${azure.version}</version>  
        <type>pom</type>  
        <scope>import</scope> 
      </dependency> 
    </dependencies> 
  </dependencyManagement>  
  <build> 
    <plugins> 
      <plugin> 
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin>  
      <plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>1.9.0</version>  
        <configuration>
          <schemaVersion>V2</schemaVersion>
          <resourceGroup>ad-1582615028467-rg</resourceGroup>
          <appName>ad-1582615028467</appName>
          <pricingTier>P1v2</pricingTier>
          <region>westeurope</region>
          <runtime>
            <os>linux</os>
            <javaVersion>jre8</javaVersion>
            <webContainer>jre8</webContainer>
          </runtime>
            <appSettings>
                <property>
                    <name>JAVA_OPTS</name>
                    <value>-Dserver.port=80</value>
                </property>
            </appSettings>
          <deployment>
            <resources>
              <resource>
                <directory>${project.basedir}/target</directory>
                <includes>
                  <include>*.jar</include>
                </includes>
              </resource>
            </resources>
          </deployment>
        </configuration>
      </plugin> 
    </plugins> 
  </build> 
</project>

最終對我有用的是以下 application.yml。

server:
  forward-headers-strategy: FRAMEWORK
  port: 8080
  error:
    include-message: "always" ## You want to be able to supply meaningful error resolution hints to end users.
  servlet:
    context-path: /myappcontextpath

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM