Spring-使用 Twitch 进行身份验证时启动 OAuth2 的奇怪行为
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>
application.properties:
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 提供商。不确定此信息是否有帮助,但我想我会包含它。 SpringOauth 的预期行为是,当只有一个提供程序配置时,/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 Boot 无法正确加载您的配置属性,因此就好像您配置了 0 个客户端注册。
几天来,我一直在尝试通过 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>
application.properties:
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 提供商。不确定此信息是否有帮助,但我想我会包含它。 SpringOauth 的预期行为是,当只有一个提供程序配置时,/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 Boot 无法正确加载您的配置属性,因此就好像您配置了 0 个客户端注册。