gpt4 book ai didi

java - 注册新的undertow SessionManager

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:44:22 26 4
gpt4 key购买 nike

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

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

最佳答案

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,应该执行以下步骤:

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

关于java - 注册新的undertow SessionManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528548/

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