Skip to content

Commit

Permalink
static 필드에 @Autowired 어노테이션 사용으로 인한 문제 해결 (#167)
Browse files Browse the repository at this point in the history
* fix: JWT 유틸 클래스의 정적필드에 사용된 Value 어노테이션 삭제 (#166)

* fix: 암호화 구현 클래스의 정적필드에 사용된 Value 어노테이션 삭제 (#166)
  • Loading branch information
hyunsb authored Aug 9, 2023
1 parent c4241ae commit c3880a0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.example.core.errors.exception.DecryptException;
import com.example.core.errors.exception.EncryptException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

Expand All @@ -18,8 +17,8 @@ public class AESEncryption implements Encryption {

private static final String AES_ALGORITHM = "AES";

@Value("${aes-key}")
private static String AES_KEY;

private final Environment environment;

public String encrypt(String data) throws EncryptException {
Expand Down
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 c3880a0

Please sign in to comment.