@Test void shouldCallAuthenticationManagerAuthenticate() { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.GET.name()); httpServletResponse = new MockHttpServletResponse(); queryFilter.setAuthenticationManager(authenticationManager); when(authenticationService.getJwtTokenFromRequest(any())).thenReturn( Optional.of(VALID_TOKEN) ); TokenAuthentication valid = new TokenAuthentication(VALID_TOKEN); valid.setAuthenticated(true); when(authenticationManager.authenticate(new TokenAuthentication(VALID_TOKEN))).thenReturn(valid); queryFilter.attemptAuthentication(httpServletRequest, httpServletResponse); verify(authenticationManager).authenticate(any()); }
@Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { if (!request.getMethod().equals(httpMethod.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } if (protectedByCertificate && (SecurityContextHolder.getContext().getAuthentication() == null || !(SecurityContextHolder.getContext().getAuthentication().getCredentials() instanceof X509Certificate) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated())) { throw new InvalidCertificateException("Invalid certificate."); } String token = authenticationService.getJwtTokenFromRequest(request) .orElseThrow(() -> new TokenNotProvidedException("Authorization token not provided.")); Authentication result = this.getAuthenticationManager().authenticate(new TokenAuthentication(token)); if (result.isAuthenticated()) { return result; } else { throw new TokenNotValidException("JWT Token is not authenticated"); } }
QueryFilter extends AbstractAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { if (!request.getMethod().equals(httpMethod.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } if (protectedByCertificate && (SecurityContextHolder.getContext().getAuthentication() == null || !(SecurityContextHolder.getContext().getAuthentication().getCredentials() instanceof X509Certificate) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated())) { throw new InvalidCertificateException("Invalid certificate."); } String token = authenticationService.getJwtTokenFromRequest(request) .orElseThrow(() -> new TokenNotProvidedException("Authorization token not provided.")); Authentication result = this.getAuthenticationManager().authenticate(new TokenAuthentication(token)); if (result.isAuthenticated()) { return result; } else { throw new TokenNotValidException("JWT Token is not authenticated"); } } }
QueryFilter extends AbstractAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { if (!request.getMethod().equals(httpMethod.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } if (protectedByCertificate && (SecurityContextHolder.getContext().getAuthentication() == null || !(SecurityContextHolder.getContext().getAuthentication().getCredentials() instanceof X509Certificate) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated())) { throw new InvalidCertificateException("Invalid certificate."); } String token = authenticationService.getJwtTokenFromRequest(request) .orElseThrow(() -> new TokenNotProvidedException("Authorization token not provided.")); Authentication result = this.getAuthenticationManager().authenticate(new TokenAuthentication(token)); if (result.isAuthenticated()) { return result; } else { throw new TokenNotValidException("JWT Token is not authenticated"); } } QueryFilter(
        String authEndpoint,
        AuthenticationSuccessHandler successHandler,
        AuthenticationFailureHandler failureHandler,
        AuthenticationService authenticationService,
        HttpMethod httpMethod,
        boolean protectedByCertificate,
        AuthenticationManager authenticationManager); }
QueryFilter extends AbstractAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { if (!request.getMethod().equals(httpMethod.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } if (protectedByCertificate && (SecurityContextHolder.getContext().getAuthentication() == null || !(SecurityContextHolder.getContext().getAuthentication().getCredentials() instanceof X509Certificate) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated())) { throw new InvalidCertificateException("Invalid certificate."); } String token = authenticationService.getJwtTokenFromRequest(request) .orElseThrow(() -> new TokenNotProvidedException("Authorization token not provided.")); Authentication result = this.getAuthenticationManager().authenticate(new TokenAuthentication(token)); if (result.isAuthenticated()) { return result; } else { throw new TokenNotValidException("JWT Token is not authenticated"); } } QueryFilter(
        String authEndpoint,
        AuthenticationSuccessHandler successHandler,
        AuthenticationFailureHandler failureHandler,
        AuthenticationService authenticationService,
        HttpMethod httpMethod,
        boolean protectedByCertificate,
        AuthenticationManager authenticationManager); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response); }
QueryFilter extends AbstractAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) { if (!request.getMethod().equals(httpMethod.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } if (protectedByCertificate && (SecurityContextHolder.getContext().getAuthentication() == null || !(SecurityContextHolder.getContext().getAuthentication().getCredentials() instanceof X509Certificate) || !SecurityContextHolder.getContext().getAuthentication().isAuthenticated())) { throw new InvalidCertificateException("Invalid certificate."); } String token = authenticationService.getJwtTokenFromRequest(request) .orElseThrow(() -> new TokenNotProvidedException("Authorization token not provided.")); Authentication result = this.getAuthenticationManager().authenticate(new TokenAuthentication(token)); if (result.isAuthenticated()) { return result; } else { throw new TokenNotValidException("JWT Token is not authenticated"); } } QueryFilter(
        String authEndpoint,
        AuthenticationSuccessHandler successHandler,
        AuthenticationFailureHandler failureHandler,
        AuthenticationService authenticationService,
        HttpMethod httpMethod,
        boolean protectedByCertificate,
        AuthenticationManager authenticationManager); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response);  }