当前位置: 首页>>代码示例>>Java>>正文


Java PrincipalCollection.getPrimaryPrincipal方法代码示例

本文整理汇总了Java中org.apache.shiro.subject.PrincipalCollection.getPrimaryPrincipal方法的典型用法代码示例。如果您正苦于以下问题:Java PrincipalCollection.getPrimaryPrincipal方法的具体用法?Java PrincipalCollection.getPrimaryPrincipal怎么用?Java PrincipalCollection.getPrimaryPrincipal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.shiro.subject.PrincipalCollection的用法示例。


在下文中一共展示了PrincipalCollection.getPrimaryPrincipal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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

示例3: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 授权(验证权限时调用)
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();
    Long userId = user.getUserId();

    //用户权限列表
    Set<String> permsSet = shiroService.getUserPermissions(userId);

    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.setStringPermissions(permsSet);
    return info;
}
 
开发者ID:zhaoqicheng,项目名称:renren-fast,代码行数:16,代码来源:OAuth2Realm.java

示例4: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String username = (String) principalCollection.getPrimaryPrincipal();
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    Set<String> roles = new HashSet<String>();
    roles.add("leader");
    Set<String> permissions = new HashSet<String>();
    permissions.add("user:view");
    simpleAuthorizationInfo.setRoles(roles);
    simpleAuthorizationInfo.setStringPermissions(permissions);
    return simpleAuthorizationInfo;
}
 
开发者ID:gittozji,项目名称:home,代码行数:12,代码来源:SimpleRealm.java

示例5: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 授权
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    Integer id = (Integer) principalCollection.getPrimaryPrincipal();
    List<Permission> permissionList = permissionService.loadUserPermission(id);
    // 权限信息对象info,用来存放查出的用户的所有的角色(role)及权限(permission)
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

    permissionList.forEach(p->info.addStringPermission(p.getPerurl()));
    return info;
}
 
开发者ID:ChinaLHR,项目名称:JavaQuarkBBS,代码行数:16,代码来源:MyShiroRealm.java

示例6: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 对用户权限进行判定
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String username = (String) principals.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(adminService.findRoles(username));
    authorizationInfo.setStringPermissions(adminService.findPermissions(username));
    return authorizationInfo;
}
 
开发者ID:melonlee,项目名称:LazyAdmin,代码行数:15,代码来源:UserRealm.java

示例7: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 用户授权-用户有哪些角色、权限
 */
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	logger.info(">>> 【用户授权】principals = {}", principals);
	String userName = (String)principals.getPrimaryPrincipal();
	AdminUser user = getPrincipalService().getPrincipalObject(userName);
	CustomAuthorizationInfo<AdminResource> authorizationInfo = new CustomAuthorizationInfo<AdminResource>();
       authorizationInfo.setRoles(getPrincipalService().getRoles(user));
       authorizationInfo.setStringPermissions(getPrincipalService().getPermissions(user));
       authorizationInfo.setResources(getPrincipalService().getResources(user));
       return authorizationInfo;
}
 
开发者ID:penggle,项目名称:xproject,代码行数:14,代码来源:AdminUserRealm.java

示例8: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
    * 授权(验证权限时调用)
    */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();
	Long userId = user.getUserId();
	
	List<String> permsList = null;
	
	//系统管理员,拥有最高权限
	if(userId == 1){
		List<SysMenuEntity> menuList = sysMenuDao.queryList(new HashMap<String, Object>());
		permsList = new ArrayList<>(menuList.size());
		for(SysMenuEntity menu : menuList){
			permsList.add(menu.getPerms());
		}
	}else{
		permsList = sysUserDao.queryAllPerms(userId);
	}

	//用户权限列表
	Set<String> permsSet = new HashSet<String>();
	for(String perms : permsList){
		if(StringUtils.isBlank(perms)){
			continue;
		}
		permsSet.addAll(Arrays.asList(perms.trim().split(",")));
	}
	
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	info.setStringPermissions(permsSet);
	return info;
}
 
开发者ID:gyp220203,项目名称:renren-msg,代码行数:35,代码来源:UserRealm.java

示例9: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 用户授予权限
 * 
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	String accountNumber = (String) principalCollection.getPrimaryPrincipal();
	User user = userService.getUserByAccountNumber(accountNumber);
	if (user != null) {// 给用户添加角色限制
		info.addRole(user.getRole().toString());
	} else {
		SecurityUtils.getSubject().logout();
	}
	return info;
}
 
开发者ID:MarchMachao,项目名称:ZHFS-WEB,代码行数:19,代码来源:MyRealm.java

示例10: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的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

示例11: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) throws AuthenticationException {
 String email = (String)principals.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(UserRoleDAO.getRoles(email));
    authorizationInfo.setStringPermissions(RolePermissionDAO.getPermission(email));
    return authorizationInfo;
}
 
开发者ID:ETspielberg,项目名称:bibliometrics,代码行数:9,代码来源:BibliometricsRealm.java

示例12: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 授权
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	
	String userName = (String) principals.getPrimaryPrincipal();
	User user = userService.findUserByName(userName);
	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	Set<Role> roleSet = user.getRoles();

	Set<String> permissionSet = new HashSet<String>();
	for (Role role : roleSet) {
		if(StringUtils.isNotBlank(role.getRoleName())){
			info.addRole(role.getRoleName());
			Set<Resource> resources = role.getResources();
			if(resources!=null && !resources.isEmpty()){
				for(Resource r : resources){
					if(StringUtils.isNotBlank(r.getUrl())){
						permissionSet.add(r.getUrl());
					}
				}
			}
		}
	}
	
	info.addStringPermissions(permissionSet);

	return info;
}
 
开发者ID:xujeff,项目名称:tianti,代码行数:31,代码来源:ShiroDBRealm.java

示例13: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * Shiro权限认证
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
        PrincipalCollection principals) {
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.setRoles(shiroUser.getRoles());
    info.addStringPermissions(shiroUser.getUrlSet());
    
    return info;
}
 
开发者ID:TomChen001,项目名称:xmanager,代码行数:15,代码来源:ShiroDbRealm.java

示例14: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 对用户权限进行判定
 *
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    String username = (String) principals.getPrimaryPrincipal();
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.setRoles(userService.findRoles(username));
    authorizationInfo.setStringPermissions(userService.findPermissions(username));
    return authorizationInfo;
}
 
开发者ID:melonlee,项目名称:PowerApi,代码行数:15,代码来源:UserRealm.java

示例15: doGetAuthorizationInfo

import org.apache.shiro.subject.PrincipalCollection; //导入方法依赖的package包/类
/**
 * 授权:验证权限时调用
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    String username = (String) principalCollection.getPrimaryPrincipal();
    UpmsUser upmsUser = upmsApiService.selectUpmsUserByUsername(username);

    // 当前用户所有角色
    List<UpmsRole> upmsRoles = upmsApiService.selectUpmsRoleByUpmsUserId(upmsUser.getUserId());
    Set<String> roles = new HashSet<>();
    for (UpmsRole upmsRole : upmsRoles) {
        if (StringUtils.isNotBlank(upmsRole.getName())) {
            roles.add(upmsRole.getName());
        }
    }

    // 当前用户所有权限
    List<UpmsPermission> upmsPermissions = upmsApiService.selectUpmsPermissionByUpmsUserId(upmsUser.getUserId());
    Set<String> permissions = new HashSet<>();
    for (UpmsPermission upmsPermission : upmsPermissions) {
        if (StringUtils.isNotBlank(upmsPermission.getPermissionValue())) {
            permissions.add(upmsPermission.getPermissionValue());
        }
    }

    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.setStringPermissions(permissions);
    simpleAuthorizationInfo.setRoles(roles);
    return simpleAuthorizationInfo;
}
 
开发者ID:sunzhen086,项目名称:lambo,代码行数:34,代码来源:UpmsRealm.java


注:本文中的org.apache.shiro.subject.PrincipalCollection.getPrimaryPrincipal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。