Shiro在分布式环境中 - 自动验证/白名单内部呼叫

时间:2014-07-03 23:05:01

标签: shiro

我正在为RBAC开发一个带shiro的分布式系统。我没有使用shiro-web,但在附加到Servlet的SecurityFilter中进行了自定义过滤。

我的问题是, 有没有办法对来自某些节点(在我的情况下,分布式系统中的对等体)的请求进行白名单(自动验证),而无需经历整个身份验证过程。

1 个答案:

答案 0 :(得分:0)

我们使用一种方法,当我们有一些由系统而不是用户启动的代码时,我们使用系统用户令牌的想法,我们将其耦合到拥有所有权限的用户。你可以采取类似的方法。我们有一个自定义领域实现来检查这些情况,因为我们有多个领域,我们需要使用令牌注册它。如果你没有,你可以删除realmName的东西。

public class SystemAuthenticationToken implements AuthenticationToken {

    private String realmName;

    public SystemAuthenticationToken(String realmName) {
        this.realmName = realmName;
    }

    public String getRealmName() {
        return realmName;
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public Object getCredentials() {
        return null;
    }
}

在我们的自定义域实现中:

public class OurRealmImpl extends AuthorizingRealm {


    @Override
    public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
        if (token instanceof SystemAuthenticationToken) {
            login = //..get the special login
        } else {
            login = //..get normal login
        }
        SimplePrincipalCollection principalCollection = new SimplePrincipalCollection(login, realmName);
        if (token instanceof SystemAuthenticationToken) {
            principalCollection.add(token, realmName);
        }

       return new SimpleAuthenticationInfo(principalCollection, login.getPasswordHash());
    }

我们扩展了您需要在安全管理器上设置的密码匹配器:

public class PasswordAndSystemCredentialsMatcher extends PasswordMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        if (token instanceof SystemAuthenticationToken){
            return true;
        }
        return super.doCredentialsMatch(token, info);
    }

然后安全过滤器会调用登录来设置令牌:

    Subject currentUser = SecurityUtils.getSubject();
    SystemAuthenticationToken token = new SystemAuthenticationToken(realmName);
    currentUser.login(token);

它是不是像你希望的那样简单,但对我们来说它完成了工作。