基于JWT Token 验证

来源:互联网 发布:mac os 10.11懒人版 编辑:程序博客网 时间:2024/05/16 07:33

因项目要前后端彻底分离,故采用JWT 进行验证


JWT 链接地址:https://github.com/auth0/java-jwt



maven  配置:

<dependency>    <groupId>com.auth0</groupId>    <artifactId>java-jwt</artifactId>    <version>3.2.0</version></dependency>

生成key:


Algorithm algorithmHS = Algorithm.HMAC256("secret");


生成基于RSA的key


//RSARSAPublicKey publicKey = //Get the key instanceRSAPrivateKey privateKey = //Get the key instanceAlgorithm algorithmRS = Algorithm.RSA256(publicKey, privateKey);


    • iss(Issuser):代表这个JWT的签发主体;
    • sub(Subject):代表这个JWT的主体,即它的所有人;
    • aud(Audience):代表这个JWT的接收对象;
    • exp(Expiration time):是一个时间戳,代表这个JWT的过期时间;
    • nbf(Not Before):是一个时间戳,代表这个JWT生效的开始时间,意味着在这个时间之前验证JWT是会失败的;
    • iat(Issued at):是一个时间戳,代表这个JWT的签发时间;
    • jti(JWT ID):是JWT的唯一标识。