Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static 필드에 @Autowired 어노테이션 사용으로 인한 문제 해결 #167

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}