Search in sources :

Example 6 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project nutzboot by nutzam.

the class SimpleAuthorizingRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    // null usernames are invalid
    if (principals == null) {
        throw new AuthorizationException("PrincipalCollection method argument cannot be null.");
    }
    long userId = ((Number) principals.getPrimaryPrincipal()).longValue();
    User user = dao().fetch(User.class, userId);
    if (user == null)
        return null;
    SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
    auth.addRole(user.getName());
    auth.addStringPermission("user:list");
    return auth;
}
Also used : User(io.nutz.demo.simple.bean.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AuthorizationException(org.apache.shiro.authz.AuthorizationException)

Example 7 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project knox by apache.

the class KnoxLdapRealm method queryForAuthorizationInfo.

/**
 * Get groups from LDAP.
 *
 * @param principals
 *            the principals of the Subject whose AuthenticationInfo should
 *            be queried from the LDAP server.
 * @param ldapContextFactory
 *            factory used to retrieve LDAP connections.
 * @return an {@link AuthorizationInfo} instance containing information
 *         retrieved from the LDAP server.
 * @throws NamingException
 *             if any LDAP errors occur during the search.
 */
@Override
protected AuthorizationInfo queryForAuthorizationInfo(final PrincipalCollection principals, final LdapContextFactory ldapContextFactory) throws NamingException {
    if (!isAuthorizationEnabled()) {
        return null;
    }
    final Set<String> roleNames = getRoles(principals, ldapContextFactory);
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(roleNames);
    Set<String> stringPermissions = permsFor(roleNames);
    simpleAuthorizationInfo.setStringPermissions(stringPermissions);
    return simpleAuthorizationInfo;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo)

Example 8 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project Ganster-CMS by Gangster-trio.

the class UserShiroRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    User user = (User) principals.getPrimaryPrincipal();
    List<Group> groupList = groupService.selectByUserId(user.getUserId());
    Set<String> groupSet = groupList.stream().map(Group::getGroupName).collect(Collectors.toSet());
    Set<String> permissionSet = groupSet.stream().flatMap(group -> permissionService.selectByGroupName(group).stream().map(Permission::getPermissionName)).collect(Collectors.toSet());
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.setStringPermissions(permissionSet);
    simpleAuthorizationInfo.setRoles(groupSet);
    return simpleAuthorizationInfo;
}
Also used : PermissionService(com.ganster.cms.core.service.PermissionService) Logger(org.slf4j.Logger) org.apache.shiro.authc(org.apache.shiro.authc) AuthorizationInfo(org.apache.shiro.authz.AuthorizationInfo) LoggerFactory(org.slf4j.LoggerFactory) Resource(javax.annotation.Resource) Permission(com.ganster.cms.core.pojo.Permission) Set(java.util.Set) Collectors(java.util.stream.Collectors) UserService(com.ganster.cms.core.service.UserService) List(java.util.List) Subject(org.apache.shiro.subject.Subject) UserExample(com.ganster.cms.core.pojo.UserExample) User(com.ganster.cms.core.pojo.User) AuthorizingRealm(org.apache.shiro.realm.AuthorizingRealm) Group(com.ganster.cms.core.pojo.Group) GroupService(com.ganster.cms.core.service.GroupService) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) SecurityUtils(org.apache.shiro.SecurityUtils) Group(com.ganster.cms.core.pojo.Group) User(com.ganster.cms.core.pojo.User) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) Permission(com.ganster.cms.core.pojo.Permission)

Example 9 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project Ganster-CMS by Gangster-trio.

the class UserShiroRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    logger.info("进入权限配置");
    String username = (String) principals.getPrimaryPrincipal();
    UserExample userExample = new UserExample();
    userExample.createCriteria().andUserNameEqualTo(username);
    List<User> users = userService.selectByExample(userExample);
    Integer j = 0;
    for (User i : users) {
        userId = i.getUserId();
        j++;
    }
    if (j >= 2) {
        return null;
    }
    User user = userService.selectByPrimaryKey(userId);
    List<Group> groupList = groupService.selectByUserId(user.getUserId());
    Set<String> groupSet = new HashSet<>();
    for (Group i : groupList) {
        if (!StringUtil.isNullOrEmpty(user.getUserName())) {
            groupSet.add(i.getGroupName());
        }
    }
    Set<String> permissionSet = new HashSet<>();
    for (Group i : groupList) {
        if (!StringUtil.isNullOrEmpty(i.getGroupName())) {
            try {
                List<Permission> permissions = permissionService.selectByGroupId(i.getGroupId());
                for (Permission permission : permissions) {
                    permissionSet.add(permission.getPermissionName());
                }
            } catch (GroupNotFountException e) {
                logger.info("角色未找到");
            }
        }
    }
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    simpleAuthorizationInfo.setStringPermissions(permissionSet);
    simpleAuthorizationInfo.setRoles(groupSet);
    return simpleAuthorizationInfo;
}
Also used : Group(com.ganster.cms.core.pojo.Group) User(com.ganster.cms.core.pojo.User) GroupNotFountException(com.ganster.cms.core.exception.GroupNotFountException) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) UserExample(com.ganster.cms.core.pojo.UserExample) Permission(com.ganster.cms.core.pojo.Permission)

Example 10 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project vip by guangdada.

the class ShiroDbRealm method doGetAuthorizationInfo.

/**
 * 权限认证
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    IShiro shiroFactory = ShiroFactroy.me();
    ShiroUser shiroUser = (ShiroUser) principals.getPrimaryPrincipal();
    List<Integer> roleList = shiroUser.getRoleList();
    Set<String> permissionSet = new HashSet<>();
    Set<String> roleNameSet = new HashSet<>();
    for (Integer roleId : roleList) {
        List<String> permissions = shiroFactory.findPermissionsByRoleId(roleId);
        if (permissions != null) {
            for (String permission : permissions) {
                if (ToolUtil.isNotEmpty(permission)) {
                    permissionSet.add(permission);
                }
            }
        }
        String roleName = shiroFactory.findRoleNameByRoleId(roleId);
        roleNameSet.add(roleName);
    }
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    info.addStringPermissions(permissionSet);
    info.addRoles(roleNameSet);
    return info;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) IShiro(com.ikoori.vip.server.core.shiro.factory.IShiro) HashSet(java.util.HashSet)

Aggregations

SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)48 Permission (org.apache.shiro.authz.Permission)8 AuthorizationException (org.apache.shiro.authz.AuthorizationException)6 KeyValueCollectionPermission (ddf.security.permission.KeyValueCollectionPermission)5 KeyValuePermission (ddf.security.permission.KeyValuePermission)5 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 AuthenticationException (org.apache.shiro.authc.AuthenticationException)5 AuthorizationInfo (org.apache.shiro.authz.AuthorizationInfo)5 KeyValuePermissionImpl (ddf.security.permission.impl.KeyValuePermissionImpl)4 CollectionPermission (ddf.security.permission.CollectionPermission)3 HashMap (java.util.HashMap)3 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)3 Group (com.ganster.cms.core.pojo.Group)2 Permission (com.ganster.cms.core.pojo.Permission)2 User (com.ganster.cms.core.pojo.User)2 UserExample (com.ganster.cms.core.pojo.UserExample)2 TbRolePermission (com.netsteadfast.greenstep.po.hbm.TbRolePermission)2 TbUserRole (com.netsteadfast.greenstep.po.hbm.TbUserRole)2 Set (java.util.Set)2