2121import org .springframework .stereotype .Component ;
2222
2323import java .security .Key ;
24- import java .util .Arrays ;
25- import java .util .Collection ;
26- import java .util .Date ;
24+ import java .util .*;
2725import java .util .stream .Collectors ;
2826
2927
@@ -61,22 +59,24 @@ public long getRefreshTokenExpirationTime() {
6159 * 인증 정보(Authentication)를 기반으로 Access Token과 Refresh Token을 생성
6260 */
6361 public MemberAuthResponseDto generateToken (Authentication authentication ) {
64- // 권한 정보와 사용자 ID 가져오기
65- String authorities = authentication .getAuthorities ().stream ()
66- .map (GrantedAuthority ::getAuthority )
67- .collect (Collectors .joining ("," ));
6862
6963 CustomUserDetails userDetails = (CustomUserDetails ) authentication .getPrincipal ();
70- String memberId = String . valueOf ( userDetails .getMemberId () );
64+ Member member = userDetails .getMember ( );
7165
7266 long now = (new Date ()).getTime ();
7367 Date accessTokenExpiresIn = new Date (now + accessTokenExpirationTime );
7468 Date refreshTokenExpiresIn = new Date (now + refreshTokenExpirationTime );
7569
7670 // Access Token 생성 (Subject: 회원 PK, Claim: 권한)
7771 String accessToken = Jwts .builder ()
78- .setSubject (memberId )
79- .claim ("auth" , authorities )
72+ .setSubject (String .valueOf (member .getId ()))
73+ .claim ("companyId" , member .getCompanyId ())
74+ .claim ("department" , member .getDepartment ().name ())
75+ .claim ("permissions" , Map .of (
76+ "inventory" , member .getPermissions ().getInventoryRole ().name (),
77+ "logistics" , member .getPermissions ().getLogisticsRole ().name (),
78+ "management" , member .getPermissions ().getManagementRole ().name ()
79+ ))
8080 .setExpiration (accessTokenExpiresIn )
8181 .signWith (key , SignatureAlgorithm .HS256 )
8282 .compact ();
@@ -100,17 +100,6 @@ public MemberAuthResponseDto generateToken(Authentication authentication) {
100100 public Authentication getAuthentication (String accessToken ) {
101101 // 클레임 추출
102102 Claims claims = parseClaims (accessToken );
103-
104- if (claims .get ("auth" ) == null ) {
105- throw new RuntimeException ("권한 정보가 없는 토큰입니다." );
106- }
107-
108- // 권한 정보(auth)를 SimpleGrantedAuthority 객체 리스트로 변환
109- Collection <? extends GrantedAuthority > authorities =
110- Arrays .stream (claims .get ("auth" ).toString ().split ("," ))
111- .map (SimpleGrantedAuthority ::new )
112- .collect (Collectors .toList ());
113-
114103 Long memberId = Long .valueOf (claims .getSubject ());
115104
116105 Member member = memberRepository .findById (memberId )
@@ -121,7 +110,7 @@ public Authentication getAuthentication(String accessToken) {
121110 return new UsernamePasswordAuthenticationToken (
122111 principal ,
123112 null ,
124- principal . getAuthorities ()
113+ List . of ()
125114 );
126115
127116 }
0 commit comments