22
33import com .neighbors .tohero .common .enums .Role ;
44import com .neighbors .tohero .common .jwt .JwtProvider ;
5+ import jakarta .annotation .PostConstruct ;
56import jakarta .servlet .http .HttpServletRequest ;
67import lombok .RequiredArgsConstructor ;
78import lombok .extern .slf4j .Slf4j ;
1011import org .springframework .stereotype .Component ;
1112import org .springframework .util .StringUtils ;
1213
13- import java .util .Optional ;
14+ import java .util .* ;
1415
1516@ Slf4j
1617@ Component
1718@ RequiredArgsConstructor
1819public class AuthenticationUtil {
1920
2021 private final JwtProvider jwtProvider ;
22+ private Map <String , List <String >> onlyUserRequest ;
23+
24+ @ PostConstruct
25+ private void initOnlyUserRequest () {
26+ onlyUserRequest = new HashMap <>();
27+
28+ // 초기화
29+ addToOnlyUserRequest ("PUT" , "/user/name" );
30+ addToOnlyUserRequest ("POST" , "/user/signout" );
31+ addToOnlyUserRequest ("POST" , "/user/logout" );
32+ addToOnlyUserRequest ("GET" , "/letter" );
33+ addToOnlyUserRequest ("PUT" , "/letter" );
34+ addToOnlyUserRequest ("GET" , "/auth/refreshToken" );
35+ }
36+
37+ private void addToOnlyUserRequest (String method , String url ) {
38+ onlyUserRequest .computeIfAbsent (method , k -> new ArrayList <>()).add (url );
39+ }
2140
2241 public void setAuthenticationFromRequest (HttpServletRequest request ) {
2342
@@ -42,9 +61,11 @@ private Optional<UserAuthentication> makeAuthentication(HttpServletRequest reque
4261
4362 if (isTokenValid (token )) {
4463 if (isRequestAvailableToGuest (token )) {
45- log .info ("[AuthenticationUtil.makeAuthentication : Guest 권한 부여]" );
46- String nickname = jwtProvider .getGuestJwtUserDetails (token ).getNickname ();
47- authentication = UserAuthentication .makeGuestAuthentication (nickname );
64+ if (checkGuestAccessRequest (request )){
65+ log .info ("[AuthenticationUtil.makeAuthentication : Guest 권한 부여]" );
66+ String nickname = jwtProvider .getGuestJwtUserDetails (token ).getNickname ();
67+ authentication = UserAuthentication .makeGuestAuthentication (nickname );
68+ }
4869 }
4970 else {
5071 log .info ("[AuthenticationUtil.makeAuthentication : User 권한 부여]" );
@@ -59,6 +80,18 @@ private Optional<UserAuthentication> makeAuthentication(HttpServletRequest reque
5980 return Optional .ofNullable (authentication );
6081 }
6182
83+ private boolean checkGuestAccessRequest (HttpServletRequest request ) {
84+ List <String > urls = onlyUserRequest .get (request .getMethod ());
85+ if (urls != null ) {
86+ for (String url : urls ) {
87+ if (request .getRequestURI ().contains (url )) {
88+ return false ;
89+ }
90+ }
91+ }
92+ return true ;
93+ }
94+
6295 private String getJwtFromRequest (HttpServletRequest request ) {
6396 String bearerToken = request .getHeader ("Authorization" );
6497
0 commit comments