content: package com.alkl1m.taskmanager.util;
  
  import com.alkl1m.taskmanager.service.auth.UserDetailsServiceImpl;
  import jakarta.servlet.FilterChain;
  import jakarta.servlet.ServletException;
  import jakarta.servlet.http.HttpServletRequest;
  import jakarta.servlet.http.HttpServletResponse;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import java.io.IOException;
  
  public class AuthTokenFilter extends OncePerRequestFilter {
      @Autowired
      private JwtUtils jwtUtils;
      @Autowired
      private UserDetailsServiceImpl userDetailsService;
      private static final Logger logger = LoggerFactory.getLogger(AuthTokenFilter.class);
      @Override
      protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
              throws ServletException, IOException {
          try {
              String jwt = parseJwt(request);
              if (jwt != null && jwtUtils.validateJwtToken(jwt)) {
                  String email = jwtUtils.getEmailFromJwtToken(jwt);
                  UserDetails userDetails = userDetailsService.loadUserByUsername(email);
                  UsernamePasswordAuthenticationToken authentication =
                          new UsernamePasswordAuthenticationToken(userDetails,
                                  null,
                                  userDetails.getAuthorities());
                  authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                  SecurityContextHolder.getContext().setAuthentication(authentication);
              }
          } catch (Exception e) {
              logger.error("Cannot set user authentication: " + e);
          }
          filterChain.doFilter(request, response);
      }
  
      private String parseJwt(HttpServletRequest request) {
          return jwtUtils.getJwtFromCookies(request);
      }
  }
  