首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >挂毯覆盖身份验证器

挂毯覆盖身份验证器
EN

Stack Overflow用户
提问于 2020-10-27 15:06:44
回答 2查看 94关注 0票数 0

我正在尝试使用一个用于tapestry安全性(org.tynamo.security)的自定义身份验证器。

我有一个自定义认证器

代码语言:javascript
复制
public class EnvironmentalRealmAuthenticator extends ModularRealmAuthenticator

在我的模块中,我重写了Tapestry (ModularRealmAuthenticator)的默认身份验证器:

代码语言:javascript
复制
public static void bind(final ServiceBinder binder) {

    binder.bind(Authenticator.class, EnvironmentalRealmAuthenticator.class).withId("EnvironmentalRealmAuthenticator");
}

@Contribute(ServiceOverride.class)
public static void setupOverrides(final MappedConfiguration<Class, Object> configuration, @Local final Authenticator override) {
    configuration.add(Authenticator.class, override);
}

但是,这会导致缓存在注销时不被清除--我怀疑这是由Shiro的DefaultSecurityManager检测身份验证者是否监听注销引起的:

代码语言:javascript
复制
Authenticator authc = getAuthenticator();
if (authc instanceof LogoutAware) {
    ((LogoutAware) authc).onLogout(principals);
}

由于EnvironmentalRealmAuthenticator被绑定为Tapestry服务,它最初作为代理注入,因此authc instanceof LogoutAware生成false --这就是为什么在Tynamo的SecurityModule中以不同的方式绑定默认ModularRealmAuthenticator的原因:

代码语言:javascript
复制
// TYNAMO-155 It's not enough to identify ModularRealmAuthenticator by it's Authenticator interface only
// because Shiro tests if the object is an instanceof LogoutAware to call logout handlers
binder.bind(ModularRealmAuthenticator.class);

但是,当我试图以这种方式覆盖我的EnvironmentalRealmAuthenticator

代码语言:javascript
复制
binder.bind(EnvironmentalRealmAuthenticator.class).withId("EnvironmentalRealmAuthenticator");

这导致以下例外情况:

由: java.lang.IllegalStateException:构造服务'ServiceOverride‘导致递归失败:服务在某种程度上依赖于自身。请通过org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31)通过org.apache.tapestry5.ioc.modules.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:52)查看对其他服务的引用,该服务本身依赖于服务“ServiceOverride”.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-30 13:39:16

我似乎找到了一种(相当讨厌的)方法。我没有覆盖Authenticator本身,而是重写了WebSecuritymanager

代码语言:javascript
复制
public static void bind(final ServiceBinder binder) {
    binder.bind(EnvironmentalRealmAuthenticator.class).withId("EnvironmentalRealmAuthenticator");
    binder.bind(WebSecurityManager.class, EnvironmentalSecurityManager.class).withId("EnvironmentalSecurityManager");
}

@Contribute(ServiceOverride.class)
public static void setupOverrides(final MappedConfiguration<Class, Object> configuration, @Local final WebSecurityManager override) {
    configuration.add(WebSecurityManager.class, override);
}

这样,我就不必将EnvironmentalRealmAuthenticator与其接口绑定。为了能够识别新的Authenticator,我对模块进行了注释:

代码语言:javascript
复制
@Marker(Primary.class)

然后,EnvironmentalSecurityManager的植入如下所示:

代码语言:javascript
复制
/**
 * Used to properly (and uniquely) identify the authenticator (without having to override it)
 */
public class EnvironmentalSecurityManager extends TapestryRealmSecurityManager {

    private final Logger logger = LoggerFactory.getLogger(EnvironmentalSecurityManager.class);

    /**
     * Mind the @Primary annotation, used to identify the EnvironmentalRealmAuthenticator
     */
    public EnvironmentalSecurityManager(final @Primary Authenticator authenticator, final SubjectFactory subjectFactory, final RememberMeManager rememberMeManager, final Collection<Realm> realms) {

        super(authenticator, subjectFactory, rememberMeManager, realms);
        logger.debug("Created EnvironmentalSecurityManager - class of authenticator is {}", authenticator.getClass());
    }
}

这样,我就可以保证使用正确的Authenticator,而不必实际覆盖它。

票数 0
EN

Stack Overflow用户

发布于 2020-10-29 08:08:45

如果不看到导致该异常的setupOverrides方法的最终版本,我就无法确定。

但是,你试过这个吗?

代码语言:javascript
复制
public static void bind(final ServiceBinder binder) {

    binder.bind(EnvironmentalRealmAuthenticator.class);
}

@Contribute(ServiceOverride.class)
public static void setupOverrides(final MappedConfiguration<Class, Object> configuration, EnvironmentalRealmAuthenticator override) {
    configuration.add(Authenticator.class, override);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64557263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档