Handling sessions


vertx-web使用会话cookie来识别会话,它是临时的,关闭时将被浏览器删除。

我们不会把数据写入会话cookie中,他只是一个标识来在服务器上查找实际的会话。

会话id使用随机的UUID.

cookie会在HTTP请求和响应中传递,因此使用HTTPS是明智的,使用HTTP vert.x会发出警告。

您必须要在路由的最开始添加SessionHandler来启动。

Session stores

Session stores用来生成随机数。

vertx-web提供两种会话存储实现,您也可以自己实现。

本地会话存储

这种模式下,sessions存储在内存中,并且只对这个实例有效。

如果只有一个实例使用session并且负载均衡已经配置了始终将HTTP请求路由到同一个Vert.x实例,使用这种方式是恰当的,如果不是的话就不要用这种方式。

本地会话存储使用共享的local map实现,并具有定期清除过期会话的功能。

清除间隔可以配置LocalSessionStore.create.

例如

SessionStore store1 = LocalSessionStore.create(vertx);

// Create a local session store specifying the local shared map name to use
// This might be useful if you have more than one application in the same
// Vert.x instance and want to use different maps for different applications
SessionStore store2 = LocalSessionStore.create(vertx, "myapp3.sessionmap");

// Create a local session store specifying the local shared map name to use and
// setting the reaper interval for expired sessions to 10 seconds
SessionStore store3 = LocalSessionStore.create(vertx, "myapp3.sessionmap", 10000);


集群会话存储

使用这种方式,会话被存储在分布式map中,Vert.x集群可以访问。

如果您没有使用粘性会话,这种方式是恰当的。例如你的负载均衡将同一浏览器发送的不同请求分配到不同的服务器。

您可以在集群中的任何节点访问您的会话。

要使用集群会话,应确保实例已经集群。

例如

Vertx.clusteredVertx(new VertxOptions().setClustered(true), res -> {

  Vertx vertx = res.result();

  // Create a clustered session store using defaults
  SessionStore store1 = ClusteredSessionStore.create(vertx);

  // Create a clustered session store specifying the distributed map name to use
  // This might be useful if you have more than one application in the cluster
  // and want to use different maps for different applications
  SessionStore store2 = ClusteredSessionStore.create(vertx, "myclusteredapp3.sessionmap");
});

创建会话处理程序

在路由的最前边添加会话处理程序。

在会话处理程序前还需要添加CookieHandler.

例如

Router router = Router.router(vertx);

// We need a cookie handler first
router.route().handler(CookieHandler.create());

// Create a clustered session store using defaults
SessionStore store = ClusteredSessionStore.create(vertx);

SessionHandler sessionHandler = SessionHandler.create(store);

// Make sure all requests are routed through the session handler too
router.route().handler(sessionHandler);

// Now your application handlers
router.route("/somepath/blah/").handler(routingContext -> {

  Session session = routingContext.session();
  session.put("foo", "bar");
  // etc

});

使用会话

添加put,获取get,删除remove.

会话的键总是字符串,值可以是是任何基本类型,或者Buffer,JsonObject,JsonArray或一个可序列化的对象。

例如

router.route().handler(CookieHandler.create());
router.route().handler(sessionHandler);

// Now your application handlers
router.route("/somepath/blah").handler(routingContext -> {

  Session session = routingContext.session();

  // Put some data from the session
  session.put("foo", "bar");

  // Retrieve some data from a session
  int age = session.get("age");

  // Remove some data from a session
  JsonObject obj = session.remove("myobj");

});

在完成响应之后会话会写会存储中。

如果没有会话,下次请求会自动创建一个会话。

会话超时

会话超时后会从存储中删除。

当请求到达时,会话将自动标记为已访问,并查看会话,并在响应完成时存储会话。

您也可以使用setAccessed手动将会话标记为已访问。

会话超时可以在创建会话处理程序时进行配置,默认超时时间为30分钟。

猜你喜欢

转载自blog.csdn.net/zyydecsdn/article/details/80279459