SpringSecurityPasswordValidationCallbackHandler extends AbstractWsPasswordCallbackHandler implements InitializingBean { @Override protected void handleUsernameTokenPrincipal(UsernameTokenPrincipalCallback callback) throws IOException, UnsupportedCallbackException { UserDetails user = loadUserDetails(callback.getPrincipal().getName()); WSUsernameTokenPrincipalImpl principal = callback.getPrincipal(); UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), user.getAuthorities()); if (logger.isDebugEnabled()) { logger.debug("Authentication success: " + authRequest.toString()); } SecurityContextHolder.getContext().setAuthentication(authRequest); }  void setUserCache(UserCache userCache); void setUserDetailsService(UserDetailsService userDetailsService); @Override void afterPropertiesSet();  }
@Test public void testHandleUsernameTokenPrincipal() throws Exception { UserDetailsService userDetailsService = createMock(UserDetailsService.class); callbackHandler.setUserDetailsService(userDetailsService); expect(userDetailsService.loadUserByUsername("Ernie")).andReturn(user).anyTimes(); replay(userDetailsService); callbackHandler.handleUsernameTokenPrincipal(callback); SecurityContext context = SecurityContextHolder.getContext(); Assert.assertNotNull("SecurityContext must not be null", context); Authentication authentication = context.getAuthentication(); Assert.assertNotNull("Authentication must not be null", authentication); Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); Assert.assertTrue("GrantedAuthority[] must not be null or empty", (authorities != null && authorities.size() > 0)); Assert.assertEquals("Unexpected authority", grantedAuthority, authorities.iterator().next()); verify(userDetailsService); }