rrezartprebreza/spring-boot-skills / skills/spring-boot-4/oauth2-resource-server
oauth2-resource-server
VerifiedUse when configuring Spring Boot as an OAuth2 resource server, validating JWTs from an external auth provider (Keycloak, Auth0, Okta, Cognito), extracting claims, or implementing scope-based authorization.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.3 KB |
| agents/openai.yaml | config | 207 B |
| examples/bad-resource-server-config.java | other | 2.1 KB |
| examples/good-resource-server-config.java | other | 1.6 KB |
| templates/ExternalIdentity.java | other | 751 B |
| templates/JwtClaimExtractor.java | other | 2.2 KB |
| templates/ResourceServerConfig.java | other | 3.3 KB |
templates/JwtClaimExtractor.java
View on GitHubpackage com.example.config;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Extracts claims from the current JWT token.
* Inject this into services that need to know the authenticated user.
*/
@Component
public class JwtClaimExtractor {
public ExternalIdentity getIdentity() {
return ExternalIdentity.from(getJwt());
}
// Opaque provider id ("auth0|...", Okta "00u..."): map it to your user record, don't parse it.
public String getSubject() {
return getClaim("sub");
}
public String getEmail() {
return getClaim("email");
}
public String getUsername() {
return getClaim("preferred_username");
}
/**
* Extract roles from standard OAuth2 scope claim.
*/
public List<String> getScopes() {
String scope = getClaim("scope");
return scope != null ? List.of(scope.split(" ")) : List.of();
}
/**
* Extract roles from Keycloak's nested realm_access.roles claim.
* Only needed when using Keycloak as the identity provider.
*/
@SuppressWarnings("unchecked")
public List<String> getKeycloakRoles() {
Jwt jwt = getJwt();
Map<String, Object> realmAccess = jwt.getClaim("realm_access");
if (realmAccess == null) return Collections.emptyList();
Object roles = realmAccess.get("roles");
return roles instanceof List ? (List<String>) roles : Collections.emptyList();
}
private String getClaim(String claimName) {
return getJwt().getClaimAsString(claimName);
}
private Jwt getJwt() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()
|| !(authentication.getPrincipal() instanceof Jwt jwt)) {
throw new AuthenticationCredentialsNotFoundException("Authenticated JWT required");
}
return jwt;
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |