簡體   English   中英

Spring-Boot OAuth2 使用 Twitch 進行身份驗證時的奇怪行為

[英]Spring-Boot OAuth2 Strange Behavior When Authenticating with Twitch

幾天來,我一直在嘗試通過 Spring 使用 Twitch 設置 OAuth2。 在這個過程中我已經解決了很多問題,但這個問題讓我很難過。 當我嘗試訪問我試圖要求用戶通過 Twitch 進行身份驗證的端點之一時,我被重定向到 localhost:8080/login 並顯示一個頁面,該頁面僅顯示“使用 OAuth 2.0 登錄”並且沒有其他內容在上面。 我的期望是 Spring 會自動將我重定向到 Twitch 的身份驗證門戶,然后 Twitch 在完成 OAuth 流程后會將我發送回 Spring 應用程序。 相反,我只是被顯示該頁面而沒有發生任何事情。

就目前為止為解決這個問題所做的工作......幾乎沒有。 我找不到其他人遇到這個問題,所以我認為可能存在一些問題......根據本教程https://spring.io/guides/tutorials/spring-boot-oauth2/它似乎 Spring 為許多不同的 OAuth 提供程序提供了開箱即用的功能。 我猜Twitch不是其中之一。 這讓我擔心 Twitch 后端的某些東西可能會導致 Spring 出現此問題(如果是這種情況,那么我將需要制作一個自定義身份驗證器)。 我想到的另一種可能性是 Spring 可能需要被告知將用戶重定向到哪里才能進行身份驗證。 如果是這種情況,那么我將不勝感激,因為我無法找到任何跡象表明這是需要在線完成的事情(而且我不知道該怎么做)。

我的項目中的一些重要文件:

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.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.redacted</groupId>
    <artifactId>redacted</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redacted</name>
    <description>redacted</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.44</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.4.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

應用程序屬性:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring.security.oauth2.client.registration.twitch.client-id=redacted
spring.security.oauth2.client.registration.twitch.client-secret=redacted
spring.security.oauth2.client.registration.twitch.client-authentication-method=post
spring.security.oauth2.client.registration.twitch.redirect-uri=http://localhost:8080/login/oauth2/code/twitch
spring.security.oauth2.client.registration.twitch.provider=twitch
spring.security.oauth2.client.registration.twitch.scope=user:read:email
spring.security.oauth2.client.registration.twitch.authorization-grant-type=AUTHORIZATION_CODE

spring.security.oauth2.client.provider.twitch.authorization-uri=https://id.twitch.tv/oauth2/authorize
spring.security.oauth2.client.provider.twitch.token-uri=https://id.twitch.tv/oauth2/token
spring.security.oauth2.client.provider.twitch.user-info-uri=https://id.twitch.tv/oauth2/userinfo
spring.security.oauth2.client.provider.twitch.user-name-attribute=redacted

以及我的 SpringSecurityConfiguration 文件,以防萬一:

package com.redacted;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        //This allows users to access the "/" and "/Info" endpoints without authenticating with Twitch. To go anywhere else they will have to authenticate.
        httpSecurity.antMatcher("/**").authorizeRequests().antMatchers("/", "/Info").permitAll().anyRequest().authenticated().and().oauth2Login();
    }
}

我的項目結構:

我的項目結構圖

感謝您的時間和考慮,感謝您的幫助。

-時代

編輯:

我最近發現我認為添加到這個討論中可能是謹慎的 - 當我嘗試訪問當前設置中的安全頁面時,我看到了這個屏幕: 在此處輸入圖像描述

看起來好像 Spring 通常顯示此屏幕的預期方式是這樣的: 在此處輸入圖像描述

就好像 Spring 根本沒有看到我的 Oauth 提供商。 不確定這些信息是否有幫助,但我想我會包括在內。 Oauth 的 Springs 預期行為是,當僅配置一個提供程序時,將完全跳過 /login 頁面。 我實際上更希望展示這種行為(但鑒於它沒有看到提供者,我認為它會顯示頁面)。

嘗試在您的configure方法中添加.oauth2Client()而不是.oauth2Login()

我能夠重現這個問題。 您已指定:

spring.security.oauth2.client.registration.twitch.authorization-grant-type=AUTHORIZATION_CODE

雖然它應該是:

spring.security.oauth2.client.registration.twitch.authorization-grant-type=authorization_code

Spring 引導無法正確加載您的配置屬性,因此就好像您配置了 0 個客戶端注冊。

暫無
暫無

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

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