|
| 1 | +package com.apexgrid.transformertracker.auth; |
| 2 | + |
| 3 | +import io.jsonwebtoken.ExpiredJwtException; |
| 4 | +import io.jsonwebtoken.JwtException; |
| 5 | +import jakarta.servlet.FilterChain; |
| 6 | +import jakarta.servlet.ServletException; |
| 7 | +import jakarta.servlet.http.HttpServletRequest; |
| 8 | +import jakarta.servlet.http.HttpServletResponse; |
| 9 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 10 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 11 | +import org.springframework.security.core.userdetails.UserDetails; |
| 12 | +import org.springframework.security.core.userdetails.UserDetailsService; |
| 13 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 14 | +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import org.springframework.util.StringUtils; |
| 17 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +@Component |
| 23 | +public class JwtAuthenticationFilter extends OncePerRequestFilter { |
| 24 | + |
| 25 | + private final JwtService jwtService; |
| 26 | + private final UserDetailsService userDetailsService; |
| 27 | + |
| 28 | + public JwtAuthenticationFilter(JwtService jwtService, UserDetailsService userDetailsService) { |
| 29 | + this.jwtService = jwtService; |
| 30 | + this.userDetailsService = userDetailsService; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) |
| 35 | + throws ServletException, IOException { |
| 36 | + final String authHeader = request.getHeader("Authorization"); |
| 37 | + |
| 38 | + if (!StringUtils.hasText(authHeader) || !authHeader.startsWith("Bearer ")) { |
| 39 | + chain.doFilter(request, response); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + final String jwt = authHeader.substring(7); |
| 44 | + final String username; |
| 45 | + try { |
| 46 | + username = jwtService.extractUsername(jwt); |
| 47 | + } catch (ExpiredJwtException ex) { |
| 48 | + respondUnauthorized(response, "Token expired"); |
| 49 | + return; |
| 50 | + } catch (JwtException | IllegalArgumentException ex) { |
| 51 | + respondUnauthorized(response, "Invalid token"); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (StringUtils.hasText(username) && SecurityContextHolder.getContext().getAuthentication() == null) { |
| 56 | + UserDetails userDetails; |
| 57 | + try { |
| 58 | + userDetails = userDetailsService.loadUserByUsername(username); |
| 59 | + } catch (UsernameNotFoundException ex) { |
| 60 | + respondUnauthorized(response, "Invalid token"); |
| 61 | + return; |
| 62 | + } |
| 63 | + if (jwtService.isTokenValid(jwt, userDetails)) { |
| 64 | + UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( |
| 65 | + userDetails, null, userDetails.getAuthorities()); |
| 66 | + authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); |
| 67 | + SecurityContextHolder.getContext().setAuthentication(authToken); |
| 68 | + } else { |
| 69 | + respondUnauthorized(response, "Invalid token"); |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + chain.doFilter(request, response); |
| 75 | + } |
| 76 | + |
| 77 | + private void respondUnauthorized(HttpServletResponse response, String message) throws IOException { |
| 78 | + if (response.isCommitted()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); |
| 82 | + response.setContentType("application/json"); |
| 83 | + response.getOutputStream().write(("{\"error\":\"" + message + "\"}").getBytes(StandardCharsets.UTF_8)); |
| 84 | + } |
| 85 | +} |
0 commit comments