Skip to content

Commit

Permalink
RAC-98 fix : 토큰 발급시 만료시간 포함
Browse files Browse the repository at this point in the history
  • Loading branch information
ywj9811 committed Oct 31, 2023
1 parent 877595e commit b84caba
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.postgraduate.domain.auth.application.dto.res;

import com.postgraduate.domain.user.domain.entity.constant.Role;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class JwtTokenResponse {
private String accessToken;
private int accessExpiration;
private String refreshToken;
private int refreshExpiration;
private Role role;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import com.postgraduate.global.jwt.JwtProvider;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class JwtUseCase {
private final JwtProvider jwtProvider;
private final SecurityUtils securityUtils;
@Value("${jwt.refreshExpiration}")
private int refreshExpiration;
@Value("${jwt.accessExpiration}")
private int accessExpiration;

public JwtTokenResponse signIn(User user) {
return generateToken(user);
Expand All @@ -28,6 +33,6 @@ public JwtTokenResponse regenerateToken(AuthDetails authDetails, HttpServletRequ
private JwtTokenResponse generateToken(User user) {
String accessToken = jwtProvider.generateAccessToken(user.getUserId(), user.getRole());
String refreshToken = jwtProvider.generateRefreshToken(user.getUserId(), user.getRole());
return new JwtTokenResponse(accessToken, refreshToken, user.getRole());
return new JwtTokenResponse(accessToken, accessExpiration, refreshToken, refreshExpiration, user.getRole());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
@RestControllerAdvice
@RequiredArgsConstructor
public class GlobalExceptionHandler {

}
8 changes: 6 additions & 2 deletions src/main/java/com/postgraduate/global/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ public class JwtProvider {
private final RedisRepository redisRepository;
@Value("${jwt.secret-key}")
private String secret;
@Value("${jwt.refreshExpiration}")
private int refreshExpiration;
@Value("${jwt.accessExpiration}")
private int accessExpiration;
private final String REFRESH = "refresh";
private final String AUTHORIZATION = "Authorization";

public String generateAccessToken(Long id, Role role) {
Instant accessDate = LocalDateTime.now().plusHours(6).atZone(ZoneId.systemDefault()).toInstant();
Instant accessDate = LocalDateTime.now().plusSeconds(accessExpiration).atZone(ZoneId.systemDefault()).toInstant();
return Jwts.builder()
.claim("role", role)
.setSubject(String.valueOf(id))
Expand All @@ -43,7 +47,7 @@ public String generateAccessToken(Long id, Role role) {
}

public String generateRefreshToken(Long id, Role role) {
Instant refreshDate = LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant();
Instant refreshDate = LocalDateTime.now().plusSeconds(refreshExpiration).atZone(ZoneId.systemDefault()).toInstant();
String refreshToken = Jwts.builder()
.claim("role", role)
.setSubject(String.valueOf(id))
Expand Down

0 comments on commit b84caba

Please sign in to comment.