Rust : Json Web Token

JWT的原理

见相关资料:
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

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

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: "[email protected]".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);
}

猜你喜欢

转载自blog.csdn.net/wowotuo/article/details/84453785