2

我正在使用 shiro 和骆驼。我可以使用 activedirectory (ldap) 对用户进行身份验证,但我无法将角色中的用户组映射到用户权限。骆驼需要绝对的权限才能工作。

这是我的 config.ini:

[main]
authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy
securityManager.authenticator.authenticationStrategy = $authcStrategy


activeDirectoryRealm = org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm
activeDirectoryRealm.systemUsername=cn=padl,cn=Users,dc=comune,dc=prato,dc=local
activeDirectoryRealm.systemPassword=xxxxxxxxxxxxxxxxxxxxxxx
activeDirectoryRealm.url = ldap://172.16.1.98:389
activeDirectoryRealm.groupRolesMap = "CN=menu_ufficiomobile_ania,OU=Menu,OU=Gruppi,OU=ComuneDiPrato,DC=comune,DC=prato,DC=local":"menu_ufficiomobile_ania"

securityManager.realms = $activeDirectoryRealm
[users]

test = test,menu_ufficiomobile_passicarrabili, menu_ufficiomobile_rubati,menu_ufficiomobile_ordinanze, menu_ufficiomobile_ztl, menu_ufficiomobile_cciaa,  menu_ufficiomobile_ania, menu_ufficiomobile_anagrafe, menu_ufficiomobile_mctc, menu_ufficiomobile_pra


[roles]

menu_ufficiomobile_anagrafe = prato:anagrafe
menu_ufficiomobile_mctc = prato:mctc
menu_ufficiomobile_pra = prato:pra
menu_ufficiomobile_ania = prato:ania
menu_ufficiomobile_cciaa = prato:cacomm
menu_ufficiomobile_ztl = prato:ztl
menu_ufficiomobile_ordinanze = prato:ordinanze
menu_ufficiomobile_rubati = prato:rubati
menu_ufficiomobile_passicarrabili = prato:permessi

我的安全注入代码:

ShiroSecurityToken shiroSecurityToken = new ShiroSecurityToken(qr.getUserName(),qr.getPassword());
ShiroSecurityTokenInjector shiroSecurityTokenInjector = new ShiroSecurityTokenInjector(shiroSecurityToken, passPhrase);
arg0.getIn().setHeader("SHIRO_SECURITY_TOKEN", shiroSecurityTokenInjector.encrypt());

以及使用权限的路由代码:

from("seda:interrogaANIA").threads(1)
 .setHeader("db", constant(Database.ANIA)).policy(aniaS)
 .to("bean:interrogaANIA?method=interrogaBancaDati")
 .to("seda:prefilter"); 

谢谢,马里奥

4

1 回答 1

6

因此,shiro 中的领域提供了 3 件事:

  1. 验证用户
  2. 将用户映射到角色
  3. 将用户映射到权限

它不提供任意角色->权限映射。为此,您需要在活动目录领域设置 RolePermissionResolver。查看 IniRealm(由 ini 文件中的 [users] 和 [roles] 部分创建的内容),似乎没有一种简单的方法可以将其用作 RolePermissionResolver。我能够将一个应该可以很好地工作的适配器类组合在一起。

package org.apache.shiro.samples.web;

import java.util.Collection;
import java.util.Collections;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.SimpleRole;
import org.apache.shiro.authz.permission.RolePermissionResolver;
import org.apache.shiro.config.Ini;
import org.apache.shiro.realm.text.IniRealm;

public class IniRealmRolePermissionResolver implements RolePermissionResolver {
        private LocalIniRealm realm;

        public Collection<Permission> resolvePermissionsInRole(final String roleString) {
                final SimpleRole role = this.realm.getRole(roleString);
                return role == null ? Collections.<Permission>emptySet() : role.getPermissions();
        }

        public void setIni(final IniRealm ini) {
                this.realm = new LocalIniRealm();
                this.realm.setIni(ini.getIni());
                this.realm.init();
        }

        private static class LocalIniRealm extends IniRealm {
                @Override
                protected SimpleRole getRole(final String rolename) {
                        return super
                                        .getRole(rolename);
                }
        }
}

这应该可以与您的 ini 文件的此添加一起使用:

rolePermissionResolver = org.apache.shiro.samples.web.IniRealmRolePermissionResolver
rolePermissionResolver.ini = $iniRealm
activeDirectoryRealm.rolePermissionResolver = $rolePermissionResolver

如果您真的不需要 ini 映射,您可以使用相同的概念,但会大大简化事情。

package org.apache.shiro.samples.web;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.authz.permission.*;

import java.util.Collection;
import java.util.Collections;

public class SimpleRolePermissionResolver implements RolePermissionResolver, PermissionResolverAware {

    private PermissionResolver permissionResolver = new WildcardPermissionResolver();

    public void setPermissionResolver(PermissionResolver permissionResolver) {
        this.permissionResolver = permissionResolver;
    }

    public Collection<Permission> resolvePermissionsInRole(String roleString) {
        return Collections.<Permission>singleton(permissionResolver.resolvePermission(roleString));
    }
}

然后您的 ini 配置也发生了一些变化:

rolePermissionResolver = org.apache.shiro.samples.web.SimpleRolePermissionResolver
activeDirectoryRealm.rolePermissionResolver = $rolePermissionResolver
于 2012-05-21T23:44:59.033 回答