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());
}
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]
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"
}
Kann Hilfe?