I am trying to implement a simple Spring OAuth2 resource server using google as OAuth server.
Basically, I've been following guides like this one spring-oauth2-with-google
application.yml:
spring:
security:
oauth2:
client:
registration:
google:
client-id: *******.apps.googleusercontent.com
client-secret:********_
scope:
- email
- profile
- openid
resourceserver:
jwt:
issuer-uri: https://accounts.google.com
jwk-set-uri: https://www.googleapis.com/oauth2/v3/certs
SecurityConfig.java:
@Configuration
public class SecurityConfig {
@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.httpBasic().disable()
.formLogin(AbstractHttpConfigurer::disable)
.csrf(AbstractHttpConfigurer::disable)
.authorizeRequests(authorize -> authorize
.anyRequest().authenticated()
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
.sessionManagement(sessionManagement ->
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
;
return http.build();
}
}
UserController.java
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
@GetMapping("/{id}")
public void getUser(@PathVariable String id) {
System.out.println("Id: " + id);
}
}
I am able to get the google JWT through postman as described in the guide, but no matter how hard I try, when I try to consume my end point, through postman, the response is always 401. I have already tried to set a space between the Bearer keyword and token_id.
Error in postman:
Bearer error="invalid_token", error_description="Invalid token", error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"
but if i review the token in google token info the result seems ok:
"issued_to": "263667859573-jve8vplquh7qn4ft7aaj1t1m9boaq5d6.apps.googleusercontent.com",
"audience": "263667859573-jve8vplquh7qn4ft7aaj1t1m9boaq5d6.apps.googleusercontent.com",
"user_id": "112897290372529438679",
"scope": "openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
"expires_in": 3296,
"email": "javier@email.com",
"verified_email": true,
"access_type": "online"