github/awesome-copilot / skills/spring-security
spring-security
VerifiedConfigure, test and review Spring Security 6/7 in Spring Boot 3/4 apps: SecurityFilterChain lambda DSL, method security, OAuth2 resource server with JWT, OIDC login, CSRF/CORS, password encoding, headers, actuator protection, and MockMvc security tests. Use when adding or auditing authentication and authorization.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 5.2 KB |
| references/actuator-protection.md | markdown | 2.0 KB |
| references/csrf-cors-sessions-headers.md | markdown | 3.4 KB |
| references/method-security.md | markdown | 1.4 KB |
| references/oauth2-resource-server-jwt.md | markdown | 2.9 KB |
| references/oidc-login-oauth2-client.md | markdown | 2.0 KB |
| references/password-encoding.md | markdown | 650 B |
| references/review-checklist.md | markdown | 729 B |
| references/testing-security.md | markdown | 3.8 KB |
references/oauth2-resource-server-jwt.md
View on GitHub# OAuth2 resource server with JWT
Dependency: `spring-boot-starter-oauth2-resource-server` (Boot 3.x) or `spring-boot-starter-security-oauth2-resource-server` (Boot 4.x; test companion `spring-boot-starter-security-oauth2-resource-server-test`).
## Minimal configuration
```yaml
spring:
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://issuer.example.com/realms/demo # discovers JWKS, validates iss
# or: jwk-set-uri: https://issuer.example.com/.well-known/jwks.json
audiences: my-api # validates aud (Boot 3.x+)
```
```java
http.oauth2ResourceServer(o -> o.jwt(Customizer.withDefaults()));
```
Use `issuer-uri` whenever possible: it validates the issuer and the signature. Keys and secrets come from the identity provider or environment variables, never from the repository.
## Custom JwtDecoder
```java
@Bean
JwtDecoder jwtDecoder(OAuth2TokenValidator<Jwt> validator) {
NimbusJwtDecoder decoder = JwtDecoders.fromIssuerLocation("https://issuer.example.com/realms/demo");
decoder.setJwtValidator(validator);
return decoder;
}
@Bean
OAuth2TokenValidator<Jwt> validator() {
return new DelegatingOAuth2TokenValidator<>(
JwtValidators.createDefaultWithIssuer("https://issuer.example.com/realms/demo"),
new JwtClaimValidator<List<String>>("aud", aud -> aud != null && aud.contains("my-api")));
}
```
Always validate expiry (default), issuer, and audience. Do not accept `alg: none` or symmetric keys unless deliberately designed.
## Mapping roles and scopes
By default `scope`/`scp` claims map to `SCOPE_*` authorities. For roles in a custom claim:
```java
@Bean
JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter scopes = new JwtGrantedAuthoritiesConverter();
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwt -> {
Collection<GrantedAuthority> authorities = new ArrayList<>(scopes.convert(jwt));
List<String> roles = jwt.getClaimAsStringList("roles");
if (roles != null) roles.forEach(r -> authorities.add(new SimpleGrantedAuthority("ROLE_" + r)));
return authorities;
});
return converter;
}
```
Register it: `.oauth2ResourceServer(o -> o.jwt(j -> j.jwtAuthenticationConverter(converter)))`. Provider-specific claim locations (for example Keycloak `realm_access.roles`) need explicit extraction.
## Opaque tokens
Use `.oauth2ResourceServer(o -> o.opaqueToken(Customizer.withDefaults()))` with `spring.security.oauth2.resourceserver.opaquetoken.introspection-uri`, `client-id`, and `client-secret` (from environment) when tokens are not JWTs.
## Notes
- Stateless: pair with `SessionCreationPolicy.STATELESS`.
- 401 vs 403: missing/invalid token gives 401 with `WWW-Authenticate`; valid token lacking authority gives 403.
- Short token lifetimes; never log tokens.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 467k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 467k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 467k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |