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


Java PrincipalCollection類代碼示例

本文整理匯總了Java中org.apache.shiro.subject.PrincipalCollection的典型用法代碼示例。如果您正苦於以下問題:Java PrincipalCollection類的具體用法?Java PrincipalCollection怎麽用?Java PrincipalCollection使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


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

示例1: doGetAuthorizationInfo

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

示例2: doGetAuthorizationInfo

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

示例3: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
/**
 * 此方法調用  hasRole,hasPermission的時候才會進行回調.
 *
 * 權限信息.(授權):
 * 1、如果用戶正常退出,緩存自動清空;
 * 2、如果用戶非正常退出,緩存自動清空;
 * 3、如果我們修改了用戶的權限,而用戶不退出係統,修改的權限無法立即生效。
 * (需要手動編程進行實現;放在service進行調用)
 * 在權限修改後調用realm中的方法,realm已經由spring管理,所以從spring中獲取realm實例,
 * 調用clearCached方法;
 * :Authorization 是授權訪問控製,用於對用戶進行的操作授權,證明該用戶是否允許進行當前操作,如訪問某個鏈接,某個資源文件等。
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) throws AuthenticationException{
   /*
    * 當沒有使用緩存的時候,不斷刷新頁麵的話,這個代碼會不斷執行,
    * 當其實沒有必要每次都重新設置權限信息,所以我們需要放到緩存中進行管理;
    * 當放到緩存中時,這樣的話,doGetAuthorizationInfo就隻會執行一次了,
    * 緩存過期之後會再次執行。
    */
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    AdminDO userInfo  = (AdminDO)principals.getPrimaryPrincipal();
    Set<String> set = new HashSet<String>();
    set.add(userInfo.getRole().getRoleId());
    authorizationInfo.setRoles(set);

    Set<String> menus = roleMenuService.getMenuCodesByRoleId(userInfo.getRole().getRoleId());
    authorizationInfo.addStringPermissions(menus);

    return authorizationInfo;
}
 
開發者ID:wu05281,項目名稱:admin-shiro,代碼行數:34,代碼來源:AdminShiroRealm.java

示例4: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	Long userId = (Long) WebUtil.getCurrentUser();
	Parameter parameter = new Parameter("sysAuthorizeService", "queryPermissionByUserId").setId(userId);
	logger.info("{} execute queryPermissionByUserId start...", parameter.getNo());
	List<?> list = provider.execute(parameter).getList();
	logger.info("{} execute queryPermissionByUserId end.", parameter.getNo());
	for (Object permission : list) {
		if (StringUtils.isNotBlank((String) permission)) {
			// 添加基於Permission的權限信息
			info.addStringPermission((String) permission);
		}
	}
	// 添加用戶權限
	info.addStringPermission("user");
	return info;
}
 
開發者ID:youngMen1,項目名稱:JAVA-,代碼行數:18,代碼來源:Realm.java

示例5: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	String username = (String) principals.getPrimaryPrincipal();
	SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
	// 根據用戶名查詢當前用戶擁有的角色
	List<Map> roles = userService.findRolesByUserName(username);
	Set<String> roleNames = new HashSet<String>();
	for (Map role : roles) {
		roleNames.add(role.get("name").toString());
	}
	// 將角色名稱提供給info
	authorizationInfo.setRoles(roleNames);
	// 根據用戶名查詢當前用戶權限
	List<Map> permissions = userService.findPermissionsByUserName(username);
	Set<String> permissionNames = new HashSet<String>();
	for (Map permission : permissions) {
		permissionNames.add(permission.get("name").toString());
	}
	// 將權限名稱提供給info
	authorizationInfo.setStringPermissions(permissionNames);

	return authorizationInfo;
}
 
開發者ID:phoenix-varus,項目名稱:jeeWe,代碼行數:24,代碼來源:UserRealm.java

示例6: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
/**
 * 暫時先不做權限,隻做有沒有登錄
 * */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    /*
    *  User user=(User) principal.fromRealm(this.getClass().getName()).iterator().next();//獲取session中的用戶
    List<String> permissions=new ArrayList<>();
    Set<Role> roles = user.getRoles();
    if(roles.size()>0) {
        for(Role role : roles) {
            Set<Module> modules = role.getModules();
            if(modules.size()>0) {
                for(Module module : modules) {
                    permissions.add(module.getMname());
                }
            }
        }
    }
    SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
    info.addStringPermissions(permissions);//將權限放入shiro中.
    return info;
    * */
    return null;
}
 
開發者ID:zpshowhan,項目名稱:SpringBootLayIM,代碼行數:26,代碼來源:AuthRealm.java

示例7: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
/**
 * 授權查詢回調函數, 進行鑒權但緩存中無用戶的授權信息時調用.
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
		PrincipalCollection principalCollection) {
	if (principalCollection == null) {
		throw new AuthorizationException("Principal is not null!");
	}
	Shiro shiro = (Shiro) principalCollection.getPrimaryPrincipal();
	User entity = new User();
	entity.setId(shiro.getId());
	entity = (User) service.iUserService.select(entity);
	if (null == entity) {
		throw new UnknownAccountException("No account found for user ["
				+ shiro.getId() + "]");
	}
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	return info;
}
 
開發者ID:jiangzongyao,項目名稱:kettle_support_kettle8.0,代碼行數:21,代碼來源:Authorizing2Realm.java

示例8: getPrincipals

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
@Override
public PrincipalCollection getPrincipals() {
    RealmSecurityManager manager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
    SimplePrincipalCollection ret = new SimplePrincipalCollection();
    for (Realm realm : manager.getRealms()) {
        /*
        if (realm instanceof ProfileRealm) {
            String email = token.getEmail();
            if (((ProfileRealm) realm).accountExists(email)) {
                ret.add(email, realm.getName());
            }
        }
        */
    }
    ret.add(token.getEmail(), bearerTokenAuthenticatingRealm.getName());
    return ret;
}
 
開發者ID:auslides,項目名稱:stateless-shiro,代碼行數:18,代碼來源:BearerAuthenticationInfo.java

示例9: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	// 配置用戶權限
	Principal principal = (Principal) principals.fromRealm(getName()).iterator().next();
	if (principal != null) {
		// 推薦將權限標識加入緩存,然後從緩存獲取權限標識集合,並添加給SimpleAuthorizationInfo,避免頻繁訪問數據庫獲取資源
		// 如:List<String> permissions = redisService.findUserPermissions(userId);
		// 權限標識與applicationContext-shiro.xml配置的filterChainDefinitionsperms中類似["admin:userModule"]
		// 以下示例
		List<String> permissions = new ArrayList<String>();
		permissions.add("admin:userModule");// 賦予這個標識才有權限訪問/user/findPage.html
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		authorizationInfo.addStringPermissions(permissions);
		return authorizationInfo;

	}
	return null;
}
 
開發者ID:wenjian-li,項目名稱:spring_mybatis_shiro,代碼行數:19,代碼來源:UserAuthorizingRealm.java

示例10: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的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

示例11: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的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

示例12: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的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

示例13: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的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

示例14: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的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

示例15: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //導入依賴的package包/類
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	return new AuthorizationInfo() {
		
		private static final long serialVersionUID = 1L;

		@Override
		public Collection<String> getStringPermissions() {
			return new HashSet<>();
		}
		
		@Override
		public Collection<String> getRoles() {
			return new HashSet<>();
		}
		
		@Override
		public Collection<Permission> getObjectPermissions() {
			return getObjectPermissionsInSession((Long) principals.getPrimaryPrincipal());
		}
	};
}
 
開發者ID:jmfgdev,項目名稱:gitplex-mit,代碼行數:23,代碼來源:GitPlexAuthorizingRealm.java


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