當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。