Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save crmaxx/a2660237a6f38703a3d5 to your computer and use it in GitHub Desktop.
Save crmaxx/a2660237a6f38703a3d5 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment