@Test public void extractsUserFromSession() throws Exception { User amy = user().withFirstName("Amy").build(); given(userRepository.findOne(amy.getId())).willReturn(amy); CurrentUser user = new CurrentUser(amy); Principal p = new UsernamePasswordAuthenticationToken(user, null); Try<User> userTry = underTest.extractUserFromPrincipal(p); assertThat(userTry, isSuccess(equalTo(amy))); }
@Transactional(readOnly = true) public Try<User> extractUserFromPrincipal(Principal principal) { if (principal instanceof UsernamePasswordAuthenticationToken) { Object sessionPrincipal = ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); if (sessionPrincipal instanceof org.springframework.security.core.userdetails.User) { CurrentUser currentUser = (CurrentUser) sessionPrincipal; User user = userRepository.findOne(currentUser.getId()); return Try.success(user); } } return Try.failure(new UserSessionFailure("Incorrect authentication configuration. Could not extract user from session")); }
SessionService { @Transactional(readOnly = true) public Try<User> extractUserFromPrincipal(Principal principal) { if (principal instanceof UsernamePasswordAuthenticationToken) { Object sessionPrincipal = ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); if (sessionPrincipal instanceof org.springframework.security.core.userdetails.User) { CurrentUser currentUser = (CurrentUser) sessionPrincipal; User user = userRepository.findOne(currentUser.getId()); return Try.success(user); } } return Try.failure(new UserSessionFailure("Incorrect authentication configuration. Could not extract user from session")); } }
SessionService { @Transactional(readOnly = true) public Try<User> extractUserFromPrincipal(Principal principal) { if (principal instanceof UsernamePasswordAuthenticationToken) { Object sessionPrincipal = ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); if (sessionPrincipal instanceof org.springframework.security.core.userdetails.User) { CurrentUser currentUser = (CurrentUser) sessionPrincipal; User user = userRepository.findOne(currentUser.getId()); return Try.success(user); } } return Try.failure(new UserSessionFailure("Incorrect authentication configuration. Could not extract user from session")); } @Autowired  SessionService(UserRepository userRepository); }
SessionService { @Transactional(readOnly = true) public Try<User> extractUserFromPrincipal(Principal principal) { if (principal instanceof UsernamePasswordAuthenticationToken) { Object sessionPrincipal = ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); if (sessionPrincipal instanceof org.springframework.security.core.userdetails.User) { CurrentUser currentUser = (CurrentUser) sessionPrincipal; User user = userRepository.findOne(currentUser.getId()); return Try.success(user); } } return Try.failure(new UserSessionFailure("Incorrect authentication configuration. Could not extract user from session")); } @Autowired  SessionService(UserRepository userRepository); @Transactional(readOnly = true) Try<User> extractUserFromPrincipal(Principal principal); }
SessionService { @Transactional(readOnly = true) public Try<User> extractUserFromPrincipal(Principal principal) { if (principal instanceof UsernamePasswordAuthenticationToken) { Object sessionPrincipal = ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); if (sessionPrincipal instanceof org.springframework.security.core.userdetails.User) { CurrentUser currentUser = (CurrentUser) sessionPrincipal; User user = userRepository.findOne(currentUser.getId()); return Try.success(user); } } return Try.failure(new UserSessionFailure("Incorrect authentication configuration. Could not extract user from session")); } @Autowired  SessionService(UserRepository userRepository); @Transactional(readOnly = true) Try<User> extractUserFromPrincipal(Principal principal);  }