分布式环境中的Shiro-自动验证/白名单内部调用

分布式环境中的Shiro-自动验证/白名单内部调用,shiro,Shiro,我正在用shiro为RBAC开发一个分布式系统。我没有使用shiro web,但在Servlet上附加了SecurityFilter中的自定义筛选 我的问题是,, 有没有一种方法可以将来自特定节点(在我的例子中是分布式系统中的对等节点)的请求列为白名单(自动认证),而不必经过整个认证过程。当我们有一些代码是由系统而非用户启动时,我们使用一种方法,我们使用系统用户令牌的概念,我们将其耦合到一个拥有所有权限的用户。你可以采取类似的方法。我们有一个自定义领域实现来检查这些情况,因为我们有多个领域,我们

我正在用shiro为RBAC开发一个分布式系统。我没有使用shiro web,但在Servlet上附加了SecurityFilter中的自定义筛选

我的问题是,,
有没有一种方法可以将来自特定节点(在我的例子中是分布式系统中的对等节点)的请求列为白名单(自动认证),而不必经过整个认证过程。

当我们有一些代码是由系统而非用户启动时,我们使用一种方法,我们使用系统用户令牌的概念,我们将其耦合到一个拥有所有权限的用户。你可以采取类似的方法。我们有一个自定义领域实现来检查这些情况,因为我们有多个领域,我们需要用令牌注册它。如果你没有,你可以删除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);

它并不像您希望的那么简单,但对我们来说,它完成了任务。

您所说的自动认证是什么意思。你想让它自动将一个shiro主题分配给当前会话吗?我想说subject.markAuthenticated()而不是sujbect.login()一种机制,在这种机制中,主体被认证,然后我可以将这个主体绑定到当前线程。