当前位置:   article > 正文

Rust : Json Web Token_rust jsonwebtoken

rust jsonwebtoken

JWT的原理

见相关资料:
https://www.jianshu.com/p/576dbf44b2ae //写得很清楚
https://zhuanlan.zhihu.com/p/27370773
https://zhuanlan.zhihu.com/p/43094841

原理:

三部分:头部(header)载荷(payload)签证(sign)

其中载荷中:
标准中注册的声明:
iss:jwt签发者
sub:jwt所面向的用户
aud:接收jwt的一方
exp:jwt的过期时间,这个过期时间必须大于签发时间
nbf:定义在什么时间之前,该token都是不可用的
iat:jwt的签发时间
jti:jwt的唯一身份标识,避免重复

JWT的相关特点

优点

不需要储存在服务器的session中,更容易扩展服务器由于用户信息可以放入token中,所以可以少了一次数据库 / 缓存的查询操作,有更好的性能不需要预防CSRF的攻击

不足

token一经泄露或者被盗取,将会暴露该用户

建议

token的过期时间,不宜过长
非常重要的操作,需要手机验证码,支付密码等二次验证作为保险
尽量使用 https



https://zhuanlan.zhihu.com/p/43094841
// Header
{
  "alg": "HS256",
  "typ": "JWT"
}

// Payload负载: iss(签发者),exp(过期时间戳), sub(面向的用户), aud(接收方), iat(签发时间)
{
  "iss": "a.com",
  "exp": "1d",
  "http://a.com": true,
  "company": "A",
  "awesome": true
}

// Signature
HS256(Base64(Header) + "." + Base64(Payload), secretKey)

// JWT
JWT = Base64(Header) + "." + Base64(Payload) + "." + $Signature
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Rust中JWT的库见https://github.com/Keats/jsonwebtoken。
具体Rust库相关示例代码如下:

use std::{thread, time};
extern crate jsonwebtoken as jwt;
#[macro_use]
extern crate serde_derive;

use jwt::errors::ErrorKind;
use jwt::{decode, encode, Algorithm, Header, Validation};

#[derive(Debug, Serialize, Deserialize)]
struct Claims {
    sub: String,
    company: String,
    exp: usize,
}

fn main() {
    let sleep_seconds = time::Duration::from_secs(1000);
    let my_claims = Claims {
        sub: "b@b.com".to_owned(),
        company: "ACME".to_owned(),
        exp: 10000000000,
    };
    let key = "secret";

    let mut header = Header::default();
    header.kid = Some("signing_key".to_owned());
    header.alg = Algorithm::HS512;

    let token = match encode(&header, &my_claims, key.as_ref()) {
        Ok(t) => t,
        Err(_) => panic!(), // in practice you would return the error
    };
    println!("token:{:?}", token);

    let token_data =
        match decode::<Claims>(&token, key.as_ref(), &Validation::new(Algorithm::HS512)) {
            Ok(c) => c,
            Err(err) => match *err.kind() {
                ErrorKind::InvalidToken => panic!(), // Example on how to handle a specific error
                _ => panic!(),
            },
        };
    println!("claims:{:?}", token_data.claims);
    println!("header:{:?}", token_data.header);
    println!("jwt, i am learning it!");
    thread::sleep(sleep_seconds);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/951204
推荐阅读
相关标签
  

闽ICP备14008679号