我正在使用 Seam 2.2.Final 并按照第 15.3.5.1 章中的说明进行操作。基于 token 的记住我身份验证。
<security:identity authenticate-method="#{authenticator.authenticate}" />
<security:remember-me enabled="true" mode="autoLogin" cookie-max-age="604800"/>
<security:jpa-token-store token-class="com.mydomain.AuthenticationToken" />
<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
<action execute="#{identity.tryLogin()}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>
当我登录并选中“记住我”时,数据库中会创建一个新 token 。但是当 session 超时并且我尝试返回时,我得到一个 NullPointerException
Caused by: java.lang.NullPointerException
at org.jboss.seam.security.management.IdentityManager.isUserEnabled(IdentityManager.java:130)
at org.jboss.seam.security.RememberMe$1.execute(RememberMe.java:306)
at org.jboss.seam.security.Identity.runAs(Identity.java:743)
at org.jboss.seam.security.RunAsOperation.run(RunAsOperation.java:75)
当identityStore初始化时,调用后identityStore为null。
protected void initIdentityStore()
{
// Default to JpaIdentityStore
if (identityStore == null)
{
identityStore = (IdentityStore) Component.getInstance(JpaIdentityStore.class, true);
}
我是否必须像这样配置 IdentityStore
<security:jpa-identity-store
user-class="com.domain.model.UserAccount"
role-class="com.domain.model.UserRole"/>
我不得不说,我不确定要放入什么内容,以及需要哪些附加注释,尤其是在需要的文档中未提及的地方。
谢谢
编辑:将打印此消息,他必须在哪里找到该方法。它不包含在 seam.core.Init 类的工厂映射中。
if ( log.isTraceEnabled() ) log.trace("Seam component not found: " + name);
编辑:好吧,看来我必须在 Components.xml 中“安装”组件并覆盖 JpaIdentityStore,至少到目前为止是lookupUser 方法。
最佳答案
总结一下我遇到的问题。文档第 15.3.5.1 章。仅涵盖所需要点的一半。
RememberMe 使用 IdentityManager,它本身使用 IdentityStore,默认情况下未启用。所以
<component class="org.jboss.seam.security.management.JpaIdentityStore"/>
是必需的。进一步需要 JpaIdentityStore 15.4.2 中提到的一些配置。但是我的用户名不是帐户对象的一部分(它包含在帐户对象的列表中),并且我的角色不是实体,只是一个枚举(好吧,可以更改)。无论如何,我跳过了 15.4.2 中的配置并覆盖了 isUserEnabled() 和 getImpliedRoles 方法。
您应该添加 Identity.tryLogin(),但使用 tryLogin() 会触发静默登录,不会引发 LoginSuccessful 事件。返回捕获的页面。
.
<event type="org.jboss.seam.security.notLoggedIn">
<action execute="#{redirect.captureCurrentView}"/>
<action execute="#{identity.tryLogin()}"/>
</event>
<event type="org.jboss.seam.security.loginSuccessful">
<action execute="#{redirect.returnToCapturedView}"/>
</event>
我制作了自己的authenticator.tryLogin(),我在其中调用identity.tryLogin(),然后触发loginSuccessful事件。现在的问题是,所有这些事件都发生在一行中,并且 captureCurrentView 和 returnToCapturedView 都在执行第一个事件时进行重定向,以便我进入登录页面。
- RememberMe 默认行为是在用户的 token 不存在时使该用户的所有 token 失效。所以每个人都可以用别人的用户名设置cookie来使他的所有 token 失效。
.
if (tokenStore.validateToken(decoded.getUsername(), decoded.getValue()))
{
credentials.setUsername(decoded.getUsername());
credentials.setPassword(decoded.getValue());
}
else
{
// Have we been compromised? Just in case, invalidate all authentication tokens
tokenStore.invalidateAll(decoded.getUsername());
}
关于java - 基于Token的rememberMe的Seam IdentityStore NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6753135/