Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOTFIX] #225 - ROLE 접두사 중복 오류 해결 #226

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 104 additions & 100 deletions src/main/java/com/beat/global/auth/jwt/provider/JwtTokenProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.beat.global.auth.jwt.provider;

import com.beat.domain.user.domain.Role;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
Expand All @@ -10,12 +11,15 @@
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import jakarta.annotation.PostConstruct;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;

import javax.crypto.SecretKey;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -25,104 +29,104 @@
@Service
public class JwtTokenProvider {

@Value("${jwt.secret}")
private String jwtSecret;

@Value("${jwt.access-token-expire-time}")
private long accessTokenExpireTime;

@Value("${jwt.refresh-token-expire-time}")
private long refreshTokenExpireTime;

private static final String MEMBER_ID = "memberId";
private static final String ROLE_KEY = "role";

@PostConstruct
protected void init() {
jwtSecret = Base64.getEncoder().encodeToString(jwtSecret.getBytes(StandardCharsets.UTF_8));
}

public String issueAccessToken(final Authentication authentication) {
return issueToken(authentication, accessTokenExpireTime);
}

public String issueRefreshToken(final Authentication authentication) {
return issueToken(authentication, refreshTokenExpireTime);
}

private String issueToken(final Authentication authentication, final long expiredTime) {
final Date now = new Date();

final Claims claims = Jwts.claims()
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + expiredTime));

claims.put(MEMBER_ID, authentication.getPrincipal());
claims.put(ROLE_KEY, "ROLE_" + authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No authorities found for user")));

return Jwts.builder()
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.setClaims(claims)
.signWith(getSigningKey())
.compact();
}

private SecretKey getSigningKey() {
String encodedKey = Base64.getEncoder().encodeToString(jwtSecret.getBytes());
return Keys.hmacShaKeyFor(encodedKey.getBytes());
}

public JwtValidationType validateToken(String token) {
try {
Claims claims = getBody(token);
return JwtValidationType.VALID_JWT;
} catch (MalformedJwtException ex) {
log.error("Invalid JWT Token: {}", ex.getMessage());
return JwtValidationType.INVALID_JWT_TOKEN;
} catch (ExpiredJwtException ex) {
log.error("Expired JWT Token: {}", ex.getMessage());
return JwtValidationType.EXPIRED_JWT_TOKEN;
} catch (UnsupportedJwtException ex) {
log.error("Unsupported JWT Token: {}", ex.getMessage());
return JwtValidationType.UNSUPPORTED_JWT_TOKEN;
} catch (IllegalArgumentException ex) {
log.error("Empty JWT Token or Illegal Argument: {}", ex.getMessage());
return JwtValidationType.EMPTY_JWT;
} catch (SignatureException ex) {
log.error("Invalid JWT Signature: {}", ex.getMessage());
return JwtValidationType.INVALID_JWT_SIGNATURE;
}
}

private Claims getBody(final String token) {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
.build()
.parseClaimsJws(token)
.getBody();
}

public Long getMemberIdFromJwt(String token) {
Claims claims = getBody(token);
Long memberId = Long.valueOf(claims.get(MEMBER_ID).toString());

// 로그 추가: memberId 확인
log.info("Extracted memberId from JWT: {}", memberId);

return memberId;
}

public Role getRoleFromJwt(String token) {
Claims claims = getBody(token);
String roleName = claims.get(ROLE_KEY, String.class);

// "ROLE_" 접두사 제거
String enumValue = roleName.replace("ROLE_", "");
log.info("Extracted role from JWT: {}", enumValue);

return Role.valueOf(enumValue.toUpperCase());
}
@Value("${jwt.secret}")
private String jwtSecret;

@Value("${jwt.access-token-expire-time}")
private long accessTokenExpireTime;

@Value("${jwt.refresh-token-expire-time}")
private long refreshTokenExpireTime;

private static final String MEMBER_ID = "memberId";
private static final String ROLE_KEY = "role";

@PostConstruct
protected void init() {
jwtSecret = Base64.getEncoder().encodeToString(jwtSecret.getBytes(StandardCharsets.UTF_8));
}

public String issueAccessToken(final Authentication authentication) {
return issueToken(authentication, accessTokenExpireTime);
}

public String issueRefreshToken(final Authentication authentication) {
return issueToken(authentication, refreshTokenExpireTime);
}

private String issueToken(final Authentication authentication, final long expiredTime) {
final Date now = new Date();

final Claims claims = Jwts.claims()
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + expiredTime));

claims.put(MEMBER_ID, authentication.getPrincipal());
claims.put(ROLE_KEY, authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No authorities found for user")));

return Jwts.builder()
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.setClaims(claims)
.signWith(getSigningKey())
.compact();
}

private SecretKey getSigningKey() {
String encodedKey = Base64.getEncoder().encodeToString(jwtSecret.getBytes());
return Keys.hmacShaKeyFor(encodedKey.getBytes());
}

public JwtValidationType validateToken(String token) {
try {
Claims claims = getBody(token);
return JwtValidationType.VALID_JWT;
} catch (MalformedJwtException ex) {
log.error("Invalid JWT Token: {}", ex.getMessage());
return JwtValidationType.INVALID_JWT_TOKEN;
} catch (ExpiredJwtException ex) {
log.error("Expired JWT Token: {}", ex.getMessage());
return JwtValidationType.EXPIRED_JWT_TOKEN;
} catch (UnsupportedJwtException ex) {
log.error("Unsupported JWT Token: {}", ex.getMessage());
return JwtValidationType.UNSUPPORTED_JWT_TOKEN;
} catch (IllegalArgumentException ex) {
log.error("Empty JWT Token or Illegal Argument: {}", ex.getMessage());
return JwtValidationType.EMPTY_JWT;
} catch (SignatureException ex) {
log.error("Invalid JWT Signature: {}", ex.getMessage());
return JwtValidationType.INVALID_JWT_SIGNATURE;
}
}

private Claims getBody(final String token) {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
.build()
.parseClaimsJws(token)
.getBody();
}

public Long getMemberIdFromJwt(String token) {
Claims claims = getBody(token);
Long memberId = Long.valueOf(claims.get(MEMBER_ID).toString());

// 로그 추가: memberId 확인
log.info("Extracted memberId from JWT: {}", memberId);

return memberId;
}

public Role getRoleFromJwt(String token) {
Claims claims = getBody(token);
String roleName = claims.get(ROLE_KEY, String.class);

// "ROLE_" 접두사 제거
String enumValue = roleName.replace("ROLE_", "");
log.info("Extracted role from JWT: {}", enumValue);

return Role.valueOf(enumValue.toUpperCase());
}
}
Loading