【问题标题】:Adding attributes to Wicket session向 Wicket 会话添加属性
【发布时间】:2015-07-01 14:51:12
【问题描述】:

有一个WebSession的实现,它应该存储登录用户的ID:

public class SecurityWebSession extends AuthenticatedWebSession {

    public SecurityWebSession(Request request) {
        super(request);
        bind();
    }

    ...

    @Override
    public boolean authenticate(String username, String password) {
        user = usersFacadeLocal.findByEmail(username);
        if (user != null) {
            try {
                boolean valid = PasswordHash.validatePassword(password, user.getPassword());
                if (valid) {
                   WebSession.get().setAttribute(USER_ID, user.getId());
                }
                return valid;
            } catch (Exception ex) {
                logger.error("Authenticate ERROR", ex);
            }
        }
        return false;
    }
}

但是,当我访问 SecurityWebSession 以从 WebPage 类获取登录用户的 ID 时,它返回 null。我发现 Session 不存储从其主体添加的值。但是,如果从 Wicket 的网页继承的类中设置值,它会完美地存储值。

我没有在文档中找到任何关于这种情况的提及。如何从 Session 中添加 Session 属性?

【问题讨论】:

    标签: java session wicket


    【解决方案1】:

    您是否偶然使用 Wicket 6.19.0?
    如果是这种情况,那么您点击https://issues.apache.org/jira/browse/WICKET-5845。它已在 6.20.0 中修复。
    如果不是这种情况,请使用显示问题的快速启动应用程序创建一个新工单。谢谢!

    【讨论】:

    • 请创建一个演示应用并将其附加到 JIRA 中的工单上。谢谢!
    【解决方案2】:

    我猜问题出在AuthenticatedWebSession.signIn(final String username, final String password)

    这个调用你的authenticate 方法并且会再次destroy()bind() 你的会话(这样做是为了避免会话固定)。

    但是,您可以通过覆盖 replaceSession() 来临时存储您需要的值:

    // this will be called *after* a successful authenticate
    @Override
    public void replaceSession() {
        //temp store any values you want to carry over to the new session...
        super.replaceSession();
        //reset them to the session after super.replaceSession();
    }
    

    【讨论】:

    • #replaceSession() 仅在 6.19.0 中使用。它导致了一些问题,已在 6.20.0 (issues.apache.org/jira/browse/WICKET-5845) 中恢复。
    • 啊,好吧 - 我没有注意到 replcaeSession() 是在 6.19.0 中引入的。感谢分享。
    猜你喜欢
    • 2021-07-14
    • 2014-04-18
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多