当前位置: 首页 > 知识库问答 >
问题:

如何在Shiro中注册身份验证监听器

吴高畅
2023-03-14

登录,注销-一切正常,但我不知道如何注册一个AuthentiationListener来记录我的用户。

我使用的是Guice,并且我使用自己的dbsaltwarerealm(公共类dbsaltarerealm扩展了授权领域)

感谢

共有2个答案

闾丘卓
2023-03-14

下面是我如何修改shiro项目中SamplesGuice中的SampleShiroServletModule:

public class SampleShiroServletModule extends ShiroWebModule {

    @Override
protected void configureShiroWeb() {
    ....

    final Multibinder<AuthenticationListener> listenerMultibinder = Multibinder.newSetBinder(binder(), AuthenticationListener.class);
    listenerMultibinder.addBinding().to(MyAuthenticationListener.class);

}

@Override
protected void bindWebSecurityManager(final AnnotatedBindingBuilder<? super WebSecurityManager> bind) {
    bind.to(DefaultWebSecurityManager.class);
}

@Provides
DefaultWebSecurityManager provideDefaultSecurityManager(final Collection<Realm> realms, final Set<AuthenticationListener> authenticationListeners) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(realms);
    ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
    authenticator.setAuthenticationListeners(authenticationListeners);
    securityManager.setAuthenticator(authenticator);
    return securityManager;
}

}

左丘楷
2023-03-14

好吧,我找到了另一个解决方案:

1.像往常一样初始化您的Guice模块:

public class ConfigServeletWithGuice extends GuiceServletContextListener {
    ...

    @Override
    protected Injector getInjector() {
        final Injector injector = Guice.createInjector(new ServeletModule(), new BusinessLogicModule(),
                new AuthenticationModule(_ctx), new ShiroAopModule(), ShiroWebModule.guiceFilterModule());

        final SecurityManager securityManager = injector.getInstance(SecurityManager.class);
        SecurityUtils.setSecurityManager(securityManager);

        logger.debug("Creation of Injector - done!");

        return injector;
    }
}

2. 创建您自己的安全管理器:

public class MyWebSecurityManager extends DefaultWebSecurityManager {
    @SuppressWarnings("unused")
    private static Logger logger = LoggerFactory.getLogger(MyWebSecurityManager.class.getSimpleName());

    @Inject
    private AuthenticationListener authenticationListener;

    public MyWebSecurityManager() {
        super();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public MyWebSecurityManager(Realm singleRealm) {
        this();
        setRealm(singleRealm);
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public MyWebSecurityManager(Collection realms) {
        this();
        setRealms(realms);
    }

    @Override
    protected void onSuccessfulLogin(final AuthenticationToken token, final AuthenticationInfo info, final Subject subject) {
        super.onSuccessfulLogin(token, info, subject);

        logger.debug("onSuccessfulLogin");
        authenticationListener.onSuccess(token, info);
    }

    @Override
    protected void onFailedLogin(final AuthenticationToken token, final AuthenticationException ae, final Subject subject) {
        super.onFailedLogin(token, ae, subject);

        logger.debug("onFailedLogin");
        authenticationListener.onFailure(token,ae);
    }

    @Override
    protected void beforeLogout(final Subject subject) {
        super.beforeLogout(subject);

        logger.debug("beforeLogout");
        authenticationListener.onLogout(subject.getPrincipals());
    }
}

3.最后,绑定您自己的SecurityManager:

public class AuthenticationModule extends ShiroWebModule {
    static Logger logger = LoggerFactory.getLogger(AuthenticationModule.class.getSimpleName());

    public AuthenticationModule(final ServletContext sc) {
        super(sc);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void configureShiroWeb() {
        logger.debug("Start to configure ShiroWeb...");

        bind(AuthenticationListener.class).to(AuthenticationListenerImpl.class);
        ...
        logger.debug("configuration ShiroWeb - done!");
    }

    @Override
    // !!!!!! Here it comes:
    protected void bindWebSecurityManager(final AnnotatedBindingBuilder bind) {
        try {
            bind.toConstructor(MyWebSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();
        } catch (NoSuchMethodException e) {
            throw new ConfigurationException("This really shouldn't happen.  Either something has changed in Shiro, or there's a bug in ShiroModule.", e);
        }
    }
}

现在,重新启动后,您应该会看到您的日志消息!

 类似资料:
  • 我一直在寻找使用各种AWS服务来处理我们下一个主要项目的基础设施。我们开始研究EC2实例上的docker容器,但在进一步研究AWS Lambda之后,这似乎是一条值得探索的道路。 使用AWS Lambda范例,我们只需使用Lambda函数作为逻辑粘合剂,将数据和事件(来自其他AWS服务)保存在一起。 例如,如果我们产品的用户创建了一个新记录,AWS Lambda可以在该事件中触发,我们可以调用La

  • 我正在开发一个Web应用程序,它是rest客户端并使用rest服务(API)来执行任何操作。我集成了apache shiro以使用工作正常的jdbc领域执行身份验证。 现在,我正在寻找一种使用apacheDS LDAP执行身份验证的解决方案。我计划使用kerberos身份验证,但我没有找到任何有用的文章或示例来使用apache shiro实现kerberos身份验证。 我发现apache shir

  • null /signup=>在我的数据库中注册用户,颁发令牌 /登录 /refresh_token null null null 我想要的是客户机V1的一致和兼容API。APIV1的任何现有客户端(应用程序)都应该能够向V1/Signupendpoint发送请求,并以与V2客户端相同的状态完成:在Firebase中创建用户帐户

  • 我是Apache Shiro的初学者。我一直在跟踪文档和许多其他教程,博客等,但我就是无法使身份验证工作。当我尝试使用有效的用户名和密码登录时,总是会引发。我正在使用DynamoDB作为存储用户凭据的自定义领域,但我真的不认为这有什么关系。显然,我存储和/或执行凭据匹配的方式是不正确的。以下是我的设置:

  • 我有一个已经完成的j2ee(jsf、cdi、jpa)应用程序,它完美地使用了ApacheShiro,它工作得非常好,我喜欢Shiro注释(hasRole、hasPermission等)。 现在,该项目还必须能够通过SiteMinder进行身份验证,我的问题是: 我如何设置一个领域来处理SiteMinder身份验证而不丢失Shiro授权(似乎SiteMinder会在HTTP头中给我用户名和Rolen

  • 大家好,我对FireBase创建用户和注册用户有问题。下面是注册和登录的代码。我得到空指针异常,无法理解原因。。初始化完成了,但经过了这么多天的努力,还是弄不明白。 注册: 正在发布注册活动的日志错误: java.lang.com.google.android.gms.internal.zzdvv.zzb(未知来源)在com.google.android.gms.internal.zzdwc.zz