8
8
import com .newfit .reservation .domains .authority .repository .AuthorityRepository ;
9
9
import com .newfit .reservation .domains .user .domain .User ;
10
10
import com .newfit .reservation .domains .user .repository .UserRepository ;
11
- import io .jsonwebtoken .Claims ;
12
- import io .jsonwebtoken .Header ;
13
- import io .jsonwebtoken .Jwts ;
14
- import io .jsonwebtoken .SignatureAlgorithm ;
11
+ import io .jsonwebtoken .*;
15
12
import jakarta .servlet .http .HttpServletRequest ;
16
13
import jakarta .servlet .http .HttpServletResponse ;
17
14
import lombok .RequiredArgsConstructor ;
20
17
import org .springframework .security .core .Authentication ;
21
18
import org .springframework .security .core .authority .SimpleGrantedAuthority ;
22
19
import org .springframework .stereotype .Service ;
20
+ import java .io .IOException ;
23
21
import java .time .Duration ;
24
22
import java .util .*;
25
23
@@ -32,6 +30,7 @@ public class TokenProvider { // JWT의 생성 및 검증 로직 담당 클래
32
30
private final static Duration ACCESS_TOKEN_DURATION = Duration .ofMinutes (30 );
33
31
private final static Duration ACCESS_TEMPORARY_TOKEN_DURATION = Duration .ofMinutes (10 );
34
32
private final static Duration REFRESH_TOKEN_DURATION = Duration .ofDays (7 );
33
+ private final static Duration ADMIN_TOKEN_DURATION = Duration .ofMinutes (10 );
35
34
36
35
private final JwtProperties jwtProperties ;
37
36
private final AuthorityRepository authorityRepository ;
@@ -88,6 +87,19 @@ public String generateRefreshToken(User user) {
88
87
.getToken ();
89
88
}
90
89
90
+ public String generateAdminToken () {
91
+ Date now = new Date ();
92
+ Date expiryAt = new Date (now .getTime () + ADMIN_TOKEN_DURATION .toMillis ());
93
+ return Jwts .builder ()
94
+ .setHeaderParam (Header .TYPE , Header .JWT_TYPE )
95
+ .setIssuer (jwtProperties .getIssuer ())
96
+ .setIssuedAt (now )
97
+ .setExpiration (expiryAt )
98
+ .setSubject ("admin" )
99
+ .signWith (SignatureAlgorithm .HS256 , jwtProperties .getSecretKey ())
100
+ .compact ();
101
+ }
102
+
91
103
public void validAccessToken (String token , HttpServletRequest request , HttpServletResponse response ) {
92
104
validToken (token );
93
105
try {
@@ -97,6 +109,18 @@ public void validAccessToken(String token, HttpServletRequest request, HttpServl
97
109
}
98
110
}
99
111
112
+ public void validAdminToken (String token , HttpServletResponse response ) throws IOException {
113
+ try {
114
+ validToken (token );
115
+ Claims claims = getClaims (token );
116
+ if (!claims .getSubject ().equals ("admin" )) {
117
+ throw new CustomException (ADMIN_UNAUTHORIZED_REQUEST );
118
+ }
119
+ } catch (ExpiredJwtException exception ) {
120
+ response .sendRedirect ("/login" );
121
+ }
122
+ }
123
+
100
124
private void checkExceptionAndProceed (HttpServletResponse response , CustomException exception , Long userId ){
101
125
if (!exception .getErrorCodeType ().equals (AUTHORITY_ID_LIST_OUTDATED )) {
102
126
throw exception ;
@@ -173,6 +197,12 @@ public Authentication getAnonymousAuthentication(String token) {
173
197
}
174
198
}
175
199
200
+ public Authentication getAdminAuthentication () {
201
+ Set <SimpleGrantedAuthority > authorities = Collections .singleton (new SimpleGrantedAuthority (RoleType .ADMIN .getDescription ()));
202
+
203
+ return new UsernamePasswordAuthenticationToken (new org .springframework .security .core .userdetails .User ("admin" , "" , authorities ), null , authorities );
204
+ }
205
+
176
206
private List <Integer > getAuthorityIdList (String token ) {
177
207
Claims claims = getClaims (token );
178
208
return claims .get ("authorityIdList" , List .class );
0 commit comments