@Test void authenticate() { RestAuthenticationToken authToken = new RestAuthenticationToken("token"); assertFalse(authToken.isAuthenticated()); when(tokenService.findUserByToken("token")) .thenReturn( new User("username", "password", Arrays.asList(new SimpleGrantedAuthority("admin")))); Authentication auth = tokenAuthenticationProvider.authenticate(authToken); assertNotNull(auth); assertTrue(auth.isAuthenticated()); assertEquals("username", auth.getName()); assertEquals(1, auth.getAuthorities().size()); assertEquals("admin", auth.getAuthorities().iterator().next().getAuthority()); }
@Override @RunAsSystem public Authentication authenticate(Authentication authentication) { if (!supports(authentication.getClass())) throw new IllegalArgumentException("Only RestAuthenticationToken is supported"); RestAuthenticationToken authToken = (RestAuthenticationToken) authentication; if (authToken.getToken() != null) { UserDetails userDetails = tokenService.findUserByToken(authToken.getToken()); userDetailsChecker.check(userDetails); authToken = new RestAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken()); } return authToken; }
TokenAuthenticationProvider implements AuthenticationProvider { @Override @RunAsSystem public Authentication authenticate(Authentication authentication) { if (!supports(authentication.getClass())) throw new IllegalArgumentException("Only RestAuthenticationToken is supported"); RestAuthenticationToken authToken = (RestAuthenticationToken) authentication; if (authToken.getToken() != null) { UserDetails userDetails = tokenService.findUserByToken(authToken.getToken()); userDetailsChecker.check(userDetails); authToken = new RestAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken()); } return authToken; } }
TokenAuthenticationProvider implements AuthenticationProvider { @Override @RunAsSystem public Authentication authenticate(Authentication authentication) { if (!supports(authentication.getClass())) throw new IllegalArgumentException("Only RestAuthenticationToken is supported"); RestAuthenticationToken authToken = (RestAuthenticationToken) authentication; if (authToken.getToken() != null) { UserDetails userDetails = tokenService.findUserByToken(authToken.getToken()); userDetailsChecker.check(userDetails); authToken = new RestAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken()); } return authToken; } TokenAuthenticationProvider(
      TokenService tokenService, UserDetailsChecker userDetailsChecker); }
TokenAuthenticationProvider implements AuthenticationProvider { @Override @RunAsSystem public Authentication authenticate(Authentication authentication) { if (!supports(authentication.getClass())) throw new IllegalArgumentException("Only RestAuthenticationToken is supported"); RestAuthenticationToken authToken = (RestAuthenticationToken) authentication; if (authToken.getToken() != null) { UserDetails userDetails = tokenService.findUserByToken(authToken.getToken()); userDetailsChecker.check(userDetails); authToken = new RestAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken()); } return authToken; } TokenAuthenticationProvider(
      TokenService tokenService, UserDetailsChecker userDetailsChecker); @Override @RunAsSystem Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication); }
TokenAuthenticationProvider implements AuthenticationProvider { @Override @RunAsSystem public Authentication authenticate(Authentication authentication) { if (!supports(authentication.getClass())) throw new IllegalArgumentException("Only RestAuthenticationToken is supported"); RestAuthenticationToken authToken = (RestAuthenticationToken) authentication; if (authToken.getToken() != null) { UserDetails userDetails = tokenService.findUserByToken(authToken.getToken()); userDetailsChecker.check(userDetails); authToken = new RestAuthenticationToken( userDetails, userDetails.getPassword(), userDetails.getAuthorities(), authToken.getToken()); } return authToken; } TokenAuthenticationProvider(
      TokenService tokenService, UserDetailsChecker userDetailsChecker); @Override @RunAsSystem Authentication authenticate(Authentication authentication); @Override boolean supports(Class<?> authentication);  }