Skip to content
Back to skills

Spring Security

ASecurity

Spring Security 6 patterns for authentication, authorization, and OAuth2

  • 57 stars
  • 0 votes
  • 0 copies
  • 5 views
  • Added October 17, 2026
securityjavaspringtestingapisecurity

Works with

  • api

Security analysis

A100/100

Scanned October 17, 2026

npx -y skills add diegosouzapw/awesome-omni-skill --skill spring-security --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Spring Security?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Spring Security
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/diegosouzapw-spring-security/badge)](https://www.skillsdirectory.com/skills/diegosouzapw-spring-security)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: spring-security
description: Spring Security 6 patterns for authentication, authorization, and OAuth2
keywords:
  - spring-security
  - security
  - authentication
  - authorization
  - oauth2
  - jwt
  - filter
  - csrf
  - method security
filePatterns:
  - "*.java"
  - "application.yml"
  - "application.properties"
frameworks:
  - spring-security
  - spring-boot
tokenCount: 3000
version: 1.0.0
---

# Spring Security Patterns

> Spring Security 6 patterns for securing APIs.
> Favor explicit security rules and least privilege.

## Selective Reading Rule

Read only files relevant to the request. Use the content map to focus.

## Content Map

| File | Description | When to Read |
|------|-------------|--------------|
| `http-security.md` | SecurityFilterChain, HttpSecurity | Basic config |
| `jwt.md` | JWT auth, filters | Token-based APIs |
| `oauth2.md` | OAuth2 login/resource server | OAuth flows |
| `method-security.md` | @PreAuthorize, @Secured | Method-level rules |

## Core Patterns

### 1. SecurityFilterChain

```java
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http
        .csrf(csrf -> csrf.disable())
        .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
        .authorizeHttpRequests(auth -> auth
            .requestMatchers("/actuator/**").permitAll()
            .requestMatchers(HttpMethod.POST, "/api/v1/auth/**").permitAll()
            .anyRequest().authenticated()
        )
        .build();
}
```

### 2. JWT Authentication Filter

```java
public class JwtAuthFilter extends OncePerRequestFilter {

    private final JwtService jwtService;

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain) throws IOException, ServletException {
        String token = resolveToken(request);
        if (token != null && jwtService.isValid(token)) {
            Authentication auth = jwtService.getAuthentication(token);
            SecurityContextHolder.getContext().setAuthentication(auth);
        }
        filterChain.doFilter(request, response);
    }
}
```

### 3. Method Security

```java
@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(String id) {
    userRepository.deleteById(id);
}
```

## Decision Checklist

- Stateless API? -> JWT + stateless session
- Admin-only endpoints? -> @PreAuthorize
- Public endpoints whitelisted?
- Secrets stored outside code?

## Anti-Patterns

| Anti-Pattern | Why Bad | Better Approach |
|--------------|---------|-----------------|
| PermitAll on broad paths | Security holes | Explicit allow list |
| Storing secrets in code | Leaks | Env variables or vault |
| Mixing auth and business logic | Hard to maintain | Dedicated filters/services |

## Related Skills

| Need | Skill |
|------|-------|
| Core Spring patterns | `@[skills/spring-boot-patterns]` |
| Testing | `@[skills/spring-testing]` |

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in security →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.