2015-05-29 46 views
3

我有野蠻8.1服務器運行。我有我自己的SessionManager實現io.undertow.server.session.SessionManager。我想配置系統以使用我的會話管理器。註冊新會話SessionManager

我在哪裏以及如何爲會話管理器配置/添加新設置?

回答

5
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) { 

    } 
} 

覆蓋默認InMemmorySessionManager與另一SessionManager以下步驟要做到:

  1. 開發SessionManager它實現io.undertow.server.session.SessionManager
  2. 開發SessionManagerFactory它實現io.undertow。 servlet.api.SessionManagerFactory
  3. 開發實現io.undertow.servlet.ServletExtension的啓動擴展,並在handleDeployment(Deployment)方法中更改sessionManagerFactory與新的SessionManagerFactory。
  4. 註冊加入../META-INF/services/io.undertow.servlet.ServletExtension文件新ServletExtension(文件應包含新ServletExtension的名稱。例如com.my.utils.StartupExtension)
+0

你可能是上面的一些示例代碼?我也不知道如何從我的安全wildfly模塊添加SessionManager。錯誤是:UT000012:會話管理器未附加到請求。確保SessionAttachmentHandler安裝在處理程序鏈中io.undertow.util.Sessions.getOrCreateSession(Sessions.java:57) – Meindert

+0

@meindert是否曾經找到一個工作示例? –

+0

@Meindert。我已經添加了示例工作(在我的情況下)的代碼。測試Wildfly 8.1,8.2,9和10 – Nem