[ 설정파일 ]
jwt:
valid-time: 3600000 # 1시간
key: password
[ Provider ]
@Service
public class JwtProvider {
@Value("${jwt.valid-time}")
private int tokenValidTime;
@Value("${jwt.key}")
private String secretKey
public String createToken(String userId) {
Claims claims = Jwts.claims();
claims.put("user", userId);
claims.put("role", "USER");
String token = Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + tokenValidTime))
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();
return token;
}
public boolean isValidToken(String token) {
try {
Jws<Claims> claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token);
return !claims.getBody().getExpiration().before(new Date());
} catch (Exception e) {
return false;
}
}
}
'개발 > Java' 카테고리의 다른 글
[Spring] Spring Cloud Gateway + GlobalFilter (0) | 2022.06.25 |
---|---|
스프링 배치 에러 (The injection porint has the following annotations) (0) | 2020.12.03 |
스프링 배치 동작 에러 (Failed to execute CommandLineRunner) (0) | 2020.09.16 |