SpringSecurityAuthenticationService implements AuthenticationService { @Override public boolean isCurrentUserAuthenticated() { return SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().isAuthenticated() && !(SecurityContextHolder.getContext().getAuthentication() instanceof AnonymousAuthenticationToken); }  @Override UserDetailsAdapter getCurrentUserDetails(); @Override boolean isCurrentUserAuthenticated();  }
@Test public void isCurrentUserAuthenticatedShouldBeAuthenticatedWhenUserIsLoggedInAndAuthenticated() { UserDetailsAdapter userDetailsAdapter = new UserDetailsAdapter(USER_ID, USER_PASSWORD, USER_NAME); SecurityContextHolder.getContext() .setAuthentication(new UsernamePasswordAuthenticationToken(userDetailsAdapter, null, Arrays.asList(new SimpleGrantedAuthority("ROLE_GRANTED")))); boolean result = springSecurityAuthenticationService.isCurrentUserAuthenticated(); assertTrue(result); }
@Test public void isCurrentUserAuthenticatedShouldNotBeAuthenticatedWhenUserIsLoggedAndNotAuthenticated() { UserDetailsAdapter userDetailsAdapter = new UserDetailsAdapter(USER_ID, USER_PASSWORD, USER_NAME); SecurityContextHolder.getContext() .setAuthentication(new UsernamePasswordAuthenticationToken(userDetailsAdapter, null)); boolean result = springSecurityAuthenticationService.isCurrentUserAuthenticated(); assertFalse(result); }
@Test public void isCurrentUserAuthenticatedShouldNotBeAuthenticatedWhenUserIsNotLoggedIn() { SecurityContextHolder.getContext() .setAuthentication(null); boolean result = springSecurityAuthenticationService.isCurrentUserAuthenticated(); assertFalse(result); }
@Test public void isCurrentUserAuthenticatedShouldNotBeAuthenticatedWhenUserIsAuthenticatedByAnonymousAuthentication() { SecurityContextHolder.getContext() .setAuthentication(new AnonymousAuthenticationToken(USER_NAME, USER_NAME, AuthorityUtils.createAuthorityList(USER_NAME))); boolean result = springSecurityAuthenticationService.isCurrentUserAuthenticated(); assertFalse(result); }