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