org.apache.shiro.authz.AuthorizationInfo Java Examples

The following examples show how to use org.apache.shiro.authz.AuthorizationInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: DouBiNovel   Author: luckymorning   File: MyShiroRealm.java    License: Apache License 2.0 6 votes vote down vote up
@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        System.out.println("权限配置-->com.cn.lucky.morning.model.web.shiro.MyShiroRealm.doGetAuthorizationInfo()");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        User user  = (User) principals.getPrimaryPrincipal();
        Role role = roleService.getById(user.getRoleId());
        if (role == null){
            return null;
        }
        authorizationInfo.addRole(role.getId().toString());
        if (Objects.equals(Const.role.IS_SUPER,role.getIsSuper())){
            authorizationInfo.addStringPermission(Const.role.ROLE_SUPER);
        }
        if (!StringUtils.isEmpty(role.getAuthority())){
            String [] authorityStrs = role.getAuthority().split(",");
            for (String id : authorityStrs){
                Authority authority = authorityService.getById(Long.valueOf(id));
                if (authority!=null){
                    authorizationInfo.addStringPermission(authority.getCode());
                }
            }
        }
        return authorizationInfo;
    }
 
Example #2
Source Project: erp-framework   Author: chyanwu   File: MyShiroRealm.java    License: MIT License 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
    // 当前登录信息
    ShiroUser shiroUser = (ShiroUser) principalCollection.getPrimaryPrincipal();
    // 查询当前用户
    ErpUser user = erpUserService.findUserByLoginName(shiroUser.getLoginName());
    // 添加角色和权限
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    List<ErpRole> roles = user.getRoles();
    for(ErpRole role: roles) {
        if(StringUtils.isNotBlank(role.getName())) {
            simpleAuthorizationInfo.addRole(role.getName());
        }
    }
    List<ErpMenu> menus = user.getMenus();
    for(ErpMenu menu: menus) {
        if(StringUtils.isNotBlank(menu.getPermission())) {
            simpleAuthorizationInfo.addStringPermission(menu.getPermission());
        }
    }
    return simpleAuthorizationInfo;
}
 
Example #3
Source Project: spring-boot-shiro   Author: MRLEILOVE   File: MyShiroRealm.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * create by: leigq
 * description: 授权
 * create time: 2019/7/1 10:32
 *
 * @return 权限信息,包括角色以及权限
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.warn("开始执行授权操作.......");

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    //如果身份认证的时候没有传入User对象,这里只能取到userName
    //也就是SimpleAuthenticationInfo构造的时候第一个参数传递需要User对象
    User user = (User) principals.getPrimaryPrincipal();

    // 查询用户角色,一个用户可能有多个角色
    List<Role> roles = iRoleService.getUserRoles(user.getUserId());

    for (Role role : roles) {
        authorizationInfo.addRole(role.getRole());
        // 根据角色查询权限
        List<Permission> permissions = iPermissionService.getRolePermissions(role.getRoleId());
        for (Permission p : permissions) {
            authorizationInfo.addStringPermission(p.getPermission());
        }
    }
    return authorizationInfo;
}
 
Example #4
Source Project: SENS   Author: saysky   File: NormalRealm.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    User user = (User) principals.getPrimaryPrincipal();

    List<Role> roles = roleService.listRolesByUserId(user.getId());
    for (Role role : roles) {
        authorizationInfo.addRole(role.getRole());
        List<Permission> permissions = permissionService.listPermissionsByRoleId(role.getId());
        //把权限的URL全部放到authorizationInfo中去
        Set<String> urls = permissions.stream().map(p -> p.getUrl()).collect(Collectors.toSet());
        authorizationInfo.addStringPermissions(urls);

    }
    return authorizationInfo;
}
 
Example #5
Source Project: SENS   Author: saysky   File: FreeRealm.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    System.out.println("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    User user = (User) principals.getPrimaryPrincipal();

    List<Role> roles = roleService.listRolesByUserId(user.getId());
    for (Role role : roles) {
        authorizationInfo.addRole(role.getRole());
        List<Permission> permissions = permissionService.listPermissionsByRoleId(role.getId());
        for (Permission p : permissions) {
            authorizationInfo.addStringPermission(p.getUrl());
        }
    }
    return authorizationInfo;
}
 
Example #6
Source Project: Moss   Author: SpringCloud   File: DBRealm.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals,
                                                      LdapContextFactory ldapContextFactory) throws NamingException {
    System.out.println("————权限认证————");
    String username = JwtUtil.getUsername(principals.toString());
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //获得该用户角色
    //String role = userMapper.getRole(username);
    //每个角色拥有默认的权限
    //String rolePermission = userMapper.getRolePermission(username);
    //每个用户可以设置新的权限
    //String permission = userMapper.getPermission(username);
    Set<String> roleSet = new HashSet<>();
    Set<String> permissionSet = new HashSet<>();
    //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
   // roleSet.add(role);
   // permissionSet.add(rolePermission);
    //permissionSet.add(permission);
    //设置该用户拥有的角色和权限
    info.setRoles(roleSet);
    info.setStringPermissions(permissionSet);
    return info;
}
 
Example #7
Source Project: Moss   Author: SpringCloud   File: LdapRealm.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals,
                                                      LdapContextFactory ldapContextFactory) throws NamingException {
    System.out.println("————权限认证————");
    String username = JwtUtil.getUsername(principals.toString());
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //获得该用户角色
    //String role = userMapper.getRole(username);
    //每个角色拥有默认的权限
    //String rolePermission = userMapper.getRolePermission(username);
    //每个用户可以设置新的权限
    //String permission = userMapper.getPermission(username);
    Set<String> roleSet = new HashSet<>();
    Set<String> permissionSet = new HashSet<>();
    //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
   // roleSet.add(role);
   // permissionSet.add(rolePermission);
    //permissionSet.add(permission);
    //设置该用户拥有的角色和权限
    info.setRoles(roleSet);
    info.setStringPermissions(permissionSet);
    return info;
}
 
Example #8
Source Project: SpringAll   Author: wuyouzhuguli   File: ShiroRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 获取用户角色和权限
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
	User user = (User) SecurityUtils.getSubject().getPrincipal();
	String userName = user.getUserName();

	System.out.println("用户" + userName + "获取权限-----ShiroRealm.doGetAuthorizationInfo");
	SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

	// 获取用户角色集
	List<Role> roleList = userRoleMapper.findByUserName(userName);
	Set<String> roleSet = new HashSet<String>();
	for (Role r : roleList) {
		roleSet.add(r.getName());
	}
	simpleAuthorizationInfo.setRoles(roleSet);

	// 获取用户权限集
	List<Permission> permissionList = userPermissionMapper.findByUserName(userName);
	Set<String> permissionSet = new HashSet<String>();
	for (Permission p : permissionList) {
		permissionSet.add(p.getName());
	}
	simpleAuthorizationInfo.setStringPermissions(permissionSet);
	return simpleAuthorizationInfo;
}
 
Example #9
Source Project: SpringAll   Author: wuyouzhuguli   File: ShiroRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 获取用户角色和权限
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
	User user = (User) SecurityUtils.getSubject().getPrincipal();
	String userName = user.getUserName();

	System.out.println("用户" + userName + "获取权限-----ShiroRealm.doGetAuthorizationInfo");
	SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

	// 获取用户角色集
	List<Role> roleList = userRoleMapper.findByUserName(userName);
	Set<String> roleSet = new HashSet<String>();
	for (Role r : roleList) {
		roleSet.add(r.getName());
	}
	simpleAuthorizationInfo.setRoles(roleSet);

	// 获取用户权限集
	List<Permission> permissionList = userPermissionMapper.findByUserName(userName);
	Set<String> permissionSet = new HashSet<String>();
	for (Permission p : permissionList) {
		permissionSet.add(p.getName());
	}
	simpleAuthorizationInfo.setStringPermissions(permissionSet);
	return simpleAuthorizationInfo;
}
 
Example #10
Source Project: NutzSite   Author: TomYule   File: SimpleAuthorizingRealm.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	// null usernames are invalid
	if (principals == null) {
		throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
	}
	User user = (User) principals.getPrimaryPrincipal();
	if (user == null) {
		return null;
	}
	// 角色列表
	Set<String> roles =userService.getRoleCodeList(user.getId());
	// 功能列表
	Set<String> menus = userService.getPermsByUserId(user.getId());

	SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
	auth.setRoles(roles);
	auth.setStringPermissions(menus);
	return auth;
}
 
Example #11
Source Project: scaffold-cloud   Author: Fatezhang   File: MyShiroRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

    String username = (String) principalCollection.getPrimaryPrincipal();

    ResponseModel<SysOperateBO> sysOperateResponse = sysOperateFeign.findByUserName(username);
    final SysOperateBO sysOperate = sysOperateResponse.getData();
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    List<SysMenuBO> menuList = sysMenuFeign.findByOperateId(sysOperate.getId()).getData();
    if (CollectionUtil.isNotEmpty(menuList)) {
        for (SysMenuBO menu : menuList) {
            if (StrUtil.isNotBlank(menu.getCode())) {
                info.addStringPermission(menu.getCode());
            }
        }
    }
    return info;
}
 
Example #12
Source Project: express-ssm   Author: jitwxs   File: LoginRealm.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 身份授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String userName = (String) getAvailablePrincipal(principalCollection);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    SysUser sysUser = userService.getByUserName(userName);
    // 获取角色对象

    Integer roleId = sysUser.getRoleId();

    //通过用户名从数据库获取权限/角色信息
    Set<String> r = new HashSet<>();

    if (roleId != null) {
        r.add(RoleEnum.getName(roleId));
        info.setRoles(r);
    }

    return info;
}
 
Example #13
Source Project: blog-sample   Author: jitwxs   File: ShiroRealm.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 角色权限和对应权限添加
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    // 获取用户名
    String name = (String) principalCollection.getPrimaryPrincipal();
    // 获取用户对象
    User user = userService.findByName(name);
    // 添加角色和权限

    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

    List<Role> roles = getRoles(user.getId());

    for (Role role : roles) {
        // 添加角色
        simpleAuthorizationInfo.addRole(role.getName());

        // 添加权限
        List<Permission> permissions = getPermission(role.getId());
        for (Permission permission : permissions) {
            simpleAuthorizationInfo.addStringPermission(permission.getName());
        }
    }
    return simpleAuthorizationInfo;
}
 
Example #14
Source Project: Goku.Framework.CoreUI   Author: nbfujx   File: ShiroRealm.java    License: MIT License 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    SysUser user = sysuserextmapper.getUserByUsername((String) principalCollection.getPrimaryPrincipal());
    //把principals放session中 key=userId value=principals
    SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()),SecurityUtils.getSubject().getPrincipals());
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //赋予角色
    for(SysRole userRole:user.getSysRoleList()){
        info.addRole(userRole.getKey());
    }
    //赋予权限
    for(SysMenu menu:sysmenuextmapper.getMenuByUserId(user.getId())){
        if(!"".equals(menu.getPermission())) {
            info.addStringPermission(menu.getPermission());
        }
    }

    return info;

}
 
Example #15
Source Project: easyweb-shiro   Author: uxawseny   File: UserRealm.java    License: MIT License 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    User user = (User) SecurityUtils.getSubject().getPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    // 角色
    List<Role> userRoles = roleService.getByUserId(user.getUserId());
    Set<String> roles = new HashSet<>();
    for (int i = 0; i < userRoles.size(); i++) {
        roles.add(String.valueOf(userRoles.get(i).getRoleId()));
    }
    authorizationInfo.setRoles(roles);
    // 权限
    List<Authorities> authorities = authoritiesService.listByUserId(user.getUserId());
    Set<String> permissions = new HashSet<>();
    for (int i = 0; i < authorities.size(); i++) {
        String authority = authorities.get(i).getAuthority();
        if (StringUtil.isNotBlank(authority)) {
            permissions.add(authority);
        }
    }
    authorizationInfo.setStringPermissions(permissions);
    return authorizationInfo;
}
 
Example #16
Source Project: Movie_Recommend   Author: LuckyZXL2016   File: MovieRealm.java    License: MIT License 6 votes vote down vote up
@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Admin admin = (Admin)principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // 根据用户名查询用户拥有的角色
//        AdminExample adminExample = new AdminExample();
//        AdminExample.Criteria criteria = adminExample.createCriteria();
//        criteria.andAdminnameEqualTo(adminname);
//        List<Admin> list = adminMapper.selectByExample(adminExample);
        Set<String> roleNames = new HashSet<String>();
        if (0 == admin.getRole()) {
            roleNames.add("admin");
        } else {
            roleNames.add("user");
        }
        // 将角色名称提供给info
        authorizationInfo.setRoles(roleNames);

        return authorizationInfo;
    }
 
Example #17
Source Project: ShiroJwt   Author: dolyw   File: UserRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 只有当需要检测用户权限的时候才会调用此方法,例如checkRole,checkPermission之类的
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    String account = JwtUtil.getClaim(principalCollection.toString(), Constant.ACCOUNT);
    UserDto userDto = new UserDto();
    userDto.setAccount(account);
    // 查询用户角色
    List<RoleDto> roleDtos = roleMapper.findRoleByUser(userDto);
    for (RoleDto roleDto : roleDtos) {
        if (roleDto != null) {
            // 添加角色
            simpleAuthorizationInfo.addRole(roleDto.getName());
            // 根据用户角色查询权限
            List<PermissionDto> permissionDtos = permissionMapper.findPermissionByRole(roleDto);
            for (PermissionDto permissionDto : permissionDtos) {
                if (permissionDto != null) {
                    // 添加权限
                    simpleAuthorizationInfo.addStringPermission(permissionDto.getPerCode());
                }
            }
        }
    }
    return simpleAuthorizationInfo;
}
 
Example #18
Source Project: jboot-admin   Author: pkanyue   File: LoginAuth.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public AuthorizationInfo buildAuthorizationInfo(PrincipalCollection principals) {
    String loginName = (String) principals.fromRealm("ShiroDbRealm").iterator().next();

    RoleService sysRoleApi = Jboot.service(RoleService.class);
    List<Role> sysRoleList = sysRoleApi.findByUserName(loginName);
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    List<String> roleNameList = new ArrayList<String>();
    for (Role sysRole : sysRoleList) {
        roleNameList.add(sysRole.getName());
    }

    ResService sysResService = Jboot.service(ResService.class);
    List<Res> sysResList = sysResService.findByUserNameAndStatusUsed(loginName);
    List<String> urls = new ArrayList<String>();
    for (Res sysRes : sysResList) {
        urls.add(sysRes.getUrl());
    }

    info.addRoles(roleNameList);
    info.addStringPermissions(urls);
    return info;
}
 
Example #19
Source Project: onedev   Author: theonedev   File: AbstractAuthorizingRealm.java    License: MIT License 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	Long userId = (Long) principals.getPrimaryPrincipal();						
	RequestCycle requestCycle = RequestCycle.get();
	if (requestCycle != null) {
		Map<Long, AuthorizationInfo> authorizationInfos = requestCycle.getMetaData(AUTHORIZATION_INFOS);
		if (authorizationInfos == null) {
			authorizationInfos = new HashMap<>();
			requestCycle.setMetaData(AUTHORIZATION_INFOS, authorizationInfos);
		}
		AuthorizationInfo authorizationInfo = authorizationInfos.get(userId);
		if (authorizationInfo == null) {
			authorizationInfo = newAuthorizationInfo(userId);
			authorizationInfos.put(userId, authorizationInfo);
		}
		return authorizationInfo;
	} else {
		return newAuthorizationInfo(userId);
	}
}
 
Example #20
Source Project: SpringAll   Author: wuyouzhuguli   File: ShiroRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 获取用户角色和权限
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
	User user = (User) SecurityUtils.getSubject().getPrincipal();
	String userName = user.getUserName();

	System.out.println("用户" + userName + "获取权限-----ShiroRealm.doGetAuthorizationInfo");
	SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

	// 获取用户角色集
	List<Role> roleList = userRoleMapper.findByUserName(userName);
	Set<String> roleSet = new HashSet<String>();
	for (Role r : roleList) {
		roleSet.add(r.getName());
	}
	simpleAuthorizationInfo.setRoles(roleSet);

	// 获取用户权限集
	List<Permission> permissionList = userPermissionMapper.findByUserName(userName);
	Set<String> permissionSet = new HashSet<String>();
	for (Permission p : permissionList) {
		permissionSet.add(p.getName());
	}
	simpleAuthorizationInfo.setStringPermissions(permissionSet);
	return simpleAuthorizationInfo;
}
 
Example #21
Source Project: mysiteforme   Author: wangl1989   File: AuthRealm.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    ShiroUser shiroUser = (ShiroUser)principalCollection.getPrimaryPrincipal();
    User user = userService.findUserByLoginName(shiroUser.getloginName());
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<Role> roles = user.getRoleLists();
    Set<String> roleNames = Sets.newHashSet();
    for (Role role : roles) {
        if(StringUtils.isNotBlank(role.getName())){
            roleNames.add(role.getName());
        }
    }
    Set<Menu> menus = user.getMenus();
    Set<String> permissions = Sets.newHashSet();
    for (Menu menu : menus) {
        if(StringUtils.isNotBlank(menu.getPermission())){
            permissions.add(menu.getPermission());
        }
    }
    info.setRoles(roleNames);
    info.setStringPermissions(permissions);
    return info;
}
 
Example #22
Source Project: SpringBootBucket   Author: yidao620c   File: MyShiroRealm.java    License: MIT License 6 votes vote down vote up
/**
 * 此方法调用hasRole,hasPermission的时候才会进行回调.
 * <p>
 * 权限信息.(授权):
 * 1、如果用户正常退出,缓存自动清空;
 * 2、如果用户非正常退出,缓存自动清空;
 * 3、如果我们修改了用户的权限,而用户不退出系统,修改的权限无法立即生效。
 * (需要手动编程进行实现;放在service进行调用)
 * 在权限修改后调用realm中的方法,realm已经由spring管理,所以从spring中获取realm实例,调用clearCached方法;
 * :Authorization 是授权访问控制,用于对用户进行的操作授权,证明该用户是否允许进行当前操作,如访问某个链接,某个资源文件等。
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    /*
     * 当没有使用缓存的时候,不断刷新页面的话,这个代码会不断执行,
     * 当其实没有必要每次都重新设置权限信息,所以我们需要放到缓存中进行管理;
     * 当放到缓存中时,这样的话,doGetAuthorizationInfo就只会执行一次了,
     * 缓存过期之后会再次执行。
     */
    _logger.info("权限配置-->MyShiroRealm.doGetAuthorizationInfo()");
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    ManagerInfo managerInfo = (ManagerInfo) principals.getPrimaryPrincipal();

    //设置相应角色的权限信息
    for (SysRole role : managerInfo.getRoles()) {
        //设置角色
        authorizationInfo.addRole(role.getRole());
        for (Permission p : role.getPermissions()) {
            //设置权限
            authorizationInfo.addStringPermission(p.getPermission());
        }
    }

    return authorizationInfo;
}
 
Example #23
Source Project: OneBlog   Author: zhangyd-c   File: ShiroRealm.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 权限认证,为当前登录的Subject授予角色和权限(角色的权限信息集合)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    Long userId = (Long) SecurityUtils.getSubject().getPrincipal();

    // 赋予角色
    List<Role> roleList = roleService.listRolesByUserId(userId);
    for (Role role : roleList) {
        info.addRole(role.getName());
    }

    // 赋予权限
    List<Resources> resourcesList = null;
    User user = userService.getByPrimaryKey(userId);
    if (null == user) {
        return info;
    }
    // ROOT用户默认拥有所有权限
    if (UserTypeEnum.ROOT.toString().equalsIgnoreCase(user.getUserType())) {
        resourcesList = resourcesService.listAll();
    } else {
        resourcesList = resourcesService.listByUserId(userId);
    }

    if (!CollectionUtils.isEmpty(resourcesList)) {
        Set<String> permissionSet = new HashSet<>();
        for (Resources resources : resourcesList) {
            String permission = null;
            if (!StringUtils.isEmpty(permission = resources.getPermission())) {
                permissionSet.addAll(Arrays.asList(permission.trim().split(",")));
            }
        }
        info.setStringPermissions(permissionSet);
    }
    return info;
}
 
Example #24
Source Project: supplierShop   Author: guchengwuyue   File: UserRealm.java    License: MIT License 5 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0)
{
    SysUser user = ShiroUtils.getSysUser();
    // 角色列表
    Set<String> roles = new HashSet<String>();
    // 功能列表
    Set<String> menus = new HashSet<String>();
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    // 管理员拥有所有权限
    if (user.isAdmin())
    {
        info.addRole("admin");
        info.addStringPermission("*:*:*");
    }
    else
    {
        roles = roleService.selectRoleKeys(user.getUserId());
        menus = menuService.selectPermsByUserId(user.getUserId());
        // 角色加入AuthorizationInfo认证对象
        info.setRoles(roles);
        // 权限加入AuthorizationInfo认证对象
        info.setStringPermissions(menus);
    }
    return info;
}
 
Example #25
Source Project: spring-boot-demo   Author: smltq   File: AuthRealm.java    License: MIT License 5 votes vote down vote up
/**
 * 授权
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    log.info("调用授权方法");
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    UserInfo userInfo = (UserInfo) principals.getPrimaryPrincipal();
    for (SysRole role : userInfo.getRoleList()) {
        authorizationInfo.addRole(role.getRole());
        for (SysPermission p : role.getPermissions()) {
            authorizationInfo.addStringPermission(p.getPermission());
        }
    }
    return authorizationInfo;
}
 
Example #26
Source Project: dts-shop   Author: qiguliuxing   File: AdminAuthorizingRealm.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	if (principals == null) {
		throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
	}

	DtsAdmin admin = (DtsAdmin) getAvailablePrincipal(principals);
	Integer[] roleIds = admin.getRoleIds();
	Set<String> roles = roleService.queryByIds(roleIds);
	Set<String> permissions = permissionService.queryByRoleIds(roleIds);
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.setRoles(roles);
	info.setStringPermissions(permissions);
	return info;
}
 
Example #27
Source Project: permission   Author: goufaning   File: MyShiroRealm.java    License: MIT License 5 votes vote down vote up
/**
 * 对用户进行角色授权
 *
 * @param principals 用户信息
 * @return 返回用户授权信息
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    String username = JWTUtil.getUsername(principals.toString());
    SysUser user = userService.findByName(username);
    Set<String> roles = roleService.findRoleByUserId(user.getId());
    Set<String> permissions = menuService.findPermsByUserId(user.getId());
    permissions = permissions.stream().filter(s -> s != null && !s.equals("")).collect(Collectors.toSet());
    authorizationInfo.setRoles(roles);
    authorizationInfo.setStringPermissions(permissions);
    return authorizationInfo;
}
 
Example #28
Source Project: ZTuoExchange_framework   Author: sengeiou   File: AdminRealm.java    License: MIT License 5 votes vote down vote up
/**
 * 授权
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String currentUsername = (String) getAvailablePrincipal(principals);
    log.info("doGetAuthorizationInfo,user:" + currentUsername);
    List<String> permissionList = new ArrayList<>();
    Admin admin = (Admin) getSession(SysConstant.SESSION_ADMIN);
    if (null == admin) {
        throw new AuthorizationException();
    }
    try {
        List<SysPermission> list;
        if ("root".equalsIgnoreCase(admin.getUsername())) {
            list = sysPermissionService.findAll();
        } else {
            SysRole sysRole = sysRoleService.findOne(admin.getRoleId());
            list = sysRole.getPermissions();
        }
        //获取当前用户权限列表
        list.forEach(x -> {
            if (!StringUtils.isEmpty(x.getName())) {
                permissionList.add(x.getName());
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        throw new AuthorizationException();
    }
    log.info("permission list {}", permissionList);
    SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();
    simpleAuthorInfo.addStringPermissions(permissionList);
    return simpleAuthorInfo;
}
 
Example #29
Source Project: web-flash   Author: enilu   File: ApiRealm.java    License: MIT License 5 votes vote down vote up
/**
 * 只有当需要检测用户权限的时候才会调用此方法,例如checkRole,checkPermission之类的
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String username = JwtUtil.getUsername(principals.toString());

    ShiroUser user = shiroFactroy.shiroUser(userService.findByAccount(username));
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.addRoles(user.getRoleCodes());
    Set<String> permission = user.getPermissions();
    simpleAuthorizationInfo.addStringPermissions(permission);
    return simpleAuthorizationInfo;
}
 
Example #30
Source Project: MeetingFilm   Author: daydreamdev   File: ShiroDbRealm.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 权限认证
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    IShiro shiroFactory = ShiroFactroy.me();
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    List<Integer> roleList = shiroUser.getRoleList();

    Set<String> permissionSet = new HashSet<>();
    Set<String> roleNameSet = new HashSet<>();

    for (Integer roleId : roleList) {
        List<String> permissions = shiroFactory.findPermissionsByRoleId(roleId);
        if (permissions != null) {
            for (String permission : permissions) {
                if (ToolUtil.isNotEmpty(permission)) {
                    permissionSet.add(permission);
                }
            }
        }
        String roleName = shiroFactory.findRoleNameByRoleId(roleId);
        roleNameSet.add(roleName);
    }

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addStringPermissions(permissionSet);
    info.addRoles(roleNameSet);
    return info;
}