-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48c21d8
commit 10fda2d
Showing
4 changed files
with
95 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/undertheriver/sgsg/auth/common/JwtProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.undertheriver.sgsg.auth.common; | ||
|
||
import java.util.Base64; | ||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.undertheriver.sgsg.common.exception.AccessTokenLoadException; | ||
import com.undertheriver.sgsg.common.exception.ExpiredTokenException; | ||
import com.undertheriver.sgsg.common.type.UserRole; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jws; | ||
import io.jsonwebtoken.JwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
|
||
@Component | ||
public class JwtProvider { | ||
|
||
private final String secretKey; | ||
private final long validityInMilliseconds; | ||
|
||
public JwtProvider( | ||
@Value("${security.jwt.token.secret-key:sample}") String secretKey, | ||
@Value("${security.jwt.token.expire-length:300000}") long validityInMilliseconds | ||
) { | ||
this.secretKey = Base64.getEncoder().encodeToString(secretKey.getBytes()); | ||
this.validityInMilliseconds = validityInMilliseconds; | ||
} | ||
|
||
public String createToken(Long userId, UserRole userRole) { | ||
Claims claims = Jwts.claims(); | ||
claims.put("userId", String.valueOf(userId)); | ||
claims.put("role", userRole.name()); | ||
|
||
Date now = new Date(); | ||
Date validity = new Date(now.getTime() + validityInMilliseconds); | ||
|
||
return Jwts.builder() | ||
.setClaims(claims) | ||
.setIssuedAt(now) | ||
.setExpiration(validity) | ||
.signWith(SignatureAlgorithm.HS256, secretKey) | ||
.compact(); | ||
} | ||
|
||
public Claims extractValidSubject(String token) { | ||
validateToken(token); | ||
|
||
return Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token).getBody(); | ||
} | ||
|
||
private void validateToken(String token) { | ||
try { | ||
Jws<Claims> claims = Jwts.parser().setSigningKey(secretKey).parseClaimsJws(token); | ||
validateExpiredTime(claims); | ||
} catch (JwtException | IllegalArgumentException e) { | ||
throw new AccessTokenLoadException(); | ||
} | ||
} | ||
|
||
private void validateExpiredTime(Jws<Claims> claims) { | ||
if (claims.getBody().getExpiration().before(new Date())) { | ||
throw new ExpiredTokenException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters