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


Java SimpleAuthorizationInfo.addRole方法代碼示例

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


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

示例1: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
		PrincipalCollection principals) {
	String userName = (String) principals.fromRealm(getName()).iterator()
			.next();
	List<WizardRoleInfo> roleInfos = securityService.getRoleInfo(userName);
	List<WizardOrgInfo> permissionInfos = null;
	if (null == roleInfos || 0 == roleInfos.size()) {
		return null;
	} else {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		for (WizardRoleInfo roleInfo : roleInfos) {
			info.addRole(roleInfo.getRoleName());
			permissionInfos = securityService.getPermissionInfo(roleInfo
					.getRoleName());
			for (WizardOrgInfo permissionInfo : permissionInfos) {
				info.addStringPermission(permissionInfo.getOrgName());
			}
		}
		return info;
	}

}
 
開發者ID:joaquinaimar,項目名稱:wizard,代碼行數:24,代碼來源:WizardWebRealm.java

示例2: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 獲取當前用戶的角色集合,權限集合
 */
@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;
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:25,代碼來源:UserRealm.java

示例3: doGetAuthorizationInfo

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

    final String username = getUsername(principals);

    List<Roles> roles = usersRepository.findRolesByUsername(username);
    LOG.debug("Load Roles[{}] by username: {}", roles, username);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    for (Roles role : roles) {
        info.addRole(role.roleName());
        for (RolesPermissions permissions : role.permissions()) {
            info.addStringPermission(permissions.permission());
        }
    }

    return info;
}
 
開發者ID:monkeyk,項目名稱:oauth2-shiro-redis,代碼行數:20,代碼來源:RedisRealm.java

示例4: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
//@org.springframework.transaction.annotation.Transactional
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    //獲取當前用戶
    //UserDto user = convertToDto(userDao.findUserByUsername((String)principalCollection.getPrimaryPrincipal()));
    //User currentUser = userDao.findUserByUsername((String)principalCollection.getPrimaryPrincipal());
    UserDto user = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("user");

    //把principals放session中,key=userId value=principals
    SecurityUtils.getSubject().getSession().setAttribute(String.valueOf(user.getId()),SecurityUtils.getSubject().getPrincipals());

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    //賦予角色
    for(RoleDto role:user.getRoles()){
        info.addRole(role.getName());
    }
    //賦予權限
    for(PermissionDto permission:user.getPermissions()){
        //System.out.println(permission.getName());
        info.addStringPermission(permission.getName());
    }
    return info;
}
 
開發者ID:ZhuXS,項目名稱:Spring-Shiro-Spark,代碼行數:24,代碼來源:ShiroRealm.java

示例5: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
        PrincipalCollection principals) {
    //XXX 在這裏獲取到用戶,並獲取到相關角色和權限,在這裏定義一個基礎用戶
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    String username = String.valueOf(principals.getPrimaryPrincipal());//取查詢獲取用戶名
    //獲取當前用戶
    //final IUserModel user =mscUsersPersonService.findByUserName(username);// userService.selectByUsername(username);
    //權限列表
    final List<IRoleDomain> roleInfoList =CurrentUserUtil.getPermissionList();//mscAuthRoleService.findByUser(user.getUserId());
    if(roleInfoList!=null && !roleInfoList.isEmpty()){
        for (IRoleDomain role : roleInfoList) {
            // 添加權限角色
            String authRole= AuthConstant.getRole(role.getType());
            authorizationInfo.addRole(authRole);
            authorizationInfo.addStringPermission(authRole);//將擁有的角色權限直接添加到權限中
            final List<IAuthDomain> mscAuthInfoList =role.getAuthList();// permissionService.selectPermissionsByRoleId(role.getId());
            for (IAuthDomain authInfo : mscAuthInfoList) {
                // 添加權限
                authorizationInfo.addStringPermission(authInfo.getAuthKey());//(permission.getPermissionSign());
            }
        }
    }
    return authorizationInfo;

}
 
開發者ID:haizicq,項目名稱:osframe,代碼行數:27,代碼來源:MscLoginRealm.java

示例6: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
protected AuthorizationInfo doGetAuthorizationInfo(
		PrincipalCollection principals) {
	Principal principal = (Principal) getAvailablePrincipal(principals);
	if (principal.getName() != null) {
		UserService userService = BGDispatch.userService;
		Collection<Role> myRoles = userService.getRoles(principal.getName(),principal.getPassword());
		if (myRoles != null) {
			SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
			for (Role each : myRoles) {
				info.addRole(each.getStr(Role.NAME));
				Set<String> pers = new HashSet<String>();
				for (Permission permission : each.getPermissions()) {
					if(StringUtils.isNotBlank(permission.getStr("permission"))){
					pers.add(permission.getStr("permission"));
					}
				}
				info.addStringPermissions(pers);
			}

			return info;
		}
	}
	return null;
}
 
開發者ID:yaokwok,項目名稱:loveabc,代碼行數:25,代碼來源:MyShiroRealm.java

示例7: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	Principal principal = (Principal) getAvailablePrincipal(principals);

	User user = getSystemService().getUserByLoginName(principal.getLoginName());
	if (user != null) {
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

		info.addStringPermission("sys:test:add");
		info.addStringPermission("sys:test:edit");
		// 添加用戶權限
		info.addStringPermission("user");
		info.addRole("role1");
		info.addRole("role2");
		return info;
	} else {
		return null;
	}
}
 
開發者ID:whatlookingfor,項目名稱:spring-boot-sample,代碼行數:23,代碼來源:SystemAuthorizingRealm.java

示例8: getSimpleAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
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;		
}
 
開發者ID:billchen198318,項目名稱:bamboobsc,代碼行數:23,代碼來源:GreenStepBaseAuthorizingLdapRealm.java

示例9: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@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;
}
 
開發者ID:sonatype,項目名稱:nexus-public,代碼行數:18,代碼來源:MockRealmB.java

示例10: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用.
 */
@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;
}
 
開發者ID:wu560130911,項目名稱:MultimediaDesktop,代碼行數:23,代碼來源:UserRealm.java

示例11: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/**
 * AuthorizationInfo(Subject's roles and permissions)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
       String username = (String) principalCollection.fromRealm(getName()).iterator().next(); 
       User loginUser = userService.findByUsername(username);
       if (loginUser != null) {  
           SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();  
           for (Role role : loginUser.getRoles()) { 
           	String roleName = String.valueOf(role.getId()); 
           	authInfo.addRole(roleName);
           	authInfo.addStringPermissions(role.getStringPermissions());  
           }  
           return authInfo; 
       }
	return null;
}
 
開發者ID:guhao,項目名稱:zo-spring-boot,代碼行數:19,代碼來源:ShiroDatabaseRealm.java

示例12: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    // null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }
    Integer userId = (Integer) getAvailablePrincipal(principals);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    Set<Role> roles = userRepository.getRoles(userId);
    for (Role role : roles) {
        info.addRole(role.getName());
    }
    List<PostPermission> permissions = userRepository.getPermissions(userId);

    for (PostPermission permission : permissions) {
        if (permission.getMask() > 0) {
            info.addStringPermission(permission.getLiteral());
        }
    }
    return info;
}
 
開發者ID:antoniomaria,項目名稱:shiro-jpa,代碼行數:23,代碼來源:JPARealm.java

示例13: doGetAuthorizationInfo

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

    // Get the userId from the first principal in the collection
    String userIdentifier = (String) getAvailablePrincipal(principals);

    // Lookup user
    Identity identity = identityService().findByUserId(userIdentifier);
    assertUserFound(identity);

    // Add roles to AuthorizationInfo
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addRole(Role.USER.name());
    if (identity.username().equalsIgnoreCase("admin")) {
        info.addRole(Role.ADMIN.name());
    }
    //user.userRoles().all().stream().forEach((role) -> {
    //    info.addRole(role.name());
    //});

    return info;
}
 
開發者ID:dma-graveyard,項目名稱:MaritimeCloudPortalTestbed,代碼行數:23,代碼來源:MaritimeCloudIdentityRealm.java

示例14: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) {
    try {
        String principal = (String) principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo sai = new SimpleAuthorizationInfo();
        Set<String> perms = getPerms(principal);
        if (perms != null) {
            for (String perm : perms) {
                sai.addStringPermission(perm);
            }
        }
        Set<String> roles = getRoles(principal);
        if (roles != null) {
            for (String role : roles) {
                sai.addRole(role);
            }
        }
        return sai;
    } catch (GwtUtilException e) {
        log.warn("Exception getting authorization info: {}", e);
    }
    return null;
}
 
開發者ID:rkfg,項目名稱:gwtutil,代碼行數:24,代碼來源:GwtUtilRealm.java

示例15: doGetAuthorizationInfo

import org.apache.shiro.authz.SimpleAuthorizationInfo; //導入方法依賴的package包/類
/** {@inheritDoc} */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principalCollection) {

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

    // Find the thing that stores your user's roles.
    final UserProfile userProfile = lookup(UserManagementService.class).findUserByLogin(username);
    if (userProfile == null) {
        logger.info("Principal not found for authorizing user with username: {}", username);
        return null;
    } else {
        final UserRole role = userProfile.getRole();
        logger.info("Authoriziong user {} with role: {}", username, role);
        final SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRole(role.getName());
        grantPermissions(userProfile, authorizationInfo);
        return authorizationInfo;
    }
}
 
開發者ID:ExtaSoft,項目名稱:extacrm,代碼行數:21,代碼來源:UserRealm.java


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