【发布时间】: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