<commit_before>package org.carlspring.strongbox.security.authentication;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

public class JWTAuthenticationFilter extends OncePerRequestFilter
{

    private AuthenticationManager authenticationManager;

    public JWTAuthenticationFilter(AuthenticationManager authenticationManager)
    {
        super();
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
        throws ServletException,
        IOException
    {
        String tokenHeader = request.getHeader("Authorization");

        Pattern pattern = Pattern.compile("Bearer (.*)");
        Matcher matcher;
        if (tokenHeader == null || !(matcher = pattern.matcher(tokenHeader)).matches())
        {
            filterChain.doFilter(request, response);
            return;
        }

        String token = matcher.group(1);

        try
        {
            JWTAuthentication tokenAuthentication = new JWTAuthentication(token);
            Authentication authentication = authenticationManager.authenticate(tokenAuthentication);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
        catch (AuthenticationException e)
        {
            SecurityContextHolder.clearContext();
        }
        filterChain.doFilter(request, response);
    }

}<commit_msg>SB-515: CR<commit_after>package org.carlspring.strongbox.security.authentication;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

public class JWTAuthenticationFilter extends OncePerRequestFilter
{

    private AuthenticationManager authenticationManager;

    public JWTAuthenticationFilter(AuthenticationManager authenticationManager)
    {
        super();
        this.authenticationManager = authenticationManager;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response,
                                    FilterChain filterChain)
        throws ServletException,
        IOException
    {
        String tokenHeader = request.getHeader("Authorization");

        Pattern pattern = Pattern.compile("Bearer (.*)");
        if (tokenHeader == null){
            filterChain.doFilter(request, response);
            return;            
        }
        Matcher matcher = pattern.matcher(tokenHeader);
        if (!matcher.matches())
        {
            filterChain.doFilter(request, response);
            return;
        }

        String token = matcher.group(1);
        try
        {
            JWTAuthentication tokenAuthentication = new JWTAuthentication(token);
            Authentication authentication = authenticationManager.authenticate(tokenAuthentication);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
        catch (AuthenticationException e)
        {
            SecurityContextHolder.clearContext();
        }
        filterChain.doFilter(request, response);
    }

}
{'fraction_non_alphanumeric': 0.0766698024459078, 'fraction_numerical': 0.0011759172154280338, 'mean_word_length': 3.0620821394460362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3757101', 'n_tokens_mistral': 955, 'n_tokens_neox': 937, 'n_words': 177}