SpringCertificateValidationCallbackHandler extends AbstractCallbackHandler implements InitializingBean { @Override protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException { if (callback instanceof CertificateValidationCallback) { ((CertificateValidationCallback) callback).setValidator(new SpringSecurityCertificateValidator()); } else if (callback instanceof CleanupCallback) { SecurityContextHolder.clearContext(); } else { throw new UnsupportedCallbackException(callback); } }  void setAuthenticationManager(AuthenticationManager authenticationManager); void setIgnoreFailure(boolean ignoreFailure); @Override void afterPropertiesSet();  }
@Test public void testValidateCertificateValid() throws Exception { expect(authenticationManager.authenticate(isA(X509AuthenticationToken.class))) .andReturn(new TestingAuthenticationToken(certificate, null, Collections.<GrantedAuthority> emptyList())); replay(authenticationManager); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertTrue("Not authenticated", authenticated); Assert.assertNotNull("No Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(authenticationManager); }
@Test public void testValidateCertificateInvalid() throws Exception { expect(authenticationManager.authenticate(isA(X509AuthenticationToken.class))) .andThrow(new BadCredentialsException("")); replay(authenticationManager); callbackHandler.handleInternal(callback); boolean authenticated = callback.getResult(); Assert.assertFalse("Authenticated", authenticated); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); verify(authenticationManager); }
@Test public void testCleanUp() throws Exception { TestingAuthenticationToken authentication = new TestingAuthenticationToken(new Object(), new Object(), Collections.<GrantedAuthority> emptyList()); SecurityContextHolder.getContext().setAuthentication(authentication); CleanupCallback cleanupCallback = new CleanupCallback(); callbackHandler.handleInternal(cleanupCallback); Assert.assertNull("Authentication created", SecurityContextHolder.getContext().getAuthentication()); }