【问题标题】:Apache shiro + HTTP method level permissionApache shiro + HTTP 方法级权限
【发布时间】:2017-06-14 14:34:39
【问题描述】:

我正在开发一个基于 REST 的 Web 应用程序,其中集成了 Apache shiro 的 REST 服务来执行基本身份验证和基于角色的授权。

现在我想通过方法级别的权限配置来增强授权功能(在 REST 的情况下为微服务)。如果我没记错的话,Apache shiro 提供了 HttpMethodPermissionFilter 类,该类可用作过滤器,以根据其 HTTP 方法(GET、POST、DELETE、HEAD 等)限制传入请求,该方法在内部检查角色权限表中的权限我们配置的数据库或INI配置文件。

所以要实现基于 HTTP 方法的权限功能,我是否需要在我的 shiro.ini 文件中进行任何更改。或者我的jdbc领域有事。

shiro.ini 文件

[main]
userRoles = org.apache.shiro.web.filter.authz.RolesAuthorizationFilter

jdbcRealm = my.custom.jdbc.realm.YhJdbcRealm
securityManager.realms = $jdbcRealm

[urls]
# Allowing login page to any user
/rest/login/** = anon

# Page 1
/rest/page1/** = noSessionCreation, authcBasic, userRoles[role1]


# page 2
/rest/page2/** = noSessionCreation, authcBasic, userRoles[role1,role2,role3]


# page 3
/yhrest/page3/** = noSessionCreation, authcBasic, userRoles[role1,role3]

/rest/** = noSessionCreation, authcBasic

自定义 jdbc 领域

public class YhJdbcRealm extends JdbcRealm
{
    public YhJdbcRealm()
    {
        loadDataSource();
    }

    private void loadDataSource()
    {
        this.dataSource = JdbcConnection.initConnection();
        this.permissionsLookupEnabled = true;
        this.authenticationQuery = "SELECT password FROM users WHERE username = ?";
        this.userRolesQuery = "SELECT role_name FROM user_roles WHERE username = ?";
        this.permissionsQuery = "SELECT permission FROM roles_permissions_temp WHERE role_name = ?";
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
    {
        AuthenticationInfo info = super.doGetAuthenticationInfo(token);
        return info;
    }
}

我是 apache shiro 的新手,所以任何参考都将不胜感激。 谢谢。

【问题讨论】:

    标签: java apache rest permissions shiro


    【解决方案1】:

    查看HttpMethodPermissionFilter 的文档,假设您对/rest/page1 具有以下CRUD 权限:

    • abc:create
    • abc:read
    • abc:update
    • abc:delete

    您的[urls] 映射如下所示:

    [urls]
    /rest/page1/** = noSessionCreation, authcBasic, rest[abc]
    

    /rest/page1/** 的所有GET 请求都将映射到权限rest[abc:read]

    【讨论】:

    • 所以你的意思是“abc”是这里的一个用户,它将拥有 page1 的所有 CRUD 的权限。太好了,以哪种格式或如何存储在数据库中?你能分享一下我需要为特定页面执行实际权限检查的架构和值吗?
    • 是的 'abc' 将是基本用户权限,我打算使用 page1,但希望将它们分开以进行解释。架构取决于您,但对于您的示例,permission 列将权限映射到角色。请查看以下链接以获取更多信息:Blog on Shiro PermissionsShiro Permissions
    猜你喜欢
    • 2015-01-24
    • 2016-09-28
    • 2011-07-12
    • 2013-04-26
    • 2013-10-13
    • 2017-07-13
    • 2013-02-26
    • 2017-11-27
    • 2021-07-30
    相关资源
    最近更新 更多