java使用jwt

在一个标准项目,拦截器或者过滤器一定不会少了。有了这些,那必须使用令牌验证了。这里讲解的是jwt

可以去看一下我上一篇文章,使用token和redis验证

1、JWT

1️⃣什么是jwt

jwt 全称是 json web token
在网络应用环境声明而执行的一种基于json的开发标准
jwt应用分布式的当点托登录系统,身份提供者和服务者提供者传递认证用户信息

2️⃣jwt认证流程

  • 用户输入用户密码进行登录
  • 服务判断用户登录是否正确
    -如正确,则颁发给用户一个token
  • 客户端存token,在以后每次请求主动等上token
  • 服务器去验证token处理流程

3️⃣jwt的组成

  • 第一部分 是头部
  • 第二部分 是载荷
  • 第三部分 是签名

2、引入依赖

<!-- https://mvnrepository.com/artifact/com.auth0/java-jwt -->
<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.10.3</version>
</dependency>

3、封装

根据项目,我们可以把jwt封装一下,命名为jwtUtils

1️⃣生成

在这里插入图片描述

2️⃣提取

在这里插入图片描述

3️⃣验证

    public static DecodedJWT getToken(String token){
        try{
            Algorithm algorithm=Algorithm.HMAC256(SECRET);
            JWTVerifier build=JWT.require(algorithm).build();//验证对象
            return build.verify(token);
        }catch (SignatureVerificationException e){
            ThrowException.verify(true,400,"无效签名!");
            return null;
        }catch (TokenExpiredException e){
            ThrowException.verify(true,401,"token过期!");
            return null;
        }catch (AlgorithmMismatchException e){
            ThrowException.verify(true,400,"算法不一致!");
            return null;
        }catch (Exception e){
            ThrowException.verify(true,400,"token无效");
            return null;
        }
    }

其中 ThrowException,这是我自定义的抛出异常,供全局异常获取,然后统一处理呢
(如果不存在,他会抛出异常)

使用案例


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import xyz.kszs.exceptions.ThrowException;

import java.util.Calendar;
import java.util.UUID;

public class JwtUtil {
    /**
     * 秘钥
     */
    private static final String SECRET = "密码";


    /**
     * 1.token 用户非法访问拦截
     * @return 2个小时,
     */
    public static String creatToken(){
        JWTCreator.Builder builder=JWT.create();
        builder.withClaim("UUID", UUID.randomUUID().toString());//确保RToken是唯一
        Calendar instance=Calendar.getInstance();
        //instance.add(Calendar.DATE,30);//30天
        instance.add(Calendar.HOUR, 2);//2个小时
        builder.withExpiresAt(instance.getTime());
        return builder.sign(Algorithm.HMAC256(SECRET));
    }

    /**
     * 2.是否合法(如果错误会自动抛出异常)重点是401过期
     * @param token 凭证
     * @return 结果
     */
    public static DecodedJWT getToken(String token){
        try{
            Algorithm algorithm=Algorithm.HMAC256(SECRET);
            JWTVerifier build=JWT.require(algorithm).build();//验证对象
            return build.verify(token);
        }catch (SignatureVerificationException e){
            ThrowException.verify(true,400,"无效签名!");
            return null;
        }catch (TokenExpiredException e){
            ThrowException.verify(true,401,"token过期!");
            return null;
        }catch (AlgorithmMismatchException e){
            ThrowException.verify(true,400,"算法不一致!");
            return null;
        }catch (Exception e){
            ThrowException.verify(true,400,"token无效");
            return null;
        }
    }

    public static Integer ExceptionCode(String token){
        try{
            Algorithm algorithm=Algorithm.HMAC256(SECRET);
            JWT.require(algorithm).build();//验证对象
            return 200;
        }catch (SignatureVerificationException e){
            return 400;//无效签名!
        }catch (TokenExpiredException e){
            return 401;//token过期!(重点)
        }catch (AlgorithmMismatchException e){
            return 402;//算法不一致!
        }catch (Exception e){
            return 403;//token其它问题
        }
    }

}



总结

jwt对比用 token和redis,最大优点,是他可以存储载荷,不需要在去数据库而外获取,减少了代码量
当然他是可以结合springboot使用呢

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值