Set<String> getMappableAttributes(); // 映射set集合权限
SimpleMappableAttributesRetriever实现类
// 通过setMappableAttributes方法设置set集合权限
// 通过getMappableAttributes方法返回
public class SimpleMappableAttributesRetriever {
private Set<String> mappableAttributes = null;
@Override
public Set<String> getMappableAttributes() {
return this.mappableAttributes;
}
public void setMappableAttributes(Set<String> aMappableRoles) {
this.mappableAttributes = new HashSet<>();
this.mappableAttributes.addAll(aMappableRoles);
this.mappableAttributes = Collections.unmodifiableSet(this.mappableAttributes);
}
}
// 数组权限映射
// 实现Attributes2GrantedAuthoritiesMapper接口方法
// Collection<? extends GrantedAuthority> getGrantedAuthorities(Collection<String>
// attributes)
public class SimpleAttributes2GrantedAuthoritiesMapper {
private String attributePrefix = "ROLE_";
// 与convertAttributeToLowerCase不能同时为true
private boolean convertAttributeToUpperCase = false;
private boolean convertAttributeToLowerCase = false;
private boolean addPrefixIfAlreadyExisting = false;
// 由 "aaa"得到 SimpleGrantedAuthority("ROLE_aaa")
// 由 "ROLE_c"得到 SimpleGrantedAuthority("ROLE_c")
private GrantedAuthority getGrantedAuthority(String attribute) {
if (isConvertAttributeToLowerCase()) {
attribute = attribute.toLowerCase(Locale.getDefault());
}
else if (isConvertAttributeToUpperCase()) {
attribute = attribute.toUpperCase(Locale.getDefault());
}
if (isAddPrefixIfAlreadyExisting() || !attribute.startsWith(getAttributePrefix())) {
return new SimpleGrantedAuthority(getAttributePrefix() + attribute);
}
else {
return new SimpleGrantedAuthority(attribute);
}
}
// 一对一映射
public List<GrantedAuthority> getGrantedAuthorities(Collection<String> attributes) {
List<GrantedAuthority> result = new ArrayList<>(attributes.size());
for (String attribute : attributes) {
result.add(getGrantedAuthority(attribute));
}
return result;
}
}
MapBasedAttributes2GrantedAuthoritiesMapper实现类
// 支持一对一和一对多
public class MapBasedAttributes2GrantedAuthoritiesMapper {
private Map<String, Collection<GrantedAuthority>> attributes2grantedAuthoritiesMap = null;
private String stringSeparator = ",";
// 取attributes2grantedAuthoritiesMap的key
private Set<String> mappableAttributes = null;
// 返回set权限集合
@Override
public Set<String> getMappableAttributes() {
return this.mappableAttributes;
}
// 循环attributes,根据attributes2grantedAuthoritiesMap中的key取出对应的value组成数组
// 返回
public List<GrantedAuthority> getGrantedAuthorities(Collection<String> attributes) {
ArrayList<GrantedAuthority> result = new ArrayList<>();
for (String attribute : attributes) {
Collection<GrantedAuthority> granted =
this.attributes2grantedAuthoritiesMap.get(attribute);
if (granted != null) {
result.addAll(granted);
}
}
result.trimToSize();
return result;
}
// 对象的属性attributes2grantedAuthoritiesMap集合的key必须为String.class,value的值数组
// 参数的value可以是集合或者数组或null或String,都转为SimpleGrantedAuthority
public void setAttributes2grantedAuthoritiesMap(final Map<?, ?>
attributes2grantedAuthoritiesMap) {
Assert.notEmpty(attributes2grantedAuthoritiesMap,
"A non-empty attributes2grantedAuthoritiesMap must be supplied");
this.attributes2grantedAuthoritiesMap =
preProcessMap(attributes2grantedAuthoritiesMap);
this.mappableAttributes =
Collections.unmodifiableSet(this.attributes2grantedAuthoritiesMap.keySet());
}
}