當前位置: 首頁>>代碼示例>>Java>>正文


Java SimpleAuthorizationInfo.addRoles方法代碼示例

本文整理匯總了Java中org.apache.shiro.authz.SimpleAuthorizationInfo.addRoles方法的典型用法代碼示例。如果您正苦於以下問題:Java SimpleAuthorizationInfo.addRoles方法的具體用法?Java SimpleAuthorizationInfo.addRoles怎麽用?Java SimpleAuthorizationInfo.addRoles使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.shiro.authz.SimpleAuthorizationInfo的用法示例。


在下文中一共展示了SimpleAuthorizationInfo.addRoles方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 完成授權
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
        PrincipalCollection principals) {
    //得到認證成功之後憑證的身份信息
    String username=((User) principals.getPrimaryPrincipal()).getUsername();
    //查詢數據庫得到所有的權限列表
    List<String> permissionList=new ArrayList<String>();
    List<String> roleList=new ArrayList<>();
    UserCustom userCustom = customMapper.findUserCustomByUsername(username);
    Set<RoleCustom> roles=userCustom.getRoleSet();
    for(RoleCustom role:roles){
        roleList.add(role.getRname());
        Set<Permission> permissionSet = role.getPermissionSet();
        for (Permission permission:permissionSet) {
            permissionList.add(permission.getPname());
        }
    }
    SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();
    authorizationInfo.addStringPermissions(permissionList);
    authorizationInfo.addRoles(roleList);
    return authorizationInfo;
}
 
開發者ID:fuyunwang,項目名稱:SpringBootShiro,代碼行數:26,代碼來源:AuthRealm.java

示例2: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
	Preconditions.checkNotNull(principalCollection, "You can't have a null collection of principals. No really, how did you do that");
	String userEmail = (String) getAvailablePrincipal(principalCollection);
	if (userEmail == null) {
		throw new NullPointerException("Can't find a principal in the collection");
	}
	LOGGER.debug("Finding authorization info for " + userEmail + " in DB");

	final User user = userRepository.findByEmailAndActive(userEmail, true);

	LOGGER.debug("Found " + userEmail + " in DB");

	final int totalRoles = user.getRoles().size();
	final Set<String> roleNames = new LinkedHashSet<>(totalRoles);
	final Set<String> permissionNames = new LinkedHashSet<>();
	if (totalRoles > 0) {
		for (Role role : user.getRoles()) {
			roleNames.add(role.getName());
			for (Permission permission : role.getPermissions()) {
				permissionNames.add(permission.getName());
			}
		}
	}

	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.addRoles(roleNames);
	info.addStringPermissions(permissionNames);
	return info;
}
 
開發者ID:auslides,項目名稱:stateless-shiro,代碼行數:31,代碼來源:BearerTokenAuthenticatingRealm.java

示例3: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    checkNotNull(principals.getPrimaryPrincipal());
    checkArgument(principals.getPrimaryPrincipal() instanceof ToAccount);

    ToAccount toAccount = (ToAccount) principals.getPrimaryPrincipal();

    List<ToRole> toRoles = fetchRoles(toAccount.getId());
    List<ToPermission> toPermissions = fetchPermissions(toAccount.getId());

    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.addRoles(
            toRoles.stream()
                    .map(ToRole::getId)
                    .collect(Collectors.toList())
    );

    authorizationInfo.addStringPermissions(
            toPermissions.parallelStream()
                    .map(ToPermission::getId)
                    .collect(Collectors.toList())
    );

    return authorizationInfo;
}
 
開發者ID:srarcbrsent,項目名稱:tc,代碼行數:26,代碼來源:TcAuthorizingRealm.java

示例4: addRoles

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 通過組集合,將集合中的role字段內容解析後添加到SimpleAuthorizationInfo授權信息中
 * 
 * @param info SimpleAuthorizationInfo
 * @param groupsList 組集合
 */
private void addRoles(SimpleAuthorizationInfo info, List<Group> groupsList) {
	
	//解析當前用戶組中的role
       List<String> temp = CollectionUtils.extractToList(groupsList, "role", true);
       List<String> roles = getValue(temp,"roles\\[(.*?)\\]");
      
       //添加默認的roles到roels
       if (CollectionUtils.isNotEmpty(defaultRole)) {
       	CollectionUtils.addAll(roles, defaultRole.iterator());
       }
       
       //將當前用戶擁有的roles設置到SimpleAuthorizationInfo中
       info.addRoles(roles);
	
}
 
開發者ID:extion,項目名稱:base-framework,代碼行數:22,代碼來源:AuthorizationRealm.java

示例5: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String username = (String) principalCollection.getPrimaryPrincipal();
    User user = userRepository.findByUsername(username);
    List<Role> roleList = user.getRoles();
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<String> roleSet = roleList.stream().map(Role::getRoleName).collect(Collectors.toSet());
    info.addRoles(roleSet);
    return info;
}
 
開發者ID:YupaiTS,項目名稱:docs-manage,代碼行數:11,代碼來源:StatelessRealm.java

示例6: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用.
 */
/* (non-Javadoc)
 * @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	System.out.println("開始授權查詢");
	User curUser = userService.findByAccount(principals.getPrimaryPrincipal().toString());
	int userId = curUser.getId();
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	List<Role> rolesList = roleService.selectRolesByUser(userId);
	List<Integer> roleIds = Lists.newArrayList();
	Set<String> roleCodes = Sets.newHashSet();
	if (null != rolesList && rolesList.size() > 0) {
		for (Role role : rolesList) {
			if (!Strings.isNullOrEmpty(role.getCode())) {
				roleIds.add(role.getId());
				roleCodes.add(role.getCode());
			}
		}
		info.addRoles(roleCodes);
	}
	
	// 權限
	Set<String> permissions = Sets.newHashSet();
	if (roleIds.size() > 0) {
		for (Integer roleId: roleIds) {
			List<String> dataList = permissionService.findPermissionsByRoleId(roleId);
			if(null != dataList && dataList.size() > 0) {
				permissions.addAll(dataList);
			}
		}
	}
	permissions.remove("");
	info.addStringPermissions(permissions);
	return info;
}
 
開發者ID:butter-fly,項目名稱:belling-admin,代碼行數:40,代碼來源:ShiroAuthRealm.java

示例7: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	Preconditions.checkNotNull(principals, "You can't have a null collection of principals. No really, how did you do that");
	String userEmail = (String) getAvailablePrincipal(principals);
	if (userEmail == null) {
		throw new NullPointerException("Can't find a principal in the collection");
	}
       LOGGER.debug("Finding authorization info for " + userEmail + " in DB");

	final User user = userRepository.findByEmailAndActive(userEmail, true);

       LOGGER.debug("Found " + userEmail + " in DB");

	final int totalRoles = user.getRoles().size();
	final Set<String> roleNames = new LinkedHashSet<>(totalRoles);
	final Set<String> permissionNames = new LinkedHashSet<>();
	if (totalRoles > 0) {
		for (Role role : user.getRoles()) {
			roleNames.add(role.getName());
			for (Permission permission : role.getPermissions()) {
				permissionNames.add(permission.getName());
			}
		}
	}

	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.addRoles(roleNames);
	info.addStringPermissions(permissionNames);
	return info;
}
 
開發者ID:auslides,項目名稱:stateless-shiro,代碼行數:31,代碼來源:DatabaseRealm.java

示例8: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 授權訪問
 * {@link SimpleAuthorizationInfo SimpleAuthorizationInfo}, as it is suitable in most cases.
 *
 * @param principals the primary identifying principals of the AuthorizationInfo that should be retrieved.
 * @return the AuthorizationInfo associated with this principals.
 * @see SimpleAuthorizationInfo
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //獲取當前登錄的用戶名
    String userName = (String) super.getAvailablePrincipal(principals);

    //保存角色
    List<String> roles = new ArrayList<>();
    //保存權限
    List<String> permissions = new ArrayList<>();
    //拿到當前登陸的用戶
    User user = userService.getById(userName);
    if (user != null) {
        List<Role> userRoleList = user.getRoleList();
        if (!isNullOrEmpty(userRoleList)) {
            for (Role role : userRoleList) {
                roles.add(role.getName());
                List<Permission> rolePermissionList = role.getPermissionList();
                if (isNullOrEmpty(rolePermissionList)) {
                    permissions.addAll(
                            rolePermissionList.stream()
                                    //.filter(permission -> !StringUtils.isEmpty(permission.getPermission()))// permission 不可為空
                                    .map(Permission::getPermission)
                                    .collect(Collectors.toList()
                                    )
                    );
                }
            }
        }
    } else {
        throw new AuthorizationException();
    }
    //給當前用戶設置角色
    info.addRoles(roles);
    //給當前用戶設置權限
    info.addStringPermissions(permissions);
    return info;
}
 
開發者ID:izhangzhihao,項目名稱:SpringMVCSeedProject,代碼行數:47,代碼來源:ShiroRealm.java

示例9: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用.
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
	User user = accountService.findUserByLoginName(shiroUser.loginName);
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.addRoles(user.getRoleList());
	return info;
}
 
開發者ID:XiaoChenYung,項目名稱:YCBugsManager,代碼行數:12,代碼來源:ShiroDbRealm.java

示例10: getUncachedAuthorizationInfoFromPrincipals

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * Gets the authorization info for an API key's principals from the source (not from cache).
 */
private AuthorizationInfo getUncachedAuthorizationInfoFromPrincipals(PrincipalCollection principals) {
    SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();

    for (PrincipalWithRoles principal : getPrincipalsFromPrincipalCollection(principals)) {
        authInfo.addRoles(principal.getRoles());
    }

    return authInfo;
}
 
開發者ID:bazaarvoice,項目名稱:emodb,代碼行數:13,代碼來源:ApiKeyRealm.java

示例11: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * create authorization info using defaultRoles and defaultPermissions
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  SimpleAuthorizationInfo authorization = new SimpleAuthorizationInfo();
  if (!OAuthUtils.isEmpty(defaultRoles))
    authorization.addRoles(split(defaultRoles));
  if (!OAuthUtils.isEmpty(defaultPermissions))
    authorization.addStringPermissions(split(defaultPermissions));
  return authorization;
}
 
開發者ID:hawkxu,項目名稱:shiro-oltu,代碼行數:13,代碼來源:OAuthAuthorizeRealm.java

示例12: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    final SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    final UserAuth auth = Goja.securityUserData.auth((AppUser) principals.getPrimaryPrincipal());
    info.addRoles(auth.getRoles());
    info.addStringPermissions(auth.getPermissions());
    return info;
}
 
開發者ID:GojaFramework,項目名稱:goja,代碼行數:9,代碼來源:AppDbRealm.java

示例13: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 獲取當前認證實體的授權信息(授權包括:角色、權限)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	//獲取當前登錄的用戶名
	ShiroPrincipal subject = (ShiroPrincipal)super.getAvailablePrincipal(principals);
	String username = subject.getUsername();
	Long userId = subject.getId();
	log.info("用戶【" + username + "】授權開始......");
	try {
		if(!subject.isAuthorized()) {
			//根據用戶名稱,獲取該用戶所有的權限列表
			List<String> authorities = userManager.getAuthoritiesName(userId);
			List<String> rolelist = userManager.getRolesName(userId);
			subject.setAuthorities(authorities);
			subject.setRoles(rolelist);
			subject.setAuthorized(true);
			log.info("用戶【" + username + "】授權初始化成功......");
		} else {
			log.info("用戶【" + username + "】已授權......");
		}
	} catch(RuntimeException e) {
		throw new AuthorizationException("用戶【" + username + "】授權失敗");
	}
	log.info("用戶【" + username + "】 角色列表為:" + subject.getRoles());
	log.info("用戶【" + username + "】 權限列表為:" + subject.getAuthorities());
	//給當前用戶設置權限
	info.addStringPermissions(subject.getAuthorities());
	info.addRoles(subject.getRoles());
	return info;
}
 
開發者ID:shaisxx,項目名稱:snaker-demo,代碼行數:34,代碼來源:ShiroAuthorizingRealm.java


注:本文中的org.apache.shiro.authz.SimpleAuthorizationInfo.addRoles方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。