java sessionmanager_java – 注册新的SessionManager

我有野生飞行8.1服务器运行.我有自己的SessionManager实现io.undertow.server.session.SessionManager.我想配置系统使用我的会话管理器.

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

解决方法:

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-8,java,wildfly,undertow,session-management

来源: https://codeday.me/bug/20190829/1763536.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SessionJava Web中常用的一种状态管理技术,它可以在不同的HTTP请求之间共享数据,常用于用户登录、购物车等场景。下面我来浅谈一下Session的使用。 Session的创建 在Servlet中,可以通过HttpServletRequest的getSession()方法来获取或创建Session对象。如果请求中已经存在Session,则返回已经存在的对象,否则创建一个Session对象并返回。示例代码如下: ```java HttpSession session = request.getSession(); ``` Session的存储 Session中的数据存储在服务器端,可以通过setAttribute()方法向Session中存储数据。示例代码如下: ```java session.setAttribute("username", "张三"); ``` Session的获取 在Session中存储的数据可以通过getAttribute()方法获取。示例代码如下: ```java String username = (String) session.getAttribute("username"); ``` Session的过期 Session有两种过期方式:基于时间的过期和基于使用次数的过期。基于时间的过期是指Session在一定时间内没有被使用,则失效;基于使用次数的过期是指Session在一定时间内没有被使用,则失效。 在web.xml中可以设置Session的过期时间,示例代码如下: ```xml <session-config> <session-timeout>30</session-timeout> </session-config> ``` 上述代码表示Session的过期时间为30分钟。 Session的销毁 Session可以通过invalidate()方法销毁。示例代码如下: ```java session.invalidate(); ``` 总结 SessionJava Web中常用的一种状态管理技术,可以在不同的HTTP请求之间共享数据。Session的使用需要注意过期时间和销毁方式,以免造成安全隐患和资源浪费。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值