99import jakarta .servlet .ServletException ;
1010import jakarta .servlet .http .HttpServletRequest ;
1111import jakarta .servlet .http .HttpServletResponse ;
12- import java .io .IOException ;
13- import java .util .List ;
1412import lombok .RequiredArgsConstructor ;
1513import lombok .extern .slf4j .Slf4j ;
1614import org .springframework .security .core .Authentication ;
1917import org .springframework .stereotype .Component ;
2018import 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