The answer will depend on the format of the access token. If it is a JWT, which is the preferred option, use code similar to this:
@Configuration
public class SecurityConfiguration {
@Bean
public SecurityFilterChain filterChain(final HttpSecurity http) throws Exception {
http
.antMatcher("/api/**")
.authorizeRequests(authz -> authz
.anyRequest().authenticated())
.oauth2ResourceServer().jwt();
return http.build();
}
}
Along with configuration similar to this:
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://login.example.com/.well-known/jwks.json
INTROSPECTION
If your API receives a reference token, eg in a UUID format, then introspection will instead be required.
Spring has a similar option to implement this, though it is more commonly done in an API gateway hosted in front of the API, rather than in the API's own code.
Introspection is usually accompanied by caching of the introspection result, to avoid hammering the authorization server, which is usually a critical component.