### Question:
JwtAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) { Assert.notNull(authentication, "Authentication is missing"); Assert.isInstanceOf(JwtAuthenticationToken.class, authentication, "This method only accepts JwtAuthenticationToken"); String jwtToken = authentication.getName(); if (authentication.getPrincipal() == null || jwtToken == null) { throw new AuthenticationCredentialsNotFoundException("Authentication token is missing"); } final SignedJWT signedJWT; try { signedJWT = SignedJWT.parse(jwtToken); boolean isVerified = signedJWT.verify(new MACVerifier(SecurityConstant.JWT_SECRET.getBytes())); if(!isVerified){ throw new BadCredentialsException("Invalid token signature"); } LocalDateTime expirationTime = LocalDateTime.ofInstant( signedJWT.getJWTClaimsSet().getExpirationTime().toInstant(), ZoneId.systemDefault()); if (LocalDateTime.now(ZoneId.systemDefault()).isAfter(expirationTime)) { throw new CredentialsExpiredException("Token expired"); } return new JwtAuthenticationToken(signedJWT, null, null); } catch (ParseException e) { throw new InternalAuthenticationServiceException("Unreadable token"); } catch (JOSEException e) { throw new InternalAuthenticationServiceException("Unreadable signature"); } }  @Override boolean supports(Class<?> authentication); @Override Authentication authenticate(Authentication authentication);  }### Answer:
@Test public void shouldAuthenticate() { final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(); final JwtAuthenticationToken jwtAuthenticationToken = new JwtAuthenticationToken(getJWT(120)); Authentication authentication = jwtAuthenticationProvider.authenticate(new JwtAuthenticationToken(jwtAuthenticationToken)); Assert.assertTrue(authentication.isAuthenticated()); }
@Test(expected = CredentialsExpiredException.class) public void shouldFailOnExpiredToken() { final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(); final JwtAuthenticationToken jwtAuthenticationToken = new JwtAuthenticationToken(getJWT(-120)); jwtAuthenticationProvider.authenticate(new JwtAuthenticationToken(jwtAuthenticationToken)); }
@Test(expected = BadCredentialsException.class) public void shouldFailOnBadSignature() { final JwtAuthenticationProvider jwtAuthenticationProvider = new JwtAuthenticationProvider(); String jwt = getJWT(120); int signIndex = jwt.lastIndexOf('.'); jwt = jwt.substring(0, signIndex) + ".123456"; final JwtAuthenticationToken jwtAuthenticationToken = new JwtAuthenticationToken(jwt); jwtAuthenticationProvider.authenticate(new JwtAuthenticationToken(jwtAuthenticationToken)); }