Skip to content

Spring Security ‐ OAuth 2.0 opaque()

Woo Jin Jang edited this page Oct 2, 2025 · 2 revisions

📚 Opaque 개념 및 프로세스 이해

  • Opaque 토큰은 인가 서버에서 호스트하는 OAuth 2.0 Introspection 엔드포인트로 검증한다.
  • Bearer 토큰은 리소스 서버에서 처리하는 자체 검증이라면 Opaque 토큰은 인가서버에서 처리하는 원격 검증이라고 볼 수 있다.

📚 토큰 검사 및 프로세스 이해

스크린샷 2025-10-03 00 36 07

OpaqueTokenIntrospector

  • 문자열 토큰을 RestTemplate를 사용해 인가 서버 엔드포인트로 요청한다.
  • 토큰이 검증되면 최종 OAuth2AuthenticatedPrincipal 타입의 객체로 디코딩하여 반환한다.
  • OAuth2AuthenticationPrincipal은 BearerTokenAuthentication의 Principal 속성에 저장된다.
스크린샷 2025-10-03 00 38 58

CustomOpaqueTokenIntrospector

  • OpaqueTokenIntrospector 인터페이스를 구현하여 커스텀한 구현체를 만들어 재정의 할 수 있다.
  • 검증 후 리턴 타입이 OAuth2AuthenticatedPrincipal이기 때문에 인가 서버에서 받아온 클레임 정보를 활용해서 여러가지 커스텀 작업이 가능하다.
public class CustomOpaqueTokenIntrospector implements OpaqueTokenIntrospector {

    private OpaqueTokenIntrospector delegate = new NimbusOpaqueTokenIntrospector("http://localhost:8080/realms/oauth2/protocol/openid-connect/introspect ", "client", "secret");
    
    public OAuth2AuthenticatedPrincipal introspect(String token) {
        OAuth2AuthenticatedPrincipal principal = this.delegate.introspect(token); // 인가서버로 부터 받은 클레임 정보를 저장하고 있다
        return new DefaultOAuth2AuthenticatedPrincipal(principal.getName(), principal.getAttributes(), extractAuthorities(principal)); // 커스텀하게 권한 매핑 한다

    }

    private Collection<GrantedAuthority> extractAuthorities(OAuth2AuthenticatedPrincipal principal) {
        List<String> scopes = principal.getAttribute(OAuth2IntrospectionClaimNames.SCOPE);
        return scopes.stream().map(SimpleGrantedAuthority::new)
                              .collect(Collectors.toList());
    }
}

📖 Java

📖 Kotlin

📖 Coroutine

📖 Spring

📖 Spring Security

📖 Spring Batch

📖 Reactive Programming

📖 Database

📖 MySQL

📖 Redis

📖 JPA

📖 QueryDsl

📖 MSA

📖 Kafka

📖 Apache Flink

  • [Apache Flink - Apache Flink Architecture]
  • [Apache Flink - Stream Processing]
  • [Apache Flink - Data Stream API & Window]
  • [Apache Flink - State Management]

📖 HTTP

📖 AWS

📖 Docker

📖 Kubernetes

📖 CI/CD

📖 Nginx

📖 Monitoring🥈

  • [Monitoring - Log Concept]
  • [Monitoring - Log Level & Filter]
  • [Monitoring - Logback]
  • [Monitoring - Log Collection with ELK Stack]
  • [Monitoring - Log Monitoring with Kibana]
  • [Monitoring - Building a Monitoring System with Spring Boot Actuator]
  • [Monitoring - Server Monitoring with Prometheus and Grafana with Discord Alerts]

📖 Test

📖 Effective Java 3/E

📖 Kotlin Academy - Effective Kotlin

📖 Kotlin Academy - 핵심편

📖 스프링으로 시작하는 리액티브 프로그래밍

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 1

📖 가상 면접 사례로 배우는 대규모 시스템 설계 기초 2

📖 Clean Code

📖 리팩토링 2판

📖 주니어 백엔드 개발자가 반드시 알아야 할 실무 지식

📖 GraphQL

Clone this wiki locally