gpt4 book ai didi

java - 如何在用户登录 BlazeDS 时为 FlexSession 设置一个属性?

转载 作者:行者123 更新时间:2023-11-28 23:36:02 27 4
gpt4 key购买 nike

在我的应用程序中,我想在用户登录时在他的 session 中存储一些有关用户的数据。这些数据将从数据库中检索并通过调用 flexSession.setAttribute('age', n) 放置在 FlexSession 中。我正在使用带有 JDBC 领域的 Tomcat。问题是,当用户使用他的登录名和密码进行身份验证时,我如何在 FlexSession 中放置一些数据?

我知道有一个 flex.messaging.security.LoginManager.java

 if (username != null && credentials != null)
{
Principal authenticated = loginCommand.doAuthentication(username, credentials);

if (authenticated == null) // Invalid login.
{
SecurityException se = new SecurityException();
se.setMessage(INVALID_LOGIN);
se.setCode(SecurityException.CLIENT_AUTHENTICATION_CODE);
throw se;
}
setCurrentPrincipal(authenticated);
}

但我不确定是否可以轻松覆盖它(管理器在 MessageBroker 实现中实例化)。因此,我只需要找到创建 FlexSession 的位置,使用用户名和密码并设置一些其他属性。

或者我可以使用 FlexSessionListener,通过添加

FlexSession.addSessionCreatedListener(new CustomFlexSessionListener());

但是去哪里呢?

期待您的想法。谢谢。

最佳答案

此处的解决方案:http://oncer-l.iteye.com/blog/1897972 .需要在服务配置文件中创建并指定一个新的引导服务:

<service id="listener" class="test.ListenersBootstrapService" />     

它将在 servlet 启动时执行,因此我们在其中添加:

@Override
public void initialize(String arg0, ConfigMap arg1) {
// TODO Auto-generated method stub
System.out.println("hello from init bootstrap listener!");

MyFlexSessionListener sessionListener = new MyFlexSessionListener();
FlexSession.addSessionCreatedListener(sessionListener);

}

在哪里

class MyFlexSessionListener implements FlexSessionListener { 

public void sessionCreated(FlexSession session)
{
System.out.println("FlexSession created: " + session.getId());
session.setAttribute("gender", "male");
}
}

非常感谢文章的作者。

但是,问题是 FlexContext.getUserPrincipal() 或 session.getUserPrincipal() 返回 null,这可能是因为 session 先创建,然后进行身份验证。需要找到监听 userPrincipal 上的属性更改事件的方法。

关于java - 如何在用户登录 BlazeDS 时为 FlexSession 设置一个属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com