注册新的undertow SessionManager

Posted

技术标签:

【中文标题】注册新的undertow SessionManager【英文标题】:Register new undertow SessionManager 【发布时间】:2015-08-12 06:13:23 【问题描述】:

我正在运行 Wildfly 8.1 服务器。我有自己的 SessionManager 实现 io.undertow.server.session.SessionManager。我想配置系统以使用我的会话管理器。

我应该在哪里以及如何为我的会话管理器配置/添加新设置?

【问题讨论】:

【参考方案1】:
public class StartupBeanExtension implements Extension, ServletExtension 
    @Override
    public void handleDeployment(DeploymentInfo deployment, ServletContext context) 
        boolean sessionPersistenceEnabled = Boolean.parseBoolean(BeanUtils.getBean(PropertyResolver.class).getValue("UAM.SessionPersistenceEnabled"));
        if (sessionPersistenceEnabled) 
            System.out.println("Overriding default InMemorySessionManager...[" + deployment.getDeploymentName() + ", " + deployment.getDisplayName() + "]");
            deployment.setSessionManagerFactory(new UAMSessionManagerFactory());
         else 
            System.out.println("InMemorySessionManager IS NOT OVERIDED!");
        
        


public class UAMSessionManagerFactory implements SessionManagerFactory 
    @Override
    public SessionManager createSessionManager(Deployment deployment) 
        UAMSessionManager ss = new UAMSessionManager(deployment.getDeploymentInfo().getDeploymentName());
        return ss;
    


public class UAMSessionManager extends InMemorySessionManager 

    public UAMSessionManager(String deploymentName) 
        super(deploymentName);

        UAMSessionListener uamSessionListener = new UAMSessionListener();
        super.registerSessionListener(uamSessionListener);

        System.out.println("New session manager created. Listener activated.");
    

    // create session
    public Session createSession(final HttpServerExchange serverExchange, final SessionConfig config, String sessionID) 
        config.setSessionId(serverExchange, sessionID);
        Session session = super.createSession(serverExchange, config);
        return session;
    

    // get session
    public Session getSession(final HttpServerExchange serverExchange, final SessionConfig config) 
        final String sessionId = config.findSessionId(serverExchange);
        Session session = getSession(sessionId);

        if (session == null) 
            // DO SOMETHING TO CREATE SESSION OR RESTORE IT FROM DB
            try 
                UAMService uam = getUAMService();
                if (uam != null) 
                    Sessions storedSession = uam.getSession(sessionId);

                    if (storedSession != null) 
                        String storedSessionId = storedSession.getSessionId();
                        // create new session with storedSessionID
                        session = createSession(serverExchange, config, storedSessionId);

                        // SET session attributes if needed from storedSession to new one

                     else 
                        // let InMemorySessionManager create new session
                        return null;
                    
                
             catch (Exception ex) 

            
        

        return session;
    


public class UAMSessionListener implements SessionListener 

    @Override
    public void sessionCreated(Session session, HttpServerExchange exchange) 

    

    @Override
    public void sessionDestroyed(Session session, HttpServerExchange exchange, SessionDestroyedReason reason) 

    

    @Override
    public void attributeAdded(Session session, String name, Object value) 
        UAMService uamService = getUAMService();

        if (uamService != null) 
            Sessions storedSession = uamService.getSession(session.getId());
            boolean isNew = false;
            if (storedSession == null) 
                storedSession = new Sessions();
                storedSession.setSessionId(session.getId());
                storedSession.setActvityDate(new Date());
                isNew = true;
            

            // STORE SOME INFO FROM value and update/create it in storage
            uamService.updateSession(storedSession, isNew);
        
    

    @Override
    public void attributeUpdated(Session session, String name, Object newValue, Object oldValue) 

    

    @Override
    public void attributeRemoved(Session session, String name, Object oldValue) 

    

    @Override
    public void sessionIdChanged(Session session, String oldSessionId) 

    

要使用另一个 SessionManager 覆盖默认 InMemmorySessionManager,应执行以下步骤:

    开发实现 io.undertow.server.session.SessionManager 的 SessionManager 开发实现 io.undertow.servlet.api.SessionManagerFactory 的 SessionManagerFactory 开发实现 io.undertow.servlet.ServletExtension 的启动扩展,并在 handleDeployment(Deployment) 方法中将 sessionManagerFactory 更改为新的 SessionManagerFactory。 通过添加 ../META-INF/services/io.undertow.servlet.ServletExtension 文件注册新的 ServletExtension(文件应包含新 ServletExtension 的名称。例如 com.my.utils.StartupExtension)

【讨论】:

您是否有上面的示例代码?我也不知道如何从我的安全 Wildfly 模块中添加 SessionManager。错误是:UT000012:会话管理器未附加到请求。确保 SessionAttachmentHandler 安装在处理程序链 io.undertow.util.Sessions.getOrCreateSession(Sessions.java:57) @Meindert。我添加了示例工作(在我的情况下)代码。通过 Wildfly 8.1、8.2、9 和 10 测试 谢谢。这很有帮助。我在文档中注意到的一件事是 SessionManager 需要遵循 SessionConfig 实现来检索 SessionId。您知道如何指定自定义 SessionConfig 实现吗?我在任何地方都找不到任何参考来实现这一点。 @EricB。可能这会有所帮助。会话会话 = super.createSession(serverExchange, new SessionConfig() ......); 我在想这个,但它坐得不好。如果容器向我传递了一个 SessionConfig 对象,那么它已经在更高的地方配置了要使用的对象。我试图弄清楚该配置发生在哪里,以便我可以从源头更改它。我怀疑某处必须有一个配置文件来执行此操作,但在任何地方都找不到对它的引用。这适用于 setSession 和 getSession 方法。我不应该覆盖容器期望我使用的对象。

以上是关于注册新的undertow SessionManager的主要内容,如果未能解决你的问题,请参考以下文章

Undertow:使用现有的 Servlet 实例

springboot使用undertow服务器UT005023出错

Wildfly vs undertow 嵌入式表演

为啥我玩QT一卡一卡的

undertow简单入门

undertow workthread 设置过小