Hej, piszę swoją pierwszą aplikację wykorzystującą zewnętrzne Api. Apka ma pobierać listę obserwujących artystów ze spotify i kilka innych, ale mam problem z logowaniem. Kiedy wchodzę na stronę wymagającą autoryzacji, loguję się kontem spotify, to spring boot tak jakby wcale nie pobiera danych o użytkowniku i po ponownym wejściu do strony wymagającej autoryzacji muszę się logować. W zasadzie nie mam dostępu do stron wymagających autoryzacji. Funkcja
@GetMapping("/callback")
public Principal callback(Principal principal){
return principal;
}
zwraca pusty obiekt. Gdy próbowałem implementować logowanie przez github nie było z tym problemu - dodaję w application.yaml tylko pola client-id i client-secret i po zalogowaniu przechodzę do zabezpieczonych adresów. W przypadku Spotify musiałem dodać dużo więcej pól, podejrzewam że tam znajduje się problem (ewentualnie w SecurityConfig).
Pom:
<?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>3.1.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.spotifytracker</groupId>
<artifactId>spotify-tracker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spotify Tracker</name>
<description>Spotify Tracker</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</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.yaml:
spring:
security:
oauth2:
client:
registration:
spotify:
client-id: <>
client-secret: <>
redirect-uri: http://localhost:8080/callback
provider: spotify
authorization-grant-type: authorization_code
provider:
spotify:
authorization-uri: https://accounts.spotify.com/authorize
token-uri: https://accounts.spotify.com/api/token
user-info-uri: https://api.spotify.com/v1/me
user-name-attribute: id
SecurityConfig:
package com.spotifytracker.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(auth->{
auth.requestMatchers("/", "/callback").permitAll();
auth.anyRequest().authenticated();
})
.oauth2Login(Customizer.withDefaults())
.build();
}
}
HomeController:
package com.spotifytracker.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
public class HomeController {
@GetMapping("/callback")
public Principal callback(Principal principal){
return principal;
}
@GetMapping("/user")
public String user(){
return "home";
}
}