agentleFS
KaratussKaratuss/bstack/skills/security/SKILL.md

security

Skill0 starsChanged 7 months agoMITView on GitHub

Spring Security filter chain·JWT·OAuth2·RBAC 구현. 인증/인가 설계·CORS/CSRF/XSS 방어 시 사용.

78 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use cost-reducer, trigger-dev and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • daianepepes-labSkill

    cost-reducer

    Analyze and reduce costs across cloud infrastructure, code, and services. Use when asked to cut costs, optimize spending, reduce bills, or improve cost efficiency.

    Also in 5 projects that use this

  • xfstudioSkill

    trigger-dev

    Trigger.dev expert for background jobs, AI workflows, and reliable async execution with excellent developer experience and TypeScript-first design. Use when: trigger.dev, trigger dev, background task, ai background job, long running task.

    Also in 4 projects that use this

  • ffsshhttiikkSkill

    scalability

    Core foundational concepts and principles for Scalability

    Also in 4 projects that use this

  • daianepepes-labSkill

    self-healing

    Detect, diagnose, and automatically fix recurring errors, memory issues, and system degradation. Use when a system has recurring failures, memory leaks, performance degradation, or needs automatic recovery mechanisms.

    Also in 5 projects that use this

  • xfstudioSkill

    customer-support

    Elite AI-powered customer support specialist mastering

    Also in 4 projects that use this

  • ffsshhttiikkSkill

    cicd

    Continuous integration and delivery pipelines

    Also in 4 projects that use this

  • +1 more from daianepepes-lab

The file

Files
SKILL.md

/security — Spring Security / 인증 / 인가 스킬

Spring Security filter chain, JWT, OAuth2, RBAC 관련 구현과 설계를 다룬다.

When to use

  • Spring Security 설정 구조화
  • JWT 발급/검증 로직 구현
  • OAuth2 소셜 로그인 연동
  • 역할 기반 접근 제어(RBAC) 설계
  • CORS, CSRF, XSS 방어 설정
  • API 엔드포인트별 접근 권한 설정

Spring Security 6.x 기본 설정

@Configuration
@EnableWebSecurity
@EnableMethodSecurity   // @PreAuthorize 활성화
public class SecurityConfig {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        return http
            .csrf(AbstractHttpConfigurer::disable)         // Stateless API
            .sessionManagement(s ->
                s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/api/v1/auth/**").permitAll()
                .requestMatchers("/actuator/health").permitAll()
                .requestMatchers(HttpMethod.GET, "/api/v1/public/**").permitAll()
                .anyRequest().authenticated()
            )
            .addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class)
            .exceptionHandling(ex -> ex
                .authenticationEntryPoint(jwtAuthEntryPoint)
                .accessDeniedHandler(jwtAccessDeniedHandler)
            )
            .build();
    }
}

JWT 구현 패턴

@Component
public class JwtTokenProvider {

    @Value("${jwt.secret}")
    private String secretKey;

    @Value("${jwt.expiration}")
    private long expirationMs;

    public String generateToken(Authentication authentication) {
        return Jwts.builder()
            .subject(authentication.getName())
            .issuedAt(new Date())
            .expiration(new Date(System.currentTimeMillis() + expirationMs))
            .signWith(getSigningKey())
            .compact();
    }

    public boolean validateToken(String token) {
        try {
            Jwts.parser().verifyWith(getSigningKey()).build().parseSignedClaims(token);
            return true;
        } catch (JwtException e) {
            return false;
        }
    }

    private SecretKey getSigningKey() {
        return Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));
    }
}

RBAC 설계

// 역할 계층 정의
@Bean
public RoleHierarchy roleHierarchy() {
    return RoleHierarchyImpl.fromHierarchy("""
        ROLE_ADMIN > ROLE_MANAGER
        ROLE_MANAGER > ROLE_USER
        """);
}

// 메서드 레벨 접근 제어
@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long userId) { ... }

@PreAuthorize("hasRole('USER') and #userId == authentication.principal.id")
public UserProfile getMyProfile(Long userId) { ... }

보안 설정 체크리스트

  • JWT secret은 환경변수로 관리 (코드에 하드코딩 금지)
  • 토큰 만료 시간 설정 (Access: 15-30분, Refresh: 7-14일)
  • Refresh Token은 DB 또는 Redis에 저장 (revocation 지원)
  • HTTPS 전용 (Strict-Transport-Security 헤더)
  • CORS whitelist 명시 (* 금지)
  • Actuator 엔드포인트 접근 제한
  • SQL Injection: JPA/QueryDSL 사용 (Native Query 최소화)
  • XSS: Response 헤더 X-Content-Type-Options: nosniff
  • 비밀번호: BCryptPasswordEncoder (strength 10+)

자주 발생하는 문제

증상원인해결
401 on valid tokenFilter 순서 문제addFilterBefore 순서 확인
CORS preflight 실패SecurityFilterChain에서 OPTIONS 차단.requestMatchers(HttpMethod.OPTIONS).permitAll()
@PreAuthorize 미작동@EnableMethodSecurity 누락설정 클래스에 추가
Self-invocation으로 권한 우회Spring AOP 프록시 한계별도 Bean 분리

References

  • docs/STYLE_GUIDE.md — 원칙 (Karpathy 4 + 동료 협업 + 정량)
  • docs/RED_FLAGS.md — 안티패턴
  • docs/LAYER_RULES.md — 레이어 규칙

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.