Strong answer
OAuth2 is a delegation framework: an authorization server issues access tokens, a client obtains them (Authorization Code with PKCE for user-facing apps, Client Credentials for service-to-service), and a resource server validates them before authorizing a request. A JWT is one token format; it is trustworthy only after signature, issuer, audience, expiry, and scopes are verified. API security also covers transport, rate limits, input validation, secrets handling, and the OWASP API risks such as broken object-level authorization.
Java example
1# application.yml (resource server)
2spring.security.oauth2.resourceserver.jwt:
3 issuer-uri: https://auth.example.com/realms/shop
4 audiences: orders-api
5
6@Bean
7JwtAuthenticationConverter jwtAuthConverter() {
8 var scopes = new JwtGrantedAuthoritiesConverter();
9 scopes.setAuthorityPrefix("SCOPE_");
10 var converter = new JwtAuthenticationConverter();
11 converter.setJwtGrantedAuthoritiesConverter(scopes);
12 return converter;
13}
14
15@PreAuthorize("hasAuthority('SCOPE_orders:write')")
16@PostMapping("/api/orders")
17OrderResponse create(@Valid @RequestBody CreateOrder cmd, @AuthenticationPrincipal Jwt jwt) {
18 return service.create(cmd, jwt.getSubject());
19}