-
Notifications
You must be signed in to change notification settings - Fork 1
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
34e4d66
commit dc9b9d1
Showing
4 changed files
with
99 additions
and
1 deletion.
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
20 changes: 20 additions & 0 deletions
20
4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/jwt/JwtConfig.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,20 @@ | ||
package com.mutbook.week4_mission.app.security.jwt; | ||
|
||
import io.jsonwebtoken.security.Keys; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import javax.crypto.SecretKey; | ||
import java.util.Base64; | ||
|
||
@Configuration | ||
public class JwtConfig { | ||
@Value("${custom.jwt.secretKey}") | ||
private String secretKeyPlain; | ||
|
||
@Bean | ||
public SecretKey jwtSecretKey() { | ||
String keyBase64Encoded = Base64.getEncoder().encodeToString(secretKeyPlain.getBytes()); | ||
return Keys.hmacShaKeyFor(keyBase64Encoded.getBytes()); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
4Week_Mission/src/main/java/com/mutbook/week4_mission/app/security/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,56 @@ | ||
package com.mutbook.week4_mission.app.security.jwt; | ||
|
||
import com.mutbook.week4_mission.util.Util; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import javax.crypto.SecretKey; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtProvider { | ||
private final SecretKey jwtSecretKey; | ||
|
||
private SecretKey getSecretKey() { | ||
return jwtSecretKey; | ||
} | ||
|
||
public String generateAccessToken(Map<String, Object> claims, long seconds) { | ||
long now = new Date().getTime(); | ||
Date accessTokenExpiresIn = new Date(now + 1000L * seconds); | ||
|
||
return Jwts.builder() | ||
.claim("body", Util.json.toStr(claims)) | ||
.setExpiration(accessTokenExpiresIn) | ||
.signWith(getSecretKey(), SignatureAlgorithm.HS512) | ||
.compact(); | ||
} | ||
|
||
public boolean verify(String token) { | ||
try { | ||
Jwts.parserBuilder() | ||
.setSigningKey(getSecretKey()) | ||
.build() | ||
.parseClaimsJws(token); | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public Map<String, Object> getClaims(String token) { | ||
String body = Jwts.parserBuilder() | ||
.setSigningKey(getSecretKey()) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody() | ||
.get("body", String.class); | ||
|
||
return Util.json.toMap(body); | ||
} | ||
} |
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