Skip to content

Commit

Permalink
토큰 검증 실패 예외 추가, 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
jun-ha committed Aug 17, 2024
1 parent c255ea3 commit 0983199
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package newCar.event_page.exception;

public class UnverifiedTokenException extends RuntimeException {
public UnverifiedTokenException() {}
public UnverifiedTokenException(String message) {}
}
46 changes: 26 additions & 20 deletions src/main/java/newCar/event_page/jwt/JwtTokenProviderImpl.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package newCar.event_page.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.RequiredArgsConstructor;
import newCar.event_page.config.JwtConfig;
import newCar.event_page.exception.UnverifiedTokenException;
import newCar.event_page.model.enums.Team;
import newCar.event_page.model.entity.User;
import newCar.event_page.repository.jpa.UserRepository;
import org.springframework.stereotype.Component;

import java.security.SignatureException;
import java.util.*;

@Component
Expand Down Expand Up @@ -70,7 +68,11 @@ public String generateToken(Map<String, Object> claims){
public String generateTokenWithTeam(Team team, String authorizationHeader){

Long userId;
userId = getClaims(authorizationHeader).get("userId",Long.class);
try {
userId = getClaims(authorizationHeader).get("userId",Long.class);
} catch (Exception e) {
throw new UnverifiedTokenException("잘못된 토큰입니다.");
}

Map<String, Object> claims = new HashMap<>();

Expand All @@ -82,37 +84,39 @@ public String generateTokenWithTeam(Team team, String authorizationHeader){

@Override
public Long getUserId(String token){
Long userId;
userId = getClaims(token).get("userId",Long.class);
return userId;
try{
return getClaims(token).get("userId", Long.class);
} catch (Exception e){
throw new UnverifiedTokenException("토큰 검증을 먼저 진행해야합니다.");
}
} //토큰에서 유저 Id를 추출

@Override
public Team getTeam(String token){
return claimsToTeam(getClaims(token).get("team",String.class));
try{
return claimsToTeam(getClaims(token).get("team", String.class));
} catch (Exception e){
throw new UnverifiedTokenException("토큰 검증을 먼저 진행해야합니다.");
}
} //토큰에서 유저 Team을 추출

@Override
public boolean validateToken(String token){
try{
getClaims(token);
return true;
} catch (ExpiredJwtException | SignatureException e){
} catch (Exception e){
return false;
} //토큰이 만료되었거나 변조되었다면

} //JWT 토큰 유효성 검증

@Override
public boolean validateAdminToken(String token){
String role = "";
try{
role = getClaims(token).get("role",String.class);
} catch (ExpiredJwtException | SignatureException e){
return false;
} //토큰이 만료되었거나 변조되었다면

return role.equals("admin");//role 이 admin이라면 true를, 아니라면 false를 반환한다
return getClaims(token).get("role", String.class).equals("admin");
} catch (Exception e){
return false;
}
}
//JWT 토큰이 admin의 역할을 담고 있는지 검증

Expand All @@ -127,13 +131,15 @@ private Team claimsToTeam(String team){
}
}

private Claims getClaims(String token){
private Claims getClaims(String token)
throws ExpiredJwtException, SignatureException, IllegalArgumentException, MalformedJwtException, UnsupportedJwtException {
return Jwts.parserBuilder()
.setSigningKey(secretKey())
.build()
.parseClaimsJws(token)
.getBody();
}

private byte[] secretKey(){
return Decoders.BASE64.decode(jwtConfig.getSecret());
}//원래는 secret key 값을 바로 바꿔 줄 수 있었으나 그 메소드는 deprecated 되어서
Expand Down

0 comments on commit 0983199

Please sign in to comment.