Skip to content

Commit

Permalink
feat: 요청받은 accessToken의 토큰 유효성 판단을 위한 validateJwtToken() 메서드 작성 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mybloom committed Jun 11, 2022
1 parent b00a951 commit 428bda1
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions BE/src/main/java/org/team4/airbnb/auth/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@


import io.jsonwebtoken.Claims;
import io.jsonwebtoken.IncorrectClaimException;
import io.jsonwebtoken.InvalidClaimException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MissingClaimException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import java.util.Date;
import javax.crypto.SecretKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.team4.airbnb.auth.domain.JwtPayload;
import org.team4.airbnb.exception.TokenInValidateException;

@Slf4j
@PropertySource(value = "classpath:jwt.properties", ignoreResourceNotFound = true)
Expand Down Expand Up @@ -39,4 +44,44 @@ public String createToken(JwtPayload payload) {
return token;
}


public Claims parseJwtToken(String accessToken) {
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));

return Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(accessToken)
.getBody();
}

public Claims validateJwtToken(String accessToken) {
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));
Claims body = null;

try {
body = Jwts.parserBuilder()
.requireSubject("team4Airbnb")
.require("http://org.team4.airbnb", "true")
.requireExpiration(new Date(System.currentTimeMillis()))
.setSigningKey(key)
.build()
.parseClaimsJws(accessToken)
.getBody();
} catch (MissingClaimException missingClaimException) {
log.error("**validateJwtToken: MissingClaimException 발생",
new TokenInValidateException());
throw new TokenInValidateException();
} catch (IncorrectClaimException incorrectClaimException) {
log.error("**validateJwtToken: IncorrectClaimException 발생",
new TokenInValidateException());
throw new TokenInValidateException();
} catch (InvalidClaimException invalidClaimException) {
log.error("**validateJwtToken: MissingClaimException 발생",
new TokenInValidateException());
throw new TokenInValidateException();
}

return body;
}
}

0 comments on commit 428bda1

Please sign in to comment.