Java Code Examples for org.apache.shiro.authz.SimpleAuthorizationInfo#setRoles()

The following examples show how to use org.apache.shiro.authz.SimpleAuthorizationInfo#setRoles() . 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 File: UserRealm.java    From RuoYi with Apache License 2.0 6 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
    SysUser user = ShiroUtils.getSysUser();

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    // 管理员拥有所有权限
    if (user.isAdmin()) {
        info.addRole("admin");
        info.addStringPermission("*:*:*");
    } else {
        // 角色列表
        Set<String> roles = roleService.selectRoleKeys(user.getUserId());
        // 功能列表
        Set<String> menus = menuService.selectPermsByUserId(user.getUserId());
        // 角色加入AuthorizationInfo认证对象
        info.setRoles(roles);
        // 权限加入AuthorizationInfo认证对象
        info.setStringPermissions(menus);
    }
    return info;
}
 
Example 2
Source File: ShiroRealm.java    From SpringAll with 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 3
Source File: HmacRealm.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	String payload = (String) principals.getPrimaryPrincipal();
	String appId = CommonUtils.hmacPayload(payload);
	if (Objects.isNull(appId))
		return null;
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	Set<String> roles = this.accountProvider.loadRoles(appId);
	Set<String> permissions = this.accountProvider.loadPermissions(appId);
	if (!Collections.isEmpty(roles))
		info.setRoles(roles);
	if (!Collections.isEmpty(permissions))
		info.setStringPermissions(permissions);
	return info;
}
 
Example 4
Source File: LoginRealm.java    From Online_Study_System with Apache License 2.0 6 votes vote down vote up
/**
 * 获取身份信息,从数据库获取该用户的权限和角色信息,当调用权限验证时自动调用。
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String name = (String) getAvailablePrincipal(principalCollection);

    Role role = null;

    try {
        Userlogin userlogin = userloginService.findByName(name);
        //获取角色对象
        role = roleService.findByid(userlogin.getRole());
    } catch (Exception e) {
        e.printStackTrace();
    }
    //通过用户名从数据库获取权限/角色信息
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<String> r = new HashSet<String>();
    if (role != null) {
        r.add(role.getName());
        info.setRoles(r);
    }

    return info;
}
 
Example 5
Source File: JwtRealm.java    From spring-boot-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 授权认证,设置角色/权限信息
 *
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    log.debug("doGetAuthorizationInfo principalCollection...");
    // 设置角色/权限信息
    JwtToken jwtToken = (JwtToken) principalCollection.getPrimaryPrincipal();
    // 获取username
    String username = jwtToken.getUsername();
    // 获取登录用户角色权限信息
    LoginSysUserRedisVo loginSysUserRedisVo = loginRedisService.getLoginSysUserRedisVo(username);
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    // 设置角色
    authorizationInfo.setRoles(SetUtils.hashSet(loginSysUserRedisVo.getRoleCode()));
    // 设置权限
    authorizationInfo.setStringPermissions(loginSysUserRedisVo.getPermissionCodes());
    return authorizationInfo;
}
 
Example 6
Source File: SimpleAuthorizingRealm.java    From NutzSite with 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 7
Source File: MyRealm.java    From shiroDemo with Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    //获取登录时输入的用户名
    String loginName = (String) principalCollection.fromRealm(getName()).iterator().next();
    //到数据库查是否有此对象
    User user = this.getDao().findByName(loginName);
    if (user != null) {
        //权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //用户的角色集合
        info.setRoles(user.getRolesName());
        //用户的角色对应的所有权限,如果只使用角色定义访问权限,下面的四行可以不要
        List<Role> roleList = user.getRoleList();
        for (Role role : roleList) {
            info.addStringPermissions(role.getPermissionsString());
        }
        return info;
    }
    return null;
}
 
Example 8
Source File: LoginRealm.java    From express-ssm with 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 9
Source File: UserRealm.java    From easyweb-shiro with 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 10
Source File: MyRealm.java    From songjhh_blog with Apache License 2.0 5 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String userName=(String)principals.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(userService.getRolesByUserName(userName));
    authorizationInfo.setStringPermissions(userService.getPermissionsByUserName(userName));
    return authorizationInfo;
}
 
Example 11
Source File: UsernameRealm.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

	String account = (String) principals.getPrimaryPrincipal();
	if(Objects.isNull(account)||!Strings.isNullOrEmpty(CommonUtils.jwtPayload(account))
							  ||!Strings.isNullOrEmpty(CommonUtils.hmacPayload(account))) 
		return null;
	SimpleAuthorizationInfo info =  new SimpleAuthorizationInfo();
	Set<String> roles = this.accountProvider.loadRoles(account);
	Set<String> permissions = this.accountProvider.loadPermissions(account);
	if(!Collections.isEmpty(roles)) info.setRoles(roles);
	if(!Collections.isEmpty(permissions)) info.setStringPermissions(permissions);
       return info;  
}
 
Example 12
Source File: UserRealm.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
/**
 * 授权(验证权限时调用) 配置了和会话时间相同的缓存
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	User user = (User) principals.getPrimaryPrincipal();
	String userId = user.getUserId();
	SysUser sysUser = sysUserService.findById(userId);
	if (SysUser.STATUS_STOP.equals(sysUser.getStatus())) {
		throw new ServiceException("用户已被禁用");
	}
	Set<String> permsSet = new HashSet<>();
	Set<String> roleSet = new HashSet<>();
	List<SysMenu> menus = null;
	// 系统管理员,拥有最高权限
	if (ShiroUtils.isSuperAdmin()) {
		menus = sysMenuService.findShowMenuAll();
	} else {
		menus = sysMenuService.findShowMenuByUserId(userId);
	}
	// 用户权限列表
	for (SysMenu menu : menus) {
		if (StringUtils.isNotEmpty(menu.getPermission())) {
			permsSet.addAll(Arrays.asList(StringUtils.split(menu.getPermission().trim(), Constants.SEPARATOR)));
		}
	}
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.setStringPermissions(permsSet);
	List<SysRole> roles = user.getRoles();
	if (roles != null && !roles.isEmpty()) {
		for (SysRole sysRole : roles) {
			roleSet.add(sysRole.getName());
		}
	}
	info.setRoles(roleSet);
	return info;
}
 
Example 13
Source File: AdminAuthorizingRealm.java    From litemall with MIT License 5 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    LitemallAdmin admin = (LitemallAdmin) 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 14
Source File: ShiroRealm.java    From SpringAll with MIT License 5 votes vote down vote up
/**
 * `
 * 授权模块,获取用户角色和权限
 *
 * @param token token
 * @return AuthorizationInfo 权限信息
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection token) {
    String username = JWTUtil.getUsername(token.toString());
    User user = SystemUtils.getUser(username);

    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();

    // 获取用户角色集(模拟值,实际从数据库获取)
    simpleAuthorizationInfo.setRoles(user.getRole());

    // 获取用户权限集(模拟值,实际从数据库获取)
    simpleAuthorizationInfo.setStringPermissions(user.getPermission());
    return simpleAuthorizationInfo;
}
 
Example 15
Source File: UserRealm.java    From supplierShop with 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 16
Source File: AdminAuthorizingRealm.java    From mall with MIT License 5 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }

    LitemallAdmin admin = (LitemallAdmin) 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 17
Source File: UUserRealm.java    From cjs_ssms with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 权限认证
 *
 * @param principals
 * @return AuthorizationInfo
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  String principal_username = principals.getPrimaryPrincipal().toString();
  SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
  Set<String> roleNames = userMService.findRoleNames(principal_username);
  Set<String> permissionNames = userMService.findPermissionNames(roleNames);
  info.setRoles(roleNames);

  //基于权限的授权相比基于角色的授权更好,更灵活,更符合实际情况
  info.setStringPermissions(permissionNames);
  return info;
}
 
Example 18
Source File: MyShiroRealm.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) {
    final String account = (String)principals.getPrimaryPrincipal();
    final User user = this.membershipFacade.getUser(account);

    final SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(this.membershipFacade.getRoleSet(user.getRoles()));
    authorizationInfo.setStringPermissions(this.membershipFacade.getPermissionSet(user.getRoles()));
    return authorizationInfo;
}
 
Example 19
Source File: MyShiroRealm.java    From permission with 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 20
Source File: UserRealm.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0)
{
    User user = ShiroUtils.getSysUser();
    // 角色列表
    Set<String> roles;
    // 功能列表
    Set<String> menus;
    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;
}