@Test public void testMultipleRoles() { Map<String, String> roleGroupMapping = new HashMap<>(); roleGroupMapping.put("apache1", "ROLE_USER"); roleGroupMapping.put("APACHE2", "ROLE_ADMIN"); LogSearchLdapAuthorityMapper underTest = new LogSearchLdapAuthorityMapper(roleGroupMapping); List<GrantedAuthority> result = new ArrayList<>(underTest.mapAuthorities(generateAuthorities())); assertEquals(2, result.size()); }
@Override public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { if (!groupRoleMap.isEmpty() && !authorities.isEmpty()) { List<SimpleGrantedAuthority> newAuthorities = new ArrayList<>(); for (GrantedAuthority authority : authorities) { String withoutRoleStringLowercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toLowerCase(); String withoutRoleStringUppercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toUpperCase(); String simpleRoleLowercaseString = authority.toString().toLowerCase(); String simpleRoleUppercaseString = authority.toString().toUpperCase(); if (addAuthoritiy(newAuthorities, withoutRoleStringLowercase)) continue; if (addAuthoritiy(newAuthorities, withoutRoleStringUppercase)) continue; if (addAuthoritiy(newAuthorities, simpleRoleLowercaseString)) continue; addAuthoritiy(newAuthorities, simpleRoleUppercaseString); } return newAuthorities; } return authorities; }
LogSearchLdapAuthorityMapper implements GrantedAuthoritiesMapper { @Override public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { if (!groupRoleMap.isEmpty() && !authorities.isEmpty()) { List<SimpleGrantedAuthority> newAuthorities = new ArrayList<>(); for (GrantedAuthority authority : authorities) { String withoutRoleStringLowercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toLowerCase(); String withoutRoleStringUppercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toUpperCase(); String simpleRoleLowercaseString = authority.toString().toLowerCase(); String simpleRoleUppercaseString = authority.toString().toUpperCase(); if (addAuthoritiy(newAuthorities, withoutRoleStringLowercase)) continue; if (addAuthoritiy(newAuthorities, withoutRoleStringUppercase)) continue; if (addAuthoritiy(newAuthorities, simpleRoleLowercaseString)) continue; addAuthoritiy(newAuthorities, simpleRoleUppercaseString); } return newAuthorities; } return authorities; } }
LogSearchLdapAuthorityMapper implements GrantedAuthoritiesMapper { @Override public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { if (!groupRoleMap.isEmpty() && !authorities.isEmpty()) { List<SimpleGrantedAuthority> newAuthorities = new ArrayList<>(); for (GrantedAuthority authority : authorities) { String withoutRoleStringLowercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toLowerCase(); String withoutRoleStringUppercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toUpperCase(); String simpleRoleLowercaseString = authority.toString().toLowerCase(); String simpleRoleUppercaseString = authority.toString().toUpperCase(); if (addAuthoritiy(newAuthorities, withoutRoleStringLowercase)) continue; if (addAuthoritiy(newAuthorities, withoutRoleStringUppercase)) continue; if (addAuthoritiy(newAuthorities, simpleRoleLowercaseString)) continue; addAuthoritiy(newAuthorities, simpleRoleUppercaseString); } return newAuthorities; } return authorities; } LogSearchLdapAuthorityMapper(Map<String, String> groupRoleMap); }
LogSearchLdapAuthorityMapper implements GrantedAuthoritiesMapper { @Override public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { if (!groupRoleMap.isEmpty() && !authorities.isEmpty()) { List<SimpleGrantedAuthority> newAuthorities = new ArrayList<>(); for (GrantedAuthority authority : authorities) { String withoutRoleStringLowercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toLowerCase(); String withoutRoleStringUppercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toUpperCase(); String simpleRoleLowercaseString = authority.toString().toLowerCase(); String simpleRoleUppercaseString = authority.toString().toUpperCase(); if (addAuthoritiy(newAuthorities, withoutRoleStringLowercase)) continue; if (addAuthoritiy(newAuthorities, withoutRoleStringUppercase)) continue; if (addAuthoritiy(newAuthorities, simpleRoleLowercaseString)) continue; addAuthoritiy(newAuthorities, simpleRoleUppercaseString); } return newAuthorities; } return authorities; } LogSearchLdapAuthorityMapper(Map<String, String> groupRoleMap); @Override Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities); }
LogSearchLdapAuthorityMapper implements GrantedAuthoritiesMapper { @Override public Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { if (!groupRoleMap.isEmpty() && !authorities.isEmpty()) { List<SimpleGrantedAuthority> newAuthorities = new ArrayList<>(); for (GrantedAuthority authority : authorities) { String withoutRoleStringLowercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toLowerCase(); String withoutRoleStringUppercase = StringUtils.removeStart(authority.toString(), ROLE_PREFIX).toUpperCase(); String simpleRoleLowercaseString = authority.toString().toLowerCase(); String simpleRoleUppercaseString = authority.toString().toUpperCase(); if (addAuthoritiy(newAuthorities, withoutRoleStringLowercase)) continue; if (addAuthoritiy(newAuthorities, withoutRoleStringUppercase)) continue; if (addAuthoritiy(newAuthorities, simpleRoleLowercaseString)) continue; addAuthoritiy(newAuthorities, simpleRoleUppercaseString); } return newAuthorities; } return authorities; } LogSearchLdapAuthorityMapper(Map<String, String> groupRoleMap); @Override Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities);  }