代码之家  ›  专栏  ›  技术社区  ›  Chris

存储在warp会话中的数据不持久(Rust)

  •  0
  • Chris  · 技术社区  · 3 年前

    我正在尝试设置一个服务器后端作为练习。

    我想在中存储一些数据 warp_sessions::MemoryStore 但我无法实现。我基于 warp_sessions/examples/shared_mutable_session 实例

    我可以将数据写入会话。我可以在写入后立即读回(变量 _a _b 在下面的代码中),但它们不会在调用之间持久存在。变量 _a_pre _b_pre 在连续的处理程序调用上返回空字符串。

    现在的问题是:

    • 我做错了什么?

    • 如果我想对不同的路线使用相同的会话,并且我通过 session_store.clone() 在过滤器中,就像在warp示例中所做的那样——会话的修改会被存储吗?目前,我不使用存储的克隆实例,但数据无论如何都会丢失。然而,我确实需要不同路线使用这些数据

    • 在warp_sessions的文档中,据说MemoryStore不应在生产中使用。生产中应该使用什么?

    我的路由处理程序函数如下所示:

    pub async fn verify_handler(body: warp::hyper::body::Bytes, mut session_with_store:SessionWithStore<MemoryStore> ) -> Result<(Html<String>, SessionWithStore<MemoryStore>), Rejection>  {
    
      session_with_store.cookie_options = warp_sessions::CookieOptions {
        cookie_name: "siwe_minimal",
        cookie_value: None,
        max_age:Some(3600),
        domain: None,
        path: None,
        secure: true,
        http_only: true,
        same_site: Some(SameSiteCookieOption::Strict),
      };
      
      let shared_session = Arc::new(RwLock::new(session_with_store.session));
     
      let _a_pre: String = shared_session
          .read()
          .unwrap()
          .get("nonce")  
          ..unwrap_or_default();
    
      shared_session
          .write()
          .unwrap()
          .insert("nonce", nonce )   
          .unwrap();
        
      let _a: String = shared_session
          .read()
          .unwrap()
          .get("nonce")  
          .unwrap();
    
      let msgstr = message.to_string();
    
      let _b_pre: String = shared_session
      .read()
      .unwrap()
      .get("siwe")  
      .unwrap_or_default(); 
    
      shared_session
          .write()
          .unwrap()
          .insert("siwe", msgstr)  
          .unwrap();
    
           
      let _b: String = shared_session
      .read()
      .unwrap()
      .get("siwe")  
      .unwrap();
    
       
      session_with_store.session = Arc::try_unwrap(shared_session)
          .unwrap()
          .into_inner()
          .unwrap(); 
    
    
      Ok::<_, Rejection>(
        (
          warp::reply::html("req".to_string()),
          session_with_store
        )
      )
    
    }
    
    

    路线定义如下(如果重要的话):

    let session_store = MemoryStore::new();
    let verify_route 
        = warp::path("verify")
        .and(warp::post())
        .and(warp::body::bytes())
        .and(warp_sessions::request::with_session(session_store, None))
        .and_then( verify_handler)
        .untuple_one()
        .and_then(warp_sessions::reply::with_session)
        .recover(handle_rejection)
        .with(&cors); 
    
    0 回复  |  直到 2 年前
        1
  •  1
  •   ajp    3 年前

    看起来您正在将路由定义中的CookieOptions设置为None,这意味着warp_sessions代码将期望一个名为 sid 以获取会话ID。链接到显示此行为的代码:

    https://github.com/ajpauwels/warp-sessions/blob/c440817f41c331cfa21a0c3a995be20ab420bb5e/src/request.rs#L17

    但是,在路由处理程序中,您将cookie_options重写为不同的值,cookie名称为 siwe_minimal 这意味着,在warp_sessions逻辑的回复部分,包含会话ID的cookie将在调用之间保持会话,其名称将与路由定义所期望的不同。

    指向在warp_sessions回复代码中定义Set Cookie标头的链接: https://github.com/ajpauwels/warp-sessions/blob/c440817f41c331cfa21a0c3a995be20ab420bb5e/src/session.rs#L98

    链接到 to_string CookieOptions的定义,显示如何 cookie_name 使用了: https://github.com/ajpauwels/warp-sessions/blob/c440817f41c331cfa21a0c3a995be20ab420bb5e/src/cookie.rs#L24

    为了解决这个问题,我建议:

    1. 而不是将CookieOptions设置为 None 在路线定义中,在那里设置选项

    2. 保持 没有一个 在路由定义中,并完全删除路由处理程序中的选项重写

    推荐文章