@AuthenticationPrincipal

Reference: Spring Docs - AuthenticationPrincipal

  • ํ˜„์žฌ ๋กœ๊ทธ์ธํ•œ ์œ ์ €๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด UserDetails ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์œ ์ € ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๋Š” annotation

    • UserDetails ๋ž€?

      • ์‚ฌ์šฉ์ž ์ •๋ณด (username, password ๋“ฑ)์„ ๊ฐ€์ง€๋Š” interface

        • ํ•ด๋‹น interface๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ๋กœ๊ทธ์ธ์— ์‚ฌ์šฉํ•  class๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค

      • Authentication ์œผ๋กœ ์บก์Šํ™”ํ•˜์—ฌ ์ €์žฅ๋œ๋‹ค

        • ์ฆ‰, UserDetails ๊ตฌํ˜„์ฒด ์ •๋ณด๋Š” Spring Security Context์— ์ €์žฅ๋œ Authentication ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ ธ๊ฐ€๊ณ , ์‚ฌ์šฉ์ž ์ •๋ณด๋Š” Authentication ๊ฐ์ฒด์— ๋‹ด๊ฒจ ์žˆ๋‹ค

      โ†’ @AuthenticationPrincipal ์„ ์‚ฌ์šฉํ•˜๋ฉด Authentication ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”์ถœํ•ด์„œ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

  • UserDetails ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋‹ด์€ class๋„ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฒƒ์€ Spring Security์˜ Principal ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์ฃผ์ž…๋ฐ›๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค

    • ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋‹ด์€ class๊ฐ€ Principal ์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํ•ด๋‹น class๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ์ž…๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค

    • Authentication.getPrincipal() ๋ฅผ method argument๋กœ ์‚ฌ์šฉํ•œ๋‹ค!

      • java.lang.Object getPrincipal() ๋ž€?

        • ์ธ์ฆ๋˜๋Š” ์ฃผ์ฒด์˜ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค

          Returns:
          the Principal being authenticated or the authenticated principal after authentication.

Last updated