日期:2014-05-18 浏览次数:20680 次
@Autowired
private IUserService userService;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection ps) {
Set<String> permissions = new HashSet<String>();
Set<String> roles = new HashSet<String>();//保持为空,直接使用权限
Collection<User> principalsList = ps.byType(User.class);
if (principalsList.isEmpty()) {
throw new AuthorizationException("Empty principals list!");
}
for(User u : principalsList) {
User oneUser = userService.findById(u.getUsid());
permissions.addAll(getAllPermissions(oneUser));
}
SimpleAuthorizationInfo anthInfo = new SimpleAuthorizationInfo(roles);
anthInfo.setStringPermissions(permissions);
return anthInfo;
}
/**
* 传入一个User对象,查出它的全部权限,以{@code Set<String>}形式返回
*/
public Set<String> getAllPermissions(User user) {
Set<String> permissions = new HashSet<String>();
Role role = user.getRole();
if(role == null) {
return new HashSet<String>();
}
Set<Permission> pers = role.getPers();
for(Permission p : pers) {
permissions.add(p.getPerName());
}
return permissions;
}