0

Problem

  • 401 Unauthorized when calling an endpoint secured by Spring

Tech Stack

  • Spring Boot 3
  • Google API
  • Postman

Goal I'm building a Spring Boot 3 service that will access Google's Gmail and Calendar services. I will be using Google OAuth2 as the Authorization Server and the Spring Boot app as the Resource Server. I've followed this guide to allow Google login and secure my endpoint: https://docs.spring.io/spring-security/reference/reactive/oauth2/resource-server/jwt.html

Here's my security configuration:

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfiguration {

  @Bean
  public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    http
        .csrf(e -> e.disable())
        .httpBasic(e -> e.disable())
        .formLogin(e -> e.disable())
        .authorizeHttpRequests(authz -> authz
            .requestMatchers("/**")
            .fullyAuthenticated()
        )
        .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)
        .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
    ;
    return http.build();
  }
}

Here's my Spring configuration file:

spring:
  security:
    oauth2:
#      client:
#        registration:
#          google:
#            client-id: xxx
#            client-secret: xxx
#            scope:
#              - openid
#              - profile
#              - email
      resourceserver:
        jwt:
          # https://accounts.google.com/.well-known/openid-configuration
          issuer-uri: https://accounts.google.com
          jwk-set-uri: https://www.googleapis.com/oauth2/v3/certs
#        opaque-token:
#          introspection-uri: https://oauth2.googleapis.com/tokeninfo
#          client-id: xxx
#          client-secret: xxx

I'm using Postman for testing and have generated the access token like this: enter image description here

But when I call the endpoint, I get a 401 unauthorized exception.

In Postman, when I change the token type to idToken, it works: enter image description here

Is there a way for the JWT token to work?

Searching SO, I found:

I tried the Opaque token approach as well, but I still have the same 401 Unauthorized error.

Here's the GitHub repo https://github.com/czetsuya/spring-google-signon.

Any idea how to proceed from here?

dur
  • 15,689
  • 25
  • 79
  • 125
czetsuya
  • 4,773
  • 13
  • 53
  • 99
  • Are you sure that the token provided by Google is a JWT? Because, usually, those are opaque tokens intended to be used by Google APIs, not by your own resource servers (you might have to use an authorization server of your own with "login with Google" feature, like Keycloak, Auth0, Cognito, Azure AD, or build your own with Spring Authorization Server project) – ch4mp Apr 25 '23 at 03:07
  • That's my worry. Do you think there is a way around it? I mean without using an external Auth Server? – czetsuya Apr 25 '23 at 03:30
  • Yes, but it is very inefficient: you write an introspector using the user endpoint. Don't do that. Popping a Keycloak instance is a matter of minutes. Configuring it with a client and login with Google could take a couple of hours to someone who never did it. – ch4mp Apr 25 '23 at 04:40

0 Answers0