-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
cakk-api/src/main/java/com/cakk/api/provider/jwt/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,100 @@ | ||
package com.cakk.api.provider.jwt; | ||
|
||
import static com.cakk.common.enums.ReturnCode.*; | ||
import static java.util.Objects.*; | ||
|
||
import java.security.Key; | ||
import java.security.PublicKey; | ||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Component; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import com.cakk.api.vo.JsonWebToken; | ||
import com.cakk.api.vo.OAuthUserDetails; | ||
import com.cakk.common.exception.CakkException; | ||
import com.cakk.domain.entity.user.User; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtProvider { | ||
|
||
private final Key key; | ||
|
||
@Value("${jwt.expiration.access-token}") | ||
private Long accessTokenExpiredSecond; | ||
@Value("${jwt.expiration.access-token}") | ||
private Long refreshTokenExpiredSecond; | ||
@Value("${jwt.grant-type}") | ||
private String grantType; | ||
@Value("${jwt.user-key}") | ||
private String userKey; | ||
|
||
public JsonWebToken generateToken(final User user) { | ||
final String accessToken = Jwts.builder() | ||
.claim(userKey, user) | ||
.setExpiration(new Date(System.currentTimeMillis() + accessTokenExpiredSecond)) | ||
.signWith(key, SignatureAlgorithm.HS512) | ||
.compact(); | ||
|
||
final String refreshToken = Jwts.builder() | ||
.setExpiration(new Date(System.currentTimeMillis() + refreshTokenExpiredSecond)) | ||
.signWith(key, SignatureAlgorithm.HS512) | ||
.compact(); | ||
|
||
return JsonWebToken.builder() | ||
.grantType(grantType) | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
|
||
public Authentication getAuthentication(String token) { | ||
final Claims claims = parseClaims(token); | ||
|
||
if (isNull(claims.get(userKey)) || !(claims.get(userKey) instanceof User)) { | ||
throw new CakkException(EMPTY_AUTH_JWT); | ||
} | ||
|
||
OAuthUserDetails userDetails = new OAuthUserDetails((User) claims.get(userKey)); | ||
|
||
return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities()); | ||
} | ||
|
||
public Claims parseClaims(String token) { | ||
try { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(key) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
} catch (ExpiredJwtException e) { | ||
throw new CakkException(EXPIRED_JWT_TOKEN); | ||
} catch (RuntimeException e) { | ||
throw new CakkException(WRONG_JWT_TOKEN); | ||
} | ||
} | ||
|
||
public Claims parseClaims(String token, PublicKey publicKey) { | ||
try { | ||
return Jwts.parserBuilder() | ||
.setSigningKey(publicKey) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
} catch (ExpiredJwtException e) { | ||
throw new CakkException(EXPIRED_JWT_TOKEN); | ||
} catch (RuntimeException e) { | ||
throw new CakkException(WRONG_JWT_TOKEN); | ||
} | ||
} | ||
} |
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,11 @@ | ||
package com.cakk.api.vo; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record JsonWebToken( | ||
String accessToken, | ||
String refreshToken, | ||
String grantType | ||
) { | ||
} |
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