org.apache.shiro.authz.SimpleAuthorizationInfo Java Examples

The following examples show how to use org.apache.shiro.authz.SimpleAuthorizationInfo. 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: JPARealm.java    From gazpachoquest with GNU General Public License v3.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) getAvailablePrincipal(principals);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<Role> roles = userService.getRoles(user.getId());
    for (Role role : roles) {
        info.addRole(role.getName());
    }
    Set<Permission<?>> permissions = userService.getPermissions(user.getId());

    for (Permission<?> permission : permissions) {
        info.addStringPermission(permission.getLiteral());
    }
    return info;
}
 
Example #2
Source File: UserRealm.java    From spring-boot-study with MIT License 6 votes vote down vote up
/**
 * 重写授权 doGetAuthorizationInfo 返回  授权信息对象 AuthorizationInfo
 * @param  principalCollection 身份信息
 * @return  返回  授权信息对象 AuthorizationInfo
 * */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    UserDO userDO  = (UserDO)principalCollection.getPrimaryPrincipal();
    Integer userId= userDO.getId();//转成 user 对象
    //授权 新建一个授权模块 SimpleAuthorizationInfo 把 权限赋值给当前的用户
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    //设置当前会话拥有的角色 实际场景根据业务来如从数据库获取角色列表
    Set<String> roles=new HashSet<>();
    roles.add("admin");
    roles.add("finance");
    info.setRoles(roles);

    //设置当前会话可以拥有的权限 实际场景根据业务来如从数据库获取角色列表下的权限列表
    Set<String> permissions=new HashSet<>();
    permissions.add("system:article:article");
    permissions.add("system:article:add");
    permissions.add("system:article:edit");
    permissions.add("system:article:remove");
    permissions.add("system:article:batchRemove");
    info.setStringPermissions(permissions);
    return  info;
}
 
Example #3
Source File: LdapRealm.java    From Moss with 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 #4
Source File: DBRealm.java    From Moss with 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 #5
Source File: GreenStepBaseAuthorizingRealm.java    From bamboobsc with Apache License 2.0 6 votes vote down vote up
private SimpleAuthorizationInfo getSimpleAuthorizationInfo(String username) throws Exception {
	Map<String, Object> params = new HashMap<String, Object>();
	params.put("account", username);
	List<TbUserRole> roleList = userRoleService.findListByParams(params);
	if (roleList==null) {
		return null;
	}
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	for (TbUserRole userRole : roleList) {
		info.addRole(userRole.getRole());
		params.clear();
		params.put("role", userRole.getRole());
		List<TbRolePermission> rolePermissionList = rolePermissionService.findListByParams(params);
		if (rolePermissionList==null) {
			continue;
		}
		for (TbRolePermission rolePermission : rolePermissionList) {
			info.addStringPermission(rolePermission.getPermission());
		}
	}
	return info;		
}
 
Example #6
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 #7
Source File: AuthRealm.java    From mysiteforme with 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 #8
Source File: FreeRealm.java    From SENS with 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 #9
Source File: NormalRealm.java    From SENS with 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 #10
Source File: MyShiroRealm.java    From spring-boot-shiro with 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 #11
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 #12
Source File: ShiroDbRealm.java    From Mario with Apache License 2.0 6 votes vote down vote up
/**
 * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    User user = accountService.findUserByLoginName(shiroUser.loginName);
    List<Role> userRoles = accountService.getRoleByUserID(user.getId());//用户ID对应的角色列表信息

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    for (Role role : userRoles) {
        // 基于Role的权限信息
        info.addRole(role.getName());
        // 基于Permission的权限信息
        //TODO:add permission value info 
        //info.addStringPermissions(role.getPermissionList());
    }
    return info;
}
 
Example #13
Source File: MyShiroRealm.java    From SpringBootBucket with 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 #14
Source File: UserRealm.java    From ssm with Apache License 2.0 6 votes vote down vote up
@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SysUser sysUser =  (SysUser)principals.getPrimaryPrincipal();
		List<SysPermission> sysPermissions = sysPermissionService.getPermissionsByUserAccount(sysUser.getAccount());
		List<String> permissionValus = new ArrayList<String>();
		if (sysPermissions != null) {
//			System.out.println(sysPermissions.size());
			for (SysPermission sysPermission : sysPermissions) {
				permissionValus.add(sysPermission.getValue());
//				System.out.println(sysPermission.toString());
			}
		}
		SimpleAuthorizationInfo simpleAuthorizationInfo
				= new SimpleAuthorizationInfo();
		simpleAuthorizationInfo.addStringPermissions(permissionValus);
		return simpleAuthorizationInfo;
	}
 
Example #15
Source File: ExampleLDAPRealm.java    From airpal with Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals)
{
    Set<String> roles = Sets.newHashSet("user");
    Set<Permission> permissions = Sets.newHashSet();

    Collection<AllowAllUser> principalsCollection = principals.byType(AllowAllUser.class);

    if (principalsCollection.isEmpty()) {
        throw new AuthorizationException("No principals!");
    }

    for (AllowAllUser user : principalsCollection) {
        for (UserGroup userGroup : groups) {
            if (userGroup.representedByGroupStrings(user.getGroups())) {
                permissions.addAll(userGroup.getPermissions());
                break;
            }
        }
    }

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(roles);
    authorizationInfo.setObjectPermissions(permissions);

    return authorizationInfo;
}
 
Example #16
Source File: MyShiroRealm.java    From DouBiNovel with 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 #17
Source File: LdapRealm.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
/**
* Get groups from LDAP.
*
* @param principals
*            the principals of the Subject whose AuthenticationInfo should
*            be queried from the LDAP server.
* @param ldapContextFactory
*            factory used to retrieve LDAP connections.
* @return an {@link AuthorizationInfo} instance containing information
*         retrieved from the LDAP server.
* @throws NamingException
*             if any LDAP errors occur during the search.
*/
@Override
public AuthorizationInfo queryForAuthorizationInfo(final PrincipalCollection principals,
    final LdapContextFactory ldapContextFactory) throws NamingException {
  if (!isAuthorizationEnabled()) {
    return null;
  }
  final Set<String> roleNames = getRoles(principals, ldapContextFactory);
  if (log.isDebugEnabled()) {
    log.debug("RolesNames Authorization: " + roleNames);
  }
  SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(roleNames);
  Set<String> stringPermissions = permsFor(roleNames);
  simpleAuthorizationInfo.setStringPermissions(stringPermissions);
  return simpleAuthorizationInfo;
}
 
Example #18
Source File: MockRealm.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  String userId = principals.getPrimaryPrincipal().toString();

  Set<String> roles = new HashSet<String>();
  try {
    for (RoleIdentifier roleIdentifier : userManager.getUser(userId).getRoles()) {
      roles.add(roleIdentifier.getRoleId());
    }
  }
  catch (UserNotFoundException e) {
    return null;
  }

  return new SimpleAuthorizationInfo(roles);
}
 
Example #19
Source File: KnoxPamRealm.java    From knox with Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  Set<String> roles = new LinkedHashSet<>();

  UnixUserPrincipal user = principals.oneByType(UnixUserPrincipal.class);
  if (user != null) {
    roles.addAll(user.getUnixUser().getGroups());
  }
  SecurityUtils.getSubject().getSession().setAttribute(SUBJECT_USER_ROLES, roles);
  SecurityUtils.getSubject().getSession().setAttribute(SUBJECT_USER_GROUPS, roles);

  /* Coverity Scan CID 1361682 */
  String userName = null;

  if (user != null) {
    userName = user.getName();
  }

  gatewayLog.lookedUpUserRoles(roles, userName);
  return new SimpleAuthorizationInfo(roles);
}
 
Example #20
Source File: UserRealm.java    From mumu with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前用户的角色集合,权限集合
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
	//获取保存在session中的用户信息
	SysUser user = (SysUser) SecurityUtils.getSubject().getSession().getAttribute(SysUser.SYS_USER);
	if (user == null) {
		throw new IllegalArgumentException();
	}
	//获取当前用户拥有的所有角色
	List<SysRole> roles = roleService.getSysRoleByUserId(user.getUserId().toString(), PublicEnum.NORMAL.value());
	for (SysRole sysRole : roles) {
		authorizationInfo.addRole(sysRole.getRoleCode());
	}
	//获取当前用户拥有的所有权限
	List<SysPermission> permissions = permissionService.getSysPermissionByUserId(user.getUserId(), PublicEnum.NORMAL.value());
	for (SysPermission sysPermission : permissions) {
		authorizationInfo.addStringPermission(sysPermission.getPermission());
	}
	System.out.println("用户权限:"+ JSON.toJSONString(authorizationInfo));
	return authorizationInfo;
}
 
Example #21
Source File: UserRealm.java    From MultimediaDesktop with Apache License 2.0 6 votes vote down vote up
/**
 * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
		PrincipalCollection principals) {
	ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
	UserRole role = shiroUser.role;
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	
	info.addRole(UserRole.用户.getRole());
	
	if(UserRole.开发者.equals(role)){
		info.addRole(UserRole.开发者.getRole());
	}
	
	if(UserRole.管理员.equals(role)){
		info.addRole(UserRole.开发者.getRole());
		info.addRole(UserRole.管理员.getRole());
	}
	return info;
}
 
Example #22
Source File: JpaRealm.java    From init-spring with Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals)
{
	String username = principals.getPrimaryPrincipal().toString();
	User user = this.jpaRealmRepository.findUserByName(username);

	if (null != user)
	{
		SimpleAuthorizationInfo authorization = new SimpleAuthorizationInfo();
		for (Role role : user.getRoles())
		{
			authorization.addStringPermissions(role.getPermissions());
		}
		return authorization;
	}

	return null;
}
 
Example #23
Source File: AllowAllRealm.java    From airpal with Apache License 2.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals)
{
    Set<String> roles = Sets.newHashSet("user");
    Set<Permission> permissions = Sets.newHashSet();
    Collection<AllowAllUser> principalsCollection = principals.byType(AllowAllUser.class);

    if (principalsCollection.isEmpty()) {
        throw new AuthorizationException("No principals!");
    }

    for (AllowAllUser user : principalsCollection) {
        for (UserGroup userGroup : groups) {
            if (userGroup.representedByGroupStrings(user.getGroups())) {
                permissions.addAll(userGroup.getPermissions());
                break;
            }
        }
    }

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(roles);
    authorizationInfo.setObjectPermissions(permissions);

    return authorizationInfo;
}
 
Example #24
Source File: LoginAuth.java    From jboot-admin with 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 #25
Source File: ShiroRealm.java    From blog-sample with 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 #26
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 #27
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 #28
Source File: UserRealm.java    From ShiroJwt with 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 #29
Source File: DbRealm.java    From dpCms with Apache License 2.0 6 votes vote down vote up
@Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            final PrincipalCollection principals) {
        // retrieve role names and permission names
        final String userName = (String) principals.getPrimaryPrincipal();
        final Account account = accountRepository.findByLoginName(userName);
        if (account == null) {
            throw new UnknownAccountException("Account does not exist");
        }
        //先保存岗位数量
        final int totalRoles = account.getEmployees().size();
        
        final Set<String> roleNames = new LinkedHashSet<>(totalRoles);
        final Set<String> permissionNames = new LinkedHashSet<>();

        final SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.setStringPermissions(permissionNames);
        return info;
    }
 
Example #30
Source File: MockRealmB.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  // make sure the user is jcool, (its just for testing)

  if (principals.asList().get(0).toString().equals("jcool")) {
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    info.addRole("test-role1");
    info.addRole("test-role2");

    info.addStringPermission("test:*");

    return info;
  }

  return null;
}