授权的自定义Realm(数据源)
/**
* 授权方法
* @param principalCollection
* @return
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
/*获取登陆进来的对象的所有信息,
这个对象是在认证的时候传递进来的*/
Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
//定义info,封装这个对象所有的角色和权限
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
/*如果这个用户不是超级管理员,则查询这个用户的所有角色和权限,然后封装进info*/
if(employee.isAdmin()==false){
List<String> rolelist=roleService.selectByUserId(employee.getId());
List<String> permissionlist=permissionService.selectByUserId(employee.getId());
//角色列表
info.addRoles(rolelist);
//权限列表
info.addStringPermissions(permissionlist);
}else{
/*如果是超级管理员,则角色为超级管理员,权限为所有,那个都可以访问*/
info.addRole("admin");
//权限列表
info.addStringPermission("*:*");
}
return info;
}
shiro.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www