Search in sources :

Example 21 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project pmph by BCSquad.

the class PmphUserRealm method doGetAuthorizationInfo.

/**
 * 授权
 *
 * @param principalCollection
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    logger.info("--- MyRealm doGetAuthorizationInfo ---");
    // 获得经过认证的主体信息
    PmphUser user = (PmphUser) principalCollection.getPrimaryPrincipal();
    Long userId = user.getId();
    // UserService userService = (UserService)InitServlet.getBean("userService");
    List<PmphPermission> resourceList = null;
    List<String> roleSnList = null;
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    try {
        resourceList = userService.getListAllResource(userId);
        roleSnList = userService.getListRoleSnByUser(userId);
        List<String> resStrList = new ArrayList<>();
        for (PmphPermission resource : resourceList) {
            resStrList.add(resource.getUrl());
        }
        info.setRoles(new HashSet<>(roleSnList));
        info.setStringPermissions(new HashSet<>(resStrList));
        // 以上完成了动态地对用户授权
        logger.info("role => " + roleSnList);
        logger.info("permission => " + resStrList);
    } catch (Exception e) {
        logger.info("message => " + e);
    }
    return info;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) PmphUser(com.bc.pmpheep.back.po.PmphUser) PmphPermission(com.bc.pmpheep.back.po.PmphPermission) ArrayList(java.util.ArrayList) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 22 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project dq-easy-cloud by dq-open-cloud.

the class EcAuthorityRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    EcAuthorityUserDTO<Long> authorityUserDTO = (EcAuthorityUserDTO) principals.getPrimaryPrincipal();
    List<SysRoleDTO> roleList = sysRoleService.findByUserId(authorityUserDTO.getAuthorityUserId());
    Set<Integer> roleNos = new HashSet<>();
    for (SysRoleDTO role : roleList) {
        authorizationInfo.addRole(role.getName());
        roleNos.add(role.getRoleNo());
    }
    List<SysResourceDTO> roleResourceDTOs = sysResourceService.findByRoleNos(new ArrayList<>(roleNos));
    for (SysResourceDTO resourceDTO : roleResourceDTOs) {
        authorizationInfo.addStringPermission(resourceDTO.getPermission());
    }
    return authorizationInfo;
}
Also used : EcAuthorityUserDTO(com.easy.cloud.core.authority.pojo.dto.EcAuthorityUserDTO) SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) SysRoleDTO(com.easy.cloud.core.operator.sysrole.pojo.dto.SysRoleDTO) SysResourceDTO(com.easy.cloud.core.operator.sysresource.pojo.dto.SysResourceDTO)

Example 23 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project production_ssm by megagao.

the class CustomRealm method doGetAuthorizationInfo.

/**
 * realm的授权方法
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    // 从 principals获取主身份信息
    // 将getPrimaryPrincipal方法返回值转为真实身份类型(在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型),
    ActiveUser activeUser = (ActiveUser) principals.getPrimaryPrincipal();
    // 根据身份信息从数据库获取到权限数据
    List<SysPermission> permissionList = null;
    try {
        permissionList = sysService.findPermissionListByUserId(activeUser.getUserid());
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    List<String> permissions = new ArrayList<String>();
    if (permissionList != null) {
        for (SysPermission sysPermission : permissionList) {
            permissions.add(sysPermission.getPercode());
        }
    }
    // 查到权限数据,返回授权信息(要包括 上边的permissions)
    SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
    // 将上边查询到授权信息填充到simpleAuthorizationInfo对象中
    simpleAuthorizationInfo.addStringPermissions(permissions);
    return simpleAuthorizationInfo;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) ActiveUser(com.megagao.production.ssm.domain.customize.ActiveUser) ArrayList(java.util.ArrayList) SysPermission(com.megagao.production.ssm.domain.authority.SysPermission) AuthenticationException(org.apache.shiro.authc.AuthenticationException)

Example 24 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project bamboobsc by billchen198318.

the class GreenStepBaseAuthorizingRealm method getSimpleAuthorizationInfo.

private SimpleAuthorizationInfo getSimpleAuthorizationInfo(String username) throws Exception {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("account", username);
    List<TbUserRole> roleList = userRoleService.findListByParams(params);
    if (roleList == null) {
        return null;
    }
    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    for (TbUserRole userRole : roleList) {
        info.addRole(userRole.getRole());
        params.clear();
        params.put("role", userRole.getRole());
        List<TbRolePermission> rolePermissionList = rolePermissionService.findListByParams(params);
        if (rolePermissionList == null) {
            continue;
        }
        for (TbRolePermission rolePermission : rolePermissionList) {
            info.addStringPermission(rolePermission.getPermission());
        }
    }
    return info;
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) HashMap(java.util.HashMap) TbRolePermission(com.netsteadfast.greenstep.po.hbm.TbRolePermission) TbUserRole(com.netsteadfast.greenstep.po.hbm.TbUserRole)

Example 25 with SimpleAuthorizationInfo

use of org.apache.shiro.authz.SimpleAuthorizationInfo in project perry by ca-cwds.

the class AbstractRealm method doGetAuthorizationInfo.

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    List principalsList = principals.asList();
    if (principalsList.size() == PRINCIPALS_COUNT) {
        PerryAccount perryAccount = (PerryAccount) principalsList.get(1);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        SecurityModule.getStaticAuthorizers().forEach(staticAuthorizer -> staticAuthorizer.authorize(perryAccount, authorizationInfo));
        return authorizationInfo;
    }
    throw new AuthenticationException("User authorization failed!");
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) ArrayList(java.util.ArrayList) List(java.util.List)

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