shiro 如何对非url内容授权

问题描述

应用系统是基于jackrabbit,使用shiro来做授权与认证。在建模时,很多文件操作都建立在 xxx/documents/xxx下。这些文件根据类型对不同角色有不同访问权限,但在url中无法区分。

分析

shiro filter 如下图:
部分相关类]

HttpMethodPermissionFilter类的方法

public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
    String[] perms = (String[]) mappedValue;
    // append the http action to the end of the permissions and then back to super
    String action = getHttpMethodAction(request);
    String[] resolvedPerms = buildPermissions(perms, action);
    return super.isAccessAllowed(request, response, resolvedPerms);
}

这个函数从shiro.ini中取出permission,并将http method(GET,POST…)翻译成action(read,create…),然后判断是否有权限操作。

方案

可以用建立一个HttpMethodPermissionFilter的子类,将fileType取出来,动态构造Permission,之后可以用jdbcRealm来认证。

public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
    String[] perms = (String[]) mappedValue;
    // append the http action to the end of the permissions and then back to super
    String action = getHttpMethodAction(request);
    String fileType = getFileType(request);
    String[] resolvedPerms = buildPermissionsByFileType(perms, action,fileType);
    return super.isAccessAllowed(request, response, resolvedPerms);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值