From 52b90fa98d7510bde4d5a8acbc8ea31ea9caf709 Mon Sep 17 00:00:00 2001 From: You Jung <80906691+JangYouJung@users.noreply.github.com> Date: Sun, 26 May 2024 18:44:42 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20@CurrentUser=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meltingpot/server/config/TokenProvider.java | 5 +---- .../meltingpot/server/util/CurrentUser.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 src/main/java/meltingpot/server/util/CurrentUser.java diff --git a/src/main/java/meltingpot/server/config/TokenProvider.java b/src/main/java/meltingpot/server/config/TokenProvider.java index 62c597e..a418717 100644 --- a/src/main/java/meltingpot/server/config/TokenProvider.java +++ b/src/main/java/meltingpot/server/config/TokenProvider.java @@ -172,10 +172,7 @@ public Authentication getAuthentication(String accessToken) { // UserDetails 객체를 만들어서 Authentication 리턴 UserDetails principal = new User(claims.getSubject(), "", authorities); - // 유저 객체를 등록 - Optional account = accountRepository.findByUsername(principal.getUsername()); - - return new UsernamePasswordAuthenticationToken(account, accessToken, authorities); + return new UsernamePasswordAuthenticationToken(principal, accessToken, authorities); } diff --git a/src/main/java/meltingpot/server/util/CurrentUser.java b/src/main/java/meltingpot/server/util/CurrentUser.java new file mode 100644 index 0000000..f066554 --- /dev/null +++ b/src/main/java/meltingpot/server/util/CurrentUser.java @@ -0,0 +1,17 @@ +package meltingpot.server.util; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import org.springframework.security.core.annotation.AuthenticationPrincipal; + +@Target({ElementType.PARAMETER, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@AuthenticationPrincipal(expression = "@authService.getUserInfo()") +//인증된 사용자의 Principal 정보를 참조할 수 있다. +public @interface CurrentUser { + +}