本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}