gpt4 book ai didi

java - 尝试使用 Spring Boot 通过 Oauth2 和 Strava 验证 Web 应用程序时出错

转载 作者:行者123 更新时间:2023-12-05 07:05:04 24 4
gpt4 key购买 nike

我正在尝试使用 Strava 对想要使用我的 Spring Boot 网络应用程序的客户进行身份验证,但我遇到了这个错误:

.s.o.c.w.OAuth2LoginAuthenticationFilter : Authentication requestfailed:org.springframework.security.oauth2.core.OAuth2AuthenticationException:[invalid_token_response] An error occurred while attempting toretrieve the OAuth 2.0 Access Token Response: Could not extractresponse: no suitable HttpMessageConverter found for response type[classorg.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse]and content type [text/html]

如果能帮助我继续前进并解决这个错误,我将不胜感激。我将错误的重现简化为 2 个类:

DemoSecurity.java extending WebSecurityConfigurerAdapter 
DemoApplication.java as the entry to the application with @SpringBootApplication,

并且您需要使用 Strava ( https://www.strava.com/settings/api ) 注册应用程序以获取您的 client_secretclient_id。在 strava 中,需要将回调添加为本地主机才能运行此测试。

最后,要重现错误,您只需在 IDE 中运行应用程序并在浏览器中访问 http://localhost:8080/login。

非常感谢

这是我的application.yml:

spring:   
security:
oauth2:
client:
registration:
strava:
provider: strava-provider
client-id: XXXXX
client-secret: XXXXXXXXXXXXXXXXX
client-authentication-method: POST
authorization-grant-type: authorization_code
redirect-uri: http://localhost:8080/login/oauth2/code/
scope:
- read
provider:
strava-provider:
tokenUri: https://www.strava.com/api/v3/oauth/token/
authorizationUri: https://www.strava.com/api/v3/oauth/authorize?response_type=code

这是我的 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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</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>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

这是我的 DemoApplication.java:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

这是我的 DemoSecurity.java:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
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;

@EnableWebSecurity
@Configuration
public class DemoSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login**","/", "/error", "/webjars/**").permitAll()
.anyRequest()
.authenticated().and()
.oauth2Login()
;
}


}

最佳答案

查看您的 application.yml,我可以看到您缺少 user-info-uri 和 user-name-attribute:由于它是 oauth2 而不是 oidc,Spring 需要知道它是否可以找到用户以及用户的唯一 ID 是什么字段。我快速查看了 stravas api 文档,我认为可能是这样。

用户信息 uri:https://www.strava.com/api/v3/athlete

用户名属性:id

但您可能需要查看他们的文档或联系他们的支持团队进行确认,基本上您只是在寻找一个将返回用户详细信息的 api 调用,并确认哪个字段是用户的唯一标识符或用户名。

关于java - 尝试使用 Spring Boot 通过 Oauth2 和 Strava 验证 Web 应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62804390/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com