英文:
Tapestry override Authenticator
问题
我正在尝试为 tapestry-security (org.tynamo.security) 使用自定义的认证器。
我有一个自定义的认证器:
public class EnvironmentalRealmAuthenticator extends ModularRealmAuthenticator
在我的模块中,我覆盖了 Tapestry 的默认认证器 (ModularRealmAuthenticator
):
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
检测认证器是否监听注销的方式引起的:
Authenticator authc = getAuthenticator();
if (authc instanceof LogoutAware) {
((LogoutAware) authc).onLogout(principals);
}
由于 EnvironmentalRealmAuthenticator
被绑定为 Tapestry 服务,它最初被注入为代理,因此 authc instanceof LogoutAware
得到 false
- 这就是为什么在 Tynamo 的 SecurityModule
中以不同的方式绑定默认的 ModularRealmAuthenticator
的原因:
binder.bind(ModularRealmAuthenticator.class);
然而,当我尝试以这种方式覆盖我的 EnvironmentalRealmAuthenticator
时:
binder.bind(EnvironmentalRealmAuthenticator.class).withId("EnvironmentalRealmAuthenticator");
会导致以下异常:
Caused by: java.lang.IllegalStateException: Construction of service 'ServiceOverride' has failed due to recursion: the service depends on itself in some way. Please check org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31) via org.apache.tapestry5.ioc.modules.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:52) for references to another service that is itself dependent on service 'ServiceOverride'.
英文:
I am trying to use a custom authenticator for tapestry-security (org.tynamo.security).
I have a custom authenticator
public class EnvironmentalRealmAuthenticator extends ModularRealmAuthenticator
And in my module I override the default authenticator of Tapestry (ModularRealmAuthenticator
):
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);
}
However, this causes the cache to not be cleared on logout - I have the suspicion that this is caused by the way the DefaultSecurityManager
of Shiro detects if the authenticator listens to logouts:
Authenticator authc = getAuthenticator();
if (authc instanceof LogoutAware) {
((LogoutAware) authc).onLogout(principals);
}
As the EnvironmentalRealmAuthenticator
is bound as a Tapestry service, it is initially injected as a proxy and hence authc instanceof LogoutAware
yields false
- that's why the default ModularRealmAuthenticator
is bound in a different way in the SecurityModule
of Tynamo:
// 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);
However, when I try to override my EnvironmentalRealmAuthenticator
that way
binder.bind(EnvironmentalRealmAuthenticator.class).withId("EnvironmentalRealmAuthenticator");
this results in the following exception:
> Caused by: java.lang.IllegalStateException: Construction of service 'ServiceOverride' has failed due to recursion: the service depends on itself in some way. Please check org.apache.tapestry5.ioc.internal.services.ServiceOverrideImpl(Map) (at ServiceOverrideImpl.java:31) via org.apache.tapestry5.ioc.modules.TapestryIOCModule.bind(ServiceBinder) (at TapestryIOCModule.java:52) for references to another service that is itself dependent on service 'ServiceOverride'.
答案1
得分: 1
我无法确定,在没有看到导致异常的setupOverrides
方法的最终版本之前。
但是,你尝试过这样吗:
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);
}
英文:
I can't be sure without seeing the final version of your setupOverrides
method that caused that exception.
But, have you tried this:
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);
}
答案2
得分: 1
我似乎找到了一个(相当巧妙的)方法。而不是覆盖 Authenticator
本身,我覆盖了 WebSecuritymanager
:
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
,我对模块进行了注解:
@Marker(Primary.class)
然后,EnvironmentalSecurityManager
的实现如下所示:
/**
* 用于正确(且唯一)地标识 Authenticator(无需覆盖它)
*/
public class EnvironmentalSecurityManager extends TapestryRealmSecurityManager {
private final Logger logger = LoggerFactory.getLogger(EnvironmentalSecurityManager.class);
/**
* 注意 @Primary 注解,用于标识 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
被使用,而无需实际覆盖它。
英文:
I seem to have found a (rather hacky) way. Instead of overriding the Authenticator
itself, I override the WebSecuritymanager
instead:
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);
}
That way I don't have to bind the EnvironmentalRealmAuthenticator
with its interface. In order to be able to identify the new Authenticator
I annotated the module:
@Marker(Primary.class)
The implmentation of the EnvironmentalSecurityManager
then looks like this:
/**
* 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());
}
}
That way I can guarantee that the correct Authenticator
is used without actually having to override it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论