gpt4 book ai didi

shiro - apache shiro 允许多个角色访问不工作的 url

转载 作者:行者123 更新时间:2023-12-03 14:53:29 27 4
gpt4 key购买 nike

我有一个简单的网络项目。我想在这个项目中访问多个角色是一个 URL。

网址的 sihor.ini 部分

[urls]
/login.xhtml = authc
/logout = logout
/admin/** = user, roles[admin]
/guest/** = user, roles[admin,guest]

当用户管理员角色访问 guest 目录时,我收到 401 错误。

为什么?

四郎版本 1.2.1

最佳答案

还有另一种选择:使用 OR 自定义角色过滤器的实现对于提供的角色集而不是 AND .

import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.RolesAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

/**
* Allows access if current user has at least one role of the specified list.
*
* Basically, it's the same as {@link RolesAuthorizationFilter} but using {@literal OR} instead
* of {@literal AND} on the specified roles.
*
* @see RolesAuthorizationFilter
* @author Andy Belsky
*/
public class AnyOfRolesAuthorizationFilter extends RolesAuthorizationFilter {

@Override
public boolean isAccessAllowed(ServletRequest request, ServletResponse response,
Object mappedValue) throws IOException {

final Subject subject = getSubject(request, response);
final String[] rolesArray = (String[]) mappedValue;

if (rolesArray == null || rolesArray.length == 0) {
//no roles specified, so nothing to check - allow access.
return true;
}

for (String roleName : rolesArray) {
if (subject.hasRole(roleName)) {
return true;
}
}

return false;
}
}
shiro.ini中的用法是这样的:
[main]
...
anyofroles = com.your.package.AnyOfRolesAuthorizationFilter

[urls]
...
/path/to/some/url = anyofroles["role1,role2"]

关于shiro - apache shiro 允许多个角色访问不工作的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14980703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com