From 346000708567fa6df0afa705f1915c90fcba71be Mon Sep 17 00:00:00 2001 From: inh2613 Date: Wed, 26 Jul 2023 11:31:54 +0900 Subject: [PATCH 1/5] fix: refresh-token-entity --- .../{UserToken.java => DeviceToken.java} | 0 .../gifthub/auth/entity/RefreshToken.java | 24 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) rename src/main/java/org/swmaestro/repl/gifthub/auth/entity/{UserToken.java => DeviceToken.java} (100%) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/UserToken.java b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java similarity index 100% rename from src/main/java/org/swmaestro/repl/gifthub/auth/entity/UserToken.java rename to src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/RefreshToken.java b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/RefreshToken.java index 9a988efd..2a21f4a9 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/RefreshToken.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/RefreshToken.java @@ -1,35 +1,37 @@ package org.swmaestro.repl.gifthub.auth.entity; -import org.swmaestro.repl.gifthub.util.BaseTimeEntity; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; + +import java.time.LocalDateTime; @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class RefreshToken extends BaseTimeEntity { +public class RefreshToken { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) - private String refreshToken; + private String token; @Column(length = 60, nullable = false) private String username; + @CreatedDate + @Column(nullable = false) + private LocalDateTime createdAt; + @Builder - public RefreshToken(Long id, String refreshToken, String username) { + public RefreshToken(Long id, String token, String username, LocalDateTime createdAt) { this.id = id; - this.refreshToken = refreshToken; + this.token = token; this.username = username; + this.createdAt = createdAt; } } From f1269c471e6c51ecfeabdf54a0a481d117f0c8c6 Mon Sep 17 00:00:00 2001 From: inh2613 Date: Wed, 26 Jul 2023 11:32:40 +0900 Subject: [PATCH 2/5] fix: rename to device-token --- .../repl/gifthub/auth/entity/DeviceToken.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java index d5bee339..291064e4 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/entity/DeviceToken.java @@ -1,25 +1,18 @@ package org.swmaestro.repl.gifthub.auth.entity; -import java.time.LocalDateTime; - -import org.springframework.data.annotation.CreatedDate; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; + +import java.time.LocalDateTime; @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class UserToken { +public class DeviceToken { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -36,7 +29,7 @@ public class UserToken { private LocalDateTime createdAt; @Builder - public UserToken(Long id, Member member, String token, LocalDateTime createdAt) { + public DeviceToken(Long id, Member member, String token, LocalDateTime createdAt) { this.id = id; this.member = member; this.token = token; From 60133e922e6c772dcd752afe35405d9bfbda348d Mon Sep 17 00:00:00 2001 From: inh2613 Date: Wed, 26 Jul 2023 11:35:17 +0900 Subject: [PATCH 3/5] fix: rename to device-token-repository --- .../gifthub/auth/repository/DeviceTokenRepository.java | 7 +++++++ .../repl/gifthub/auth/repository/UserTokenRepository.java | 7 ------- 2 files changed, 7 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/auth/repository/DeviceTokenRepository.java delete mode 100644 src/main/java/org/swmaestro/repl/gifthub/auth/repository/UserTokenRepository.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/repository/DeviceTokenRepository.java b/src/main/java/org/swmaestro/repl/gifthub/auth/repository/DeviceTokenRepository.java new file mode 100644 index 00000000..d1193798 --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/repository/DeviceTokenRepository.java @@ -0,0 +1,7 @@ +package org.swmaestro.repl.gifthub.auth.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.swmaestro.repl.gifthub.auth.entity.DeviceToken; + +public interface DeviceTokenRepository extends JpaRepository { +} diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/repository/UserTokenRepository.java b/src/main/java/org/swmaestro/repl/gifthub/auth/repository/UserTokenRepository.java deleted file mode 100644 index 078c4010..00000000 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/repository/UserTokenRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.swmaestro.repl.gifthub.auth.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.swmaestro.repl.gifthub.auth.entity.UserToken; - -public interface UserTokenRepository extends JpaRepository { -} From c901e01db6eefb78e56063e94048daf2abef3198 Mon Sep 17 00:00:00 2001 From: inh2613 Date: Wed, 26 Jul 2023 11:36:11 +0900 Subject: [PATCH 4/5] fix: store-refresh-token --- .../repl/gifthub/auth/service/RefreshTokenService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/auth/service/RefreshTokenService.java b/src/main/java/org/swmaestro/repl/gifthub/auth/service/RefreshTokenService.java index f61b4160..db634167 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/auth/service/RefreshTokenService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/auth/service/RefreshTokenService.java @@ -23,8 +23,9 @@ public class RefreshTokenService { @Transactional public void storeRefreshToken(TokenDto tokenDto, String username) { RefreshToken refreshToken = RefreshToken.builder() - .refreshToken(tokenDto.getRefreshToken()) + .token(tokenDto.getRefreshToken()) .username(username) + .createdAt(jwtProvider.getIssuedAt(tokenDto.getRefreshToken())) .build(); if (refreshTokenRepository.findByUsername(username).isPresent()) { From 350f0424a32926eec9035e4ce2fa6608077caaf5 Mon Sep 17 00:00:00 2001 From: inh2613 Date: Wed, 26 Jul 2023 11:37:13 +0900 Subject: [PATCH 5/5] feat: implement get-jwt-create-time --- .../repl/gifthub/util/JwtProvider.java | 75 ++++++++++++------- 1 file changed, 47 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java b/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java index 102fc393..9a66d029 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java +++ b/src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java @@ -1,9 +1,9 @@ package org.swmaestro.repl.gifthub.util; -import java.time.Instant; -import java.time.temporal.ChronoUnit; -import java.util.Date; - +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.Jws; +import io.jsonwebtoken.Jwts; +import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -15,10 +15,10 @@ import org.swmaestro.repl.gifthub.exception.ErrorCode; import org.swmaestro.repl.gifthub.security.JpaUserDetailsService; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.Jwts; -import jakarta.servlet.http.HttpServletRequest; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Date; @Component @PropertySource("classpath:application.yml") @@ -30,8 +30,8 @@ public class JwtProvider { private final RefreshTokenRepository refreshTokenRepository; public JwtProvider(@Value("${jwt.secret-key}") String secretKey, @Value("${jwt.expiration-time}") long expiration, - @Value("${issuer}") String issuer, JpaUserDetailsService userDetailsService, - RefreshTokenRepository refreshTokenRepository) { + @Value("${issuer}") String issuer, JpaUserDetailsService userDetailsService, + RefreshTokenRepository refreshTokenRepository) { this.secretKey = secretKey; this.expiration = expiration; this.issuer = issuer; @@ -47,12 +47,12 @@ public JwtProvider(@Value("${jwt.secret-key}") String secretKey, @Value("${jwt.e */ public String generateToken(String username) { return io.jsonwebtoken.Jwts.builder() - .setSubject(username) - .setIssuer(issuer) - .setIssuedAt(new java.util.Date(System.currentTimeMillis())) - .setExpiration(new java.util.Date(System.currentTimeMillis() + expiration)) - .signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes()) - .compact(); + .setSubject(username) + .setIssuer(issuer) + .setIssuedAt(new java.util.Date(System.currentTimeMillis())) + .setExpiration(new java.util.Date(System.currentTimeMillis() + expiration)) + .signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes()) + .compact(); } /** @@ -106,11 +106,11 @@ public Authentication getAuthentication(String token) { */ public String getUsername(String token) { return Jwts.parserBuilder() - .setSigningKey(secretKey.getBytes()) - .build() - .parseClaimsJws(token) - .getBody() - .getSubject(); + .setSigningKey(secretKey.getBytes()) + .build() + .parseClaimsJws(token) + .getBody() + .getSubject(); } /** @@ -121,12 +121,12 @@ public String getUsername(String token) { */ public String generateRefreshToken(String username) { return io.jsonwebtoken.Jwts.builder() - .setSubject(username) - .setIssuer(issuer) - .setIssuedAt(new java.util.Date(System.currentTimeMillis())) - .setExpiration(Date.from(Instant.now().plus(15, ChronoUnit.DAYS))) - .signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes()) - .compact(); + .setSubject(username) + .setIssuer(issuer) + .setIssuedAt(new java.util.Date(System.currentTimeMillis())) + .setExpiration(Date.from(Instant.now().plus(15, ChronoUnit.DAYS))) + .signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes()) + .compact(); } /** @@ -137,11 +137,30 @@ public String generateRefreshToken(String username) { */ public String reissueAccessToken(String refreshToken) { String username = getUsername(refreshToken); - String storedRefreshToken = refreshTokenRepository.findByUsername(username).get().getRefreshToken(); + String storedRefreshToken = refreshTokenRepository.findByUsername(username).get().getToken(); if (!refreshToken.equals(storedRefreshToken)) { throw new BusinessException("RefreshToken이 유효하지 않습니다.", ErrorCode.INVALID_AUTHENTICATION); } return generateToken(username); } + + /** + * 토큰 발급 시간을 가져오는 메소드 + * + * @param token + * @return 발급 시간 + */ + public LocalDateTime getIssuedAt(String token) { + return Jwts.parserBuilder() + .setSigningKey(secretKey.getBytes()) + .build() + .parseClaimsJws(token) + .getBody() + .getIssuedAt() + .toInstant() + .atZone(java.time.ZoneId.systemDefault()) + .toLocalDateTime(); + + } }