Skip to content

Commit

Permalink
fix: JWT 유틸 클래스의 정적필드에 사용된 Value 어노테이션 삭제 (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunsb committed Aug 9, 2023
1 parent e8afed3 commit bb23146
Showing 1 changed file with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,47 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.core.model.user.User;
import java.util.Date;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

@RequiredArgsConstructor
@Component
@Slf4j
public class JwtTokenProvider {

public static final Long EXP = 1000L * 60 * 60 * 48;
public static final String TOKEN_PREFIX = "Bearer ";
public static final String HEADER = "Authorization";

@Value("${secret-key}")
private static String secretKey;

private final Environment environment;

@PostConstruct
private void init() {
secretKey = environment.getProperty("secret-key");
}

public static String create(User user) {
String jwt =
JWT.create()
.withSubject(user.getEmail())
.withExpiresAt(new Date(System.currentTimeMillis() + EXP))
.withClaim("username", user.getUsername())
.withClaim("id", user.getId())
.withClaim("role", user.getRole())
.sign(Algorithm.HMAC512(secretKey));
log.info("JWT created: " + jwt);
return TOKEN_PREFIX + jwt;
}

public static DecodedJWT verify(String jwt) {
log.info("JWT verify: " + jwt);
return JWT.require(Algorithm.HMAC512(secretKey)).build().verify(jwt);
}
public static final Long EXP = 1000L * 60 * 60 * 48;
public static final String TOKEN_PREFIX = "Bearer ";
public static final String HEADER = "Authorization";

private static String secretKey;

private final Environment environment;

public static String create(User user) {
String jwt =
JWT.create()
.withSubject(user.getEmail())
.withExpiresAt(new Date(System.currentTimeMillis() + EXP))
.withClaim("username", user.getUsername())
.withClaim("id", user.getId())
.withClaim("role", user.getRole())
.sign(Algorithm.HMAC512(secretKey));
log.info("JWT created: " + jwt);
return TOKEN_PREFIX + jwt;
}

public static DecodedJWT verify(String jwt) {
log.info("JWT verify: " + jwt);
return JWT.require(Algorithm.HMAC512(secretKey)).build().verify(jwt);
}

@PostConstruct
private void init() {
secretKey = environment.getProperty("secret-key");
}
}

0 comments on commit bb23146

Please sign in to comment.