SpringSecurityUserProvider implements UserProvider { @Override public FeatureUser getCurrentUser() { SecurityContext context = SecurityContextHolder.getContext(); Authentication authentication = context.getAuthentication(); if (authentication == null) { return null; } String name = getUserName(authentication); Set<String> authorities = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); boolean featureAdmin = isFeatureAdmin(authorities); SimpleFeatureUser user = new SimpleFeatureUser(name, featureAdmin); user.setAttribute(USER_ATTRIBUTE_ROLES, authorities); return user; } SpringSecurityUserProvider(String featureAdminAuthority); @Override FeatureUser getCurrentUser(); static final String USER_ATTRIBUTE_ROLES; }
@Test public void getCurrentUserWillCopyAuthoritiesFromAuthenticationIntoFeatureUser() { Collection authorities = new ArrayList<GrantedAuthority>(); authorities.add(new SimpleGrantedAuthority("ROLE_1")); authorities.add(new SimpleGrantedAuthority("ROLE_2")); when(authentication.getAuthorities()).thenReturn(authorities); FeatureUser user = userProvider.getCurrentUser(); Object authoritiesAttr = user.getAttribute(USER_ATTRIBUTE_ROLES); assertTrue(authoritiesAttr instanceof Set); Set authSet = (Set) authoritiesAttr; assertNotNull(authSet); Set<String> authoritySet = (Set<String>) authSet; assertEquals(2, authoritySet.size()); assertTrue(authoritySet.contains("ROLE_1")); assertTrue(authoritySet.contains("ROLE_2")); }