Skip to content

Commit 7d5be72

Browse files
committed
refactor(JwtAuthFilter): 리팩토링, name() 사용 제거
1 parent d7ca840 commit 7d5be72

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/main/java/com/somemore/auth/jwt/filter/JwtAuthFilter.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import jakarta.servlet.ServletException;
1010
import jakarta.servlet.http.HttpServletRequest;
1111
import jakarta.servlet.http.HttpServletResponse;
12-
import java.io.IOException;
13-
import java.util.List;
1412
import lombok.RequiredArgsConstructor;
1513
import lombok.extern.slf4j.Slf4j;
1614
import org.springframework.security.core.Authentication;
@@ -19,6 +17,9 @@
1917
import org.springframework.stereotype.Component;
2018
import org.springframework.web.filter.OncePerRequestFilter;
2119

20+
import java.io.IOException;
21+
import java.util.List;
22+
2223
@RequiredArgsConstructor
2324
@Slf4j
2425
@Component
@@ -34,7 +35,7 @@ protected boolean shouldNotFilter(HttpServletRequest request) {
3435

3536
@Override
3637
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
37-
FilterChain filterChain) throws ServletException, IOException {
38+
FilterChain filterChain) throws ServletException, IOException {
3839
EncodedToken accessToken = getAccessToken(request);
3940
jwtUseCase.processAccessToken(accessToken, response);
4041

@@ -47,24 +48,24 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
4748

4849
private EncodedToken getAccessToken(HttpServletRequest request) {
4950
String accessToken = request.getHeader("Authorization");
50-
if (!accessToken.startsWith("Bearer ")) {
51-
throw new JwtException(JwtErrorType.MISSING_TOKEN);
52-
}
5351

54-
accessToken = accessToken.substring(7);
52+
String tokenPrefix = "Bearer ";
53+
if (accessToken.startsWith(tokenPrefix)) {
54+
return new EncodedToken(accessToken.substring(tokenPrefix.length()));
55+
}
5556

56-
return new EncodedToken(accessToken);
57+
throw new JwtException(JwtErrorType.MISSING_TOKEN);
5758
}
5859

5960
private JwtAuthenticationToken createAuthenticationToken(Claims claims,
60-
EncodedToken accessToken) {
61+
EncodedToken accessToken) {
6162
String userId = claims.get("id", String.class);
62-
UserRole role = UserRole.valueOf(claims.get("role", String.class));
63+
String role = claims.get("role", String.class);
6364

6465
return new JwtAuthenticationToken(
6566
userId,
6667
accessToken,
67-
List.of(new SimpleGrantedAuthority(role.name()))
68+
List.of(new SimpleGrantedAuthority(role))
6869
);
6970
}
7071
}

0 commit comments

Comments
 (0)