content: package com.alexatiks.security;
  
  import io.jsonwebtoken.ExpiredJwtException;
  import io.jsonwebtoken.MalformedJwtException;
  import io.jsonwebtoken.SignatureException;
  import io.jsonwebtoken.UnsupportedJwtException;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.web.filter.OncePerRequestFilter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  
  public class JWTAuthenticationFilter extends OncePerRequestFilter {
      @Override
      protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException,
              IOException {
          try {
              Authentication authentication = TokenAuthenticationHelper.getAuthentication(request);
              SecurityContextHolder.getContext().setAuthentication(authentication);
              filterChain.doFilter(request, response);
          } catch (ExpiredJwtException | UnsupportedJwtException | MalformedJwtException |
                  SignatureException | IllegalArgumentException e) {
              response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Token expired");
          }
      }
  }