Apache shiro + HTTP 方法级权限
Apache shiro + HTTP method level permission
我正在开发一个基于 REST 的 Web 应用程序,其中的休息服务集成了 Apache shiro 以执行基本身份验证和基于角色的授权。
现在我想通过方法级别的权限配置来增强授权功能(在 REST 的情况下是微服务)。如果我没记错的话,Apache shiro 提供了 HttpMethodPermissionFilter class,它可以用作过滤器来根据其内部检查的 HTTP 方法(GET、POST、DELETE、HEAD 等...)限制传入请求从我们配置的数据库的roles_permissions table或者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 的新手,所以任何参考都将不胜感激。
谢谢
查看 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]
我正在开发一个基于 REST 的 Web 应用程序,其中的休息服务集成了 Apache shiro 以执行基本身份验证和基于角色的授权。
现在我想通过方法级别的权限配置来增强授权功能(在 REST 的情况下是微服务)。如果我没记错的话,Apache shiro 提供了 HttpMethodPermissionFilter class,它可以用作过滤器来根据其内部检查的 HTTP 方法(GET、POST、DELETE、HEAD 等...)限制传入请求从我们配置的数据库的roles_permissions table或者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 的新手,所以任何参考都将不胜感激。 谢谢
查看 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]