Skip to content

Commit

Permalink
feat: JwtProvider - AccessToken 발급 method (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
toychip committed Nov 29, 2023
1 parent 6556cd5 commit 47eea6e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ dependencies {

// oauth
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

// jwt
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}

tasks.named('bootBuildImage') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ private OauthConstant() {
public static final String LOGIN_PATTERN = "login";
public static final String NAME_PATTERN = "name";
public static final String EMAIL_PATTERN = "email";
public static final long ACCESS_TOKEN_VALID_TIME = 15 * 60 * 1000L;

}
45 changes: 45 additions & 0 deletions src/main/java/com/api/TaveShot/global/jwt/JwtProvider.java
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));
}

}

0 comments on commit 47eea6e

Please sign in to comment.