Created
July 25, 2017 16:23
-
-
Save sintetico82/ee5f4b735fcec6db2271bba55c4529d8 to your computer and use it in GitHub Desktop.
GrantedAuthoritiesMapper problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() { | |
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(domain, adUrls); | |
provider.setConvertSubErrorCodesToExceptions(true); | |
provider.setUseAuthenticationRequestCredentials(true); | |
provider.setAuthoritiesMapper(applicationADGrantedAuthoritiesMapper()); | |
return provider; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class ApplicationADGrantedAuthoritiesMapper implements GrantedAuthoritiesMapper { | |
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |
@Autowired | |
private RoleRepository roleRepository; | |
@Override | |
public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { | |
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(); | |
authList.add(new SimpleGrantedAuthority("ciao")); | |
if(authorities != null && authorities.size() >0 ) { | |
List<String> groups = authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); | |
List<Role> applicationRoles = roleRepository.findByActiveDirectoryGroupIn(groups); | |
if(applicationRoles != null && applicationRoles.size() >0) { | |
List<Permission> permissions = applicationRoles.stream().map(Role::getPermissions).flatMap(List::stream).collect(Collectors.toList()); | |
if(permissions != null) | |
for (Permission permission : permissions) { | |
authList.add(new SimpleGrantedAuthority(permission.getName())); | |
} | |
} | |
} | |
log.debug("authorities : {}", authList); | |
return authList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment