Skip to content

Commit

Permalink
refactor(JWT): ROLE_ prefix 설정 로직 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee committed Aug 7, 2024
1 parent 293ceda commit 128b0ae
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
13 changes: 2 additions & 11 deletions src/main/java/page/clab/api/global/auth/jwt/JwtTokenProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -51,7 +50,7 @@ public TokenInfo generateToken(String id, Role role) {
Date accessTokenExpiry = new Date(expiry.getTime() + (accessTokenDuration));
String accessToken = Jwts.builder()
.subject(id)
.claim("role", role)
.claim("role", role.getKey())
.issuedAt(expiry)
.expiration(accessTokenExpiry)
.signWith(key)
Expand All @@ -60,7 +59,7 @@ public TokenInfo generateToken(String id, Role role) {
Date refreshTokenExpiry = new Date(expiry.getTime() + (refreshTokenDuration));
String refreshToken = Jwts.builder()
.subject(id)
.claim("role", role)
.claim("role", role.getKey())
.issuedAt(expiry)
.expiration(refreshTokenExpiry)
.signWith(key)
Expand Down Expand Up @@ -93,7 +92,6 @@ public Authentication getAuthentication(String token) {

Collection<? extends GrantedAuthority> authorities =
Arrays.stream(claims.get("role").toString().split(","))
.map(this::formatRoleString)
.map(SimpleGrantedAuthority::new)
.toList();

Expand Down Expand Up @@ -151,11 +149,4 @@ public Claims parseClaims(String token) {
return e.getClaims();
}
}

private String formatRoleString(String role) {
if (!role.startsWith("ROLE_")) {
return "ROLE_" + role;
}
return role;
}
}
4 changes: 2 additions & 2 deletions src/main/java/page/clab/api/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
Expand Down Expand Up @@ -40,7 +40,7 @@

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@EnableMethodSecurity(securedEnabled = true)
@RequiredArgsConstructor
@Slf4j
public class SecurityConfig {
Expand Down

0 comments on commit 128b0ae

Please sign in to comment.