1 λΆ„ μ†Œμš”

1. OAuth 2.0 Resource Server μ†Œκ°œPermalink

κ°œμš”Permalink

  • OAuth 2.0 인가 ν”„λ ˆμž„μ›Œν¬μ˜ μ—­ν•  쀑 ν΄λΌμ΄μ–ΈνŠΈ 및 μΈκ°€μ„œλ²„μ™€μ˜ 톡신을 λ‹΄λ‹Ήν•˜λŠ” λ¦¬μ†ŒμŠ€ μ„œλ²„μ˜ κΈ°λŠ₯을 ν•„ν„° 기반으둜 κ΅¬ν˜„ν•œ λͺ¨λ“ˆ
  • κ°„λ‹¨ν•œ μ„€μ •λ§ŒμœΌλ‘œ ν΄λΌμ΄μ–ΈνŠΈμ˜ λ¦¬μ†ŒμŠ€ μ ‘κ·Ό μ œν•œ, 토큰 검증을 μœ„ν•œ μΈκ°€μ„œλ²„μ™€μ˜ 톡신 λ“±μ˜ κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ‹€
  • μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ κΆŒν•œ 관리λ₯Ό 별도 인가 μ„œλ²„μ— μœ„μž„ν•˜λŠ” κ²½μš°μ— μ‚¬μš©ν•  수 있으며 λ¦¬μ†ŒμŠ€ μ„œλ²„λŠ” μš”μ²­μ„ 인가할 λ•Œ 이 인가 μ„œλ²„μ— λ¬Όμ–΄λ³Ό 수 μžˆλ‹€

OAuth2ResourceServerPermalink

  • ν΄λΌμ΄μ–ΈνŠΈμ˜ 접근을 μ œν•œν•˜λŠ” 인가 정책을 μ„€μ •ν•œλ‹€
  • μΈκ°€μ„œλ²„μ—μ„œ λ°œκΈ‰ν•œ Access Token 의 μœ νš¨μ„±μ„ κ²€μ¦ν•˜κ³  μ ‘κ·Ό λ²”μœ„μ— 따라 μ μ ˆν•œ μžμ›μ„ μ „λ‹¬ν•˜λ„λ‘ μ„€μ •ν•œλ‹€

JWTPermalink

  • JWT 둜 μ „λ‹¬λ˜λŠ” 토큰을 κ²€μ¦ν•˜κΈ° μœ„ν•œ JwtDecoder, BearerTokenAuthenticationFilter, JwtAuthenticationProvider λ“±μ˜ 클래슀 λͺ¨λΈλ“€μ„ μ œκ³΅ν•œλ‹€
  • 자체 검증 ν”„λ‘œμ„ΈμŠ€λ₯Ό μ§€μ›ν•œλ‹€

OpaquePermalink

  • μΈκ°€μ„œλ²„μ˜ introspection μ—”λ“œ 포인트둜 검증할 수 μžˆλŠ” Opaque 토큰을 μ§€μ›ν•œλ‹€
  • μ‹€μ‹œκ°„μœΌλ‘œ ν† ν°μ˜ ν™œμ„±ν™” μ—¬λΆ€λ₯Ό 확인할 수 μžˆλ‹€

βœ“ μ˜μ‘΄μ„± μΆ”κ°€Permalink

  • λ¦¬μ†ŒμŠ€ μ„œλ²„λ₯Ό μ§€μ›ν•˜λŠ” μ½”λ“œλŠ” λŒ€λΆ€λΆ„ spring-security-oauth2-resource-server 에 λ“€μ–΄μžˆλ‹€. κ·ΈλŸ¬λ‚˜ JWTλ₯Ό λ””μ½”λ”©ν•˜κ³  κ²€μ¦ν•˜λŠ” λ‘œμ§μ€ spring-security-oauth2-jose 에 μžˆλ‹€. λ”°λΌμ„œ λ¦¬μ†ŒμŠ€ μ„œλ²„κ°€ μ‚¬μš©ν•  Bearer 토큰을 JWT둜 μΈμ½”λ”©ν•œλ‹€λ©΄ 두 λͺ¨λ“ˆμ΄ λͺ¨λ‘ ν•„μš”ν•˜λ‹€.
    dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-security'
      implementation 'org.springframework.boot:spring-boot-starter-web'
      implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
      testImplementation 'org.springframework.boot:spring-boot-starter-test'
      testImplementation 'org.springframework.security:spring-security-test'
    }
    

2. application.yml / OAuth2ResourceServerPropertiesPermalink

ν™˜κ²½μ„€μ • 파일 ꡬ성Permalink

  1. application. yml ν™˜κ²½μ„€μ • νŒŒμΌμ— μ•„λ¬΄λŸ° 섀정을 ν•˜μ§€ μ•Šκ³  기동결과λ₯Ό ν™•μΈν•œλ‹€
  2. application. yml ν™˜κ²½μ„€μ • νŒŒμΌμ— OAuth2 ResourceServer κΈ°λ³Έμ„€μ • ν›„ 기동결과 ν™•μΈν•œλ‹€

OAuth2ResourceServerProperties (prefix = β€œspring.security.oauth2.resourceserverβ€œ)Permalink

  • jwkSetUri 은 인가 μ„œλ²„μ—μ„œ μ„œλͺ…ν•œ ν† ν°μ˜ μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚Έλ‹€
  • issuerUri λŠ” μΈκ°€μ„œλ²„μ˜ μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚Έλ‹€
  • publicKeyLocation 은 κ³΅κ°œν‚€λ₯Ό κ°€μ§€κ³  μžˆλŠ” 파일의 μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚Έλ‹€
  • jwsAlgorithm 은 JWT 토큰을 μ„œλͺ…ν•˜κΈ° μœ„ν•œ μ•Œκ³ λ¦¬μ¦˜μ„ λ‚˜νƒ€λ‚Έλ‹€
  • IntrospectionUri λŠ” 토큰을 κ²€μ¦ν•˜κΈ° μœ„ν•œ introspection μ—”λ“œ 포인트λ₯Ό λ‚˜νƒ€λ‚Έλ‹€

application. yml μ„€μ •Permalink

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8080/realms/oauth2
          jwk-set-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/certs
  • issuer-uri = http://localhost:8080λŠ” 인가 μ„œλ²„κ°€ λ°œκΈ‰ν•  JWT ν† ν°μ˜ iss ν΄λ ˆμž„μ— μΆ”κ°€λ˜λŠ” κ°’μœΌλ‘œμ„œ λ°œκΈ‰μžλ₯Ό λ‚˜νƒ€λ‚Έλ‹€.
  • jwk-set-uri = http://localhost:8080/oauth2/jwksλŠ” 인가 μ„œλ²„κ°€ λ°œκΈ‰ν•œ JWT ν† ν°μ˜ κ³΅κ°œν‚€ 정보λ₯Ό 검색할 수 μžˆλŠ” μ—”λ“œν¬μΈνŠΈλ₯Ό λ‚˜νƒ€λ‚Έλ‹€.
  • λ¦¬μ†ŒμŠ€ μ„œλ²„λŠ” 자체 검증 섀정에도 이 속성을 μ‚¬μš©ν•˜λ©°, 이 μ†μ„±μœΌλ‘œ 인가 μ„œλ²„μ˜ κ³΅κ°œν‚€λ₯Ό μ°Ύκ³ , 건내 받은 JWT의 μœ νš¨μ„±μ„ κ²€μ‚¬ν•œλ‹€.

인가 μ„œλ²„ 메타데이터 μ—”λ“œν¬μΈνŠΈPermalink

  • issuer-uri ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ 인가 μ„œλ²„κ°€ μ§€μ›ν•˜λŠ” μ—”λ“œν¬μΈνŠΈλŠ” λ°˜λ“œμ‹œ μ…‹ 쀑 ν•˜λ‚˜μ—¬μ•Ό ν•œλ‹€.
    • https://localhost:8080/issuer/.well-known/openid-configuration
    • https://localhost:8080/.well-known/openid-configuration/issuer
    • https://localhost:8080/.well-known/oauth-authorization-server/issuer
  • 이 μ—”λ“œν¬μΈνŠΈλŠ” Provider μ„€μ • μ—”λ“œν¬μΈνŠΈ λ˜λŠ” 인가 μ„œλ²„ 메타데이터 μ—”λ“œν¬μΈνŠΈλΌκ³  ν•œλ‹€.

ResourceServer#Jwt application.yml μ„€μ • ν•­λͺ©Permalink

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:8080/realms/oauth2 # μ„œλΉ„μŠ€ κ³΅κΈ‰μž μœ„μΉ˜
          jwk-set-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/certs # OAuth 2.0 JwkSetUri μ—”λ“œ 포인트
          jws-algorithms: RSA256 # OAuth 2.0 JWS μ„œλͺ… μ•Œκ³ λ¦¬μ¦˜
          audiences: http://localhost:8081 # μˆ˜μ‹ μž μœ„μΉ˜
          public-key-location: classpath:certs/publicKey.txt # OAuth 2.0 JWS 검증을 μœ„ν•œ PublicKey 파일 μœ„μΉ˜
  • μœ„ 속성 쀑에 issuer-uri ν˜Ήμ€ jwk-set-uri ν•œ κ°œλŠ” λ°˜λ“œμ‹œ ν•„μš”ν•˜κ³  λ‚˜λ¨Έμ§€ 속성은 ν•„μš”μ‹œ μ„€μ •ν•˜λ©΄ λœλ‹€

3. AuthenticationEntryPointPermalink

4. μžλ™μ„€μ •μ— μ˜ν•œ μ΄ˆκΈ°ν™” κ³Όμ •Permalink

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ:

λŒ“κΈ€λ‚¨κΈ°κΈ°