Skip to content
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");
}
}