V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xiaoyanbot

csrf 的随机 csrf token 默认是保存在 session 中, 如果改为分布式环境下, 只能将这一值保存在 redis 中吗?还有别的办法吗?

  •  
  •   xiaoyanbot · Oct 12, 2021 · 2896 views
    This topic created in 1657 days ago, the information mentioned may be changed or developed.
    9 replies    2021-10-15 09:33:31 +08:00
    Peter2Zhu
        1
    Peter2Zhu  
       Oct 12, 2021
    加密存在表单里
    Xusually
        2
    Xusually  
       Oct 12, 2021
    这个看你的 token 生成和验证方式

    如果是普通的随机生成的,那么在分布式环境中只能存在 redis 、memcache 等大家都可以读写验证的地方。
    如果是有算法的,比如服务端有 key 的可逆加密的,可以解出来有效信息,并且有效信息比如过期时间,用户 id 什么可供单点验证的,那倒也可以不存?


    楼主你的顾虑是?
    ragnaroks
        3
    ragnaroks  
       Oct 12, 2021
    有别的办法,但存在 memcache/redis 这里就是低成本最优解决办法了
    yemoluo
        4
    yemoluo  
       Oct 12, 2021
    使用 sha256 等算法,把密钥写死在代码里,然后生成 token = `sha256(密钥+时间戳)`, 下发下去给客户端的 csrf_token 值为 `token . 时间戳`,也就是把时间戳带下去给客户端。
    chendy
        5
    chendy  
       Oct 12, 2021
    用分布式的 session 不用单机 session ( java 这边可以 spring-session )就完事了。。。
    phithon
        6
    phithon  
       Oct 13, 2021
    csrf 的 token 可以保存在 cookie 里。可以看下这篇文章:<https://www.leavesongs.com/PENETRATION/think-about-cookie-form-csrf-protected.html>
    clcx
        7
    clcx  
       Oct 13, 2021 via iPhone
    JWT
    lululau
        8
    lululau  
       Oct 13, 2021
    1. 和 Rails 一样,直接把 session 加密放到 Cookie 里
    2. 单把 CSRF Token 放到 Cookie 里
    xiaoyanbot
        9
    xiaoyanbot  
    OP
       Oct 15, 2021
    @Xusually

    有道理,对的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   871 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 22:03 · PVG 06:03 · LAX 15:03 · JFK 18:03
    ♥ Do have faith in what you're doing.