MappableAttributesRetriever接口

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());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值