当前位置: 首页 > article >正文

授权与认证之jwt(一)创建Jwt工具类

JWT的Token要经过加密才能返回给客户端,包括客户端上传的Tokn,后端项目需要验证核
实。于是我们需要一个WT工具类,用来加密Token和验证Token的有效性。

一、导入依赖

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.10.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

二、定义密钥和过期时间

在application文件中加入,建议大家把密钥和过期时间定义到Spring Boot配置文件中,然后再值注入到javaBean中,这样维护起来比较方便。

emos:
  jwt:
    #密钥
    secret: abc123456
    #令牌过期时间(天)
    expire: 5
    #令牌缓存时间(天)
    cache-expire: 10

三、创建jwt工具类

@Component
@Slf4j
public class JwtUtil {

    @Value("${emos.jwt.secret}")  //读取的就是application文件中的数值
    private String secret;

    @Value("${emos.jwt.expire}")
    private int expire;

    //创建令牌
    private String createToken(int userId) {
        //根据expire算下过期时间在什么时候
        DateTime date = DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 5);

        //生成密钥
        Algorithm algorithm= Algorithm.HMAC256(secret);

        //创建内部类绑定userid,密钥和过期时间
        JWTCreator.Builder builder= JWT.create();

        builder.withClaim("userId", userId);
        builder.withExpiresAt(date);

        //生成的令牌
        String token = builder.sign(algorithm);
        return token;
    }

    //从令牌对象反向获取userid
    public int getUserId(String token) {
        DecodedJWT jwt = JWT.decode(token);

        Integer userId = jwt.getClaim("userId").asInt();
        return userId;
    }

    //验证令牌有效性
    public void verifyToken(String token) {
        //验证令牌内容有效性 创建算法对象
        Algorithm algorithm = Algorithm.HMAC256(token);

        //创建验证对象
        JWTVerifier build = JWT.require(algorithm).build();
        //验证token是否有问题
        build.verify(token);
    }
}


http://www.kler.cn/a/569020.html

相关文章:

  • 【音视频】编解码相关概念总结
  • 命令行方式安装KFS同步KES到KADB
  • python基于后门的神经网络模型水印通用方法
  • 【软件测试】_使用selenium进行自动化测试示例
  • 在QML中注册C++类型
  • 通过logback日志简单实现链路追踪
  • 总结前端常用数据结构 之 队列篇【JavaScript 】
  • yolov8,yolo11,yolo12 服务器训练到部署全流程 笔记
  • hive 面试题
  • Linux 检测内存泄漏方法总结
  • 【漫话机器学习系列】113.逻辑回归(Logistic Regression) VS 线性回归(Linear Regression)
  • JPA属性转换器的使用与实例解析
  • 3-5 WPS JS宏 工作表的移动与复制学习笔记
  • Vue3生命周期以及与Vue2的区别
  • 面试基础--JVM垃圾回收深度剖析(JDK8)
  • 北斗模块在无人机领域的革新应用与未来展望
  • 第一节:基于Winform框架的串口助手小项目---基础控件使用《C#编程》
  • MIT何恺明再次突破传统:分形递归架构引爆生成模型新纪元!
  • VirtualVM:Java 监控工具使用指南
  • LeetCode 72 - 编辑距离 (Edit Distance)