-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JwtProvider - AccessToken 발급 method (#7)
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/com/api/TaveShot/global/jwt/JwtProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.api.TaveShot.global.jwt; | ||
|
||
import static com.api.TaveShot.global.constant.OauthConstant.ACCESS_TOKEN_VALID_TIME; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.security.Keys; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Date; | ||
import javax.crypto.SecretKey; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
public class JwtProvider { | ||
|
||
@Value("${jwt.secret.key}") | ||
private String SECRET_KEY; | ||
|
||
public String generateAccessToken(String id) { | ||
Claims claims = createClaims(id); | ||
Date now = new Date(); | ||
long expiredDate = calculateExpirationDate(now); | ||
SecretKey secretKey = generateKey(); | ||
|
||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(expiredDate)) | ||
.signWith(secretKey, SignatureAlgorithm.HS256) | ||
.compact(); | ||
} | ||
|
||
private Claims createClaims(String id) { | ||
return Jwts.claims().setSubject(id); | ||
} | ||
|
||
private long calculateExpirationDate(Date now) { | ||
return now.getTime() + ACCESS_TOKEN_VALID_TIME; | ||
} | ||
|
||
private SecretKey generateKey() { | ||
return Keys.hmacShaKeyFor(SECRET_KEY.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
} |