Google Login kann in Spring Security nicht verwendet werdenJava

Java-Forum
Guest
 Google Login kann in Spring Security nicht verwendet werden

Post by Guest »

Ich versuche, Spring Security zu verwenden, um die Google-Anmeldung für meine Anwendung zu aktivieren. Ich habe die folgende Konfiguration angewendet:

Code: Select all

@Configuration
@EnableWebSecurity(debug=true)
public class SpringConfig {

Logger logger = Logger.getLogger("MyLogger");

@Bean
public SecurityFilterChain filterChain(HttpSecurity http)  throws Exception {
http
.csrf(Customizer.withDefaults())
.oauth2Login(Customizer.withDefaults())
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
);

SecurityFilterChain chain = http.build();
return chain;
}

private ClientRegistration googleClientRegistration() {

return ClientRegistration.withRegistrationId("google")
.clientId("yyy")
.clientSecret("xxx")
.authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
.issuerUri("https://accounts.google.com")
.jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
.redirectUri("http://localhost:8080/app/login/oauth2/code/google")
.scope("openid", "email", "profile")
.tokenUri("https://oauth2.googleapis.com/token")
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.build();
}

@Bean
public ClientRegistrationRepository clientRegistrationRepository() {
return new InMemoryClientRegistrationRepository(this.googleClientRegistration());
}
Ich habe alle Werte noch einmal überprüft. Aus irgendeinem Grund, den ich nicht herausfinden konnte, erhalte ich nach der Authentifizierung bei Google immer wieder diese Fehlermeldung:

Code: Select all

[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: Error while extracting response for type [class org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse] and content type [application/json;charset=utf-8]
Ich habe eine kleine Python-Anwendung erstellt, die diesen OpenID Connect-Ablauf mit einfachen Anfragen ausführt, und sie scheint genau mit diesen Werten einwandfrei zu funktionieren. Was ist hier falsch?
Debugging
Ich habe javax.net.debug=all aktiviert, ich kann sehen, dass die Anfrage an Der Zugriffsendpunkt erhält eine gültige Antwort, die sowohl das ID-Token als auch das Zugriffstoken enthält. Das Zugriffstoken sieht so aus:

Code: Select all

{
"issued_to": "xxx.apps.googleusercontent.com",
"audience": "xxx.apps.googleusercontent.com",
"user_id": "xxx45",
"scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
"expires_in": 2864,
"email": "[email protected]",
"verified_email": true,
"access_type": "online"
}
Es scheint, dass die Fehlermeldung impliziert, dass dieses Zugriffstoken nicht in ein Objekt von OAuth2AccessTokenResponse oder etwas Ähnliches übersetzt werden kann.
Kann Hilfe?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post