访问决策管理器是做什么的??
@Component
public class AccessDecisionManagerImpl implements AccessDecisionManager {
@Override
public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> collection) throws AccessDeniedException, InsufficientAuthenticationException {
// 获取用户权限列表
List<String> permissionList = authentication.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList());
for (ConfigAttribute item : collection) {
if (permissionList.contains(item.getAttribute())) {
return;
}
}
throw new AccessDeniedException("没有操作权限");
}
@Override
public boolean supports(ConfigAttribute configAttribute) {
return true;
}
@Override
public boolean supports(Class aClass) {
return true;
}
}
这一段代码是什么意思呀
// 获取用户权限列表
List permissionList = authentication.getAuthorities()
.stream()
.map(GrantedAuthority::getAuthority)
.collect(Collectors.toList());
for (ConfigAttribute item : collection) {
if (permissionList.contains(item.getAttribute())) {
return;
}
}
throw new AccessDeniedException("没有操作权限");
}
decide方法用来校验是否拥有某种权限
第一块是Java8里面的stream,用来筛选所有的权限
具体可以详细了解下Java8 stream。这里大概意思是将authrooties转变成流处理,map是转换,只取authority,类型是string,最后collect是将这些全部收集起来变成list,所以返回值是List‹String
›
第二块for循环用来判断用户是否含有指定的权限(入参collection)。如果包含,说明有权限,结束(return)。否则说明没有权限,抛出异常。