Skip to content

Instantly share code, notes, and snippets.

@BorzdeG
Last active August 19, 2017 06:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BorzdeG/675fac06a6338dad10a7 to your computer and use it in GitHub Desktop.
Save BorzdeG/675fac06a6338dad10a7 to your computer and use it in GitHub Desktop.
example Java 8 Streams
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.StringUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.stream.Collectors;
/**
* Строки в роли для Spring Security. В каждой строке может быть несколько ролей через запятую
* Результат: набор ролей, консолидированный из переданных roles
*/
public static Set<GrantedAuthority> toGrantedAuthoritiesDemo(final String[] roles) {
final Set<GrantedAuthority> authorities = Stream.of(roles)
.flatMap(e -> StringUtils.commaDelimitedListToSet(e).stream())
.distinct()
.sorted()
.map(e -> "ROLE_" + e)
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet());
LOG.trace("authorities: {}", authorities);
return authorities;
}
@ArthurGazizov
Copy link

ArthurGazizov commented Aug 19, 2017

I think that you can remove distinct function from this conveyer because you put roles into set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment