Hutool Hutool
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
  • 核心(Hutool-core)
  • 配置文件(Hutool-setting)
  • 日志(Hutool-log)
  • 缓存(Hutool-cache)
  • JSON(Hutool-json)
  • 加密解密(Hutool-crypto)
  • DFA查找(Hutool-dfa)
  • 数据库(Hutool-db)
  • HTTP客户端(Hutool-http)
  • 定时任务(Hutool-cron)
  • 扩展(Hutool-extra)
  • 布隆过滤(Hutool-bloomFilter)
  • 切面(Hutool-aop)
  • 脚本(Hutool-script)
  • Office文档操作(Hutool-poi)
  • 系统调用(Hutool-system)
  • 图形验证码(Hutool-captcha)
  • 网络Socket(Hutool-socket)
  • JWT(Hutool-jwt)
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(Hutool-core)

  • 配置文件(Hutool-setting)

  • 日志(Hutool-log)

  • 缓存(Hutool-cache)

  • JSON(Hutool-json)

  • 加密解密(Hutool-crypto)

  • DFA查找(Hutool-dfa)

  • 数据库(Hutool-db)

  • HTTP客户端(Hutool-http)

  • 定时任务(Hutool-cron)

  • 扩展(Hutool-extra)

  • 布隆过滤(Hutool-bloomFilter)

  • 切面(Hutool-aop)

  • 脚本(Hutool-script)

  • Office文档操作(Hutool-poi)

  • 系统调用(Hutool-system)

  • 图形验证码(Hutool-captcha)

  • 网络Socket(Hutool-socket)

  • JWT(Hutool-jwt)

    • 概述
    • JWT工具-JWTUtil
    • JWT签名工具-JWTSignerUtil
      • 介绍
        • 对称签名
        • 非对称签名
        • 依赖于BounyCastle的算法
      • 使用
        • 创建预定义算法签名器
        • 创建自定义算法签名器
        • 自行实现算法签名器
    • JWT验证-JWTValidator
  • 指南
  • JWT(Hutool-jwt)
Hutool
2023-03-28
目录

JWT签名工具-JWTSignerUtil

特别赞助 by:

# 介绍

JWT签名算法比较多,主要分为非对称算法和对称算法,支持的算法定义在SignAlgorithm中。

# 对称签名

  • HS256(HmacSHA256)
  • HS384(HmacSHA384)
  • HS512(HmacSHA512)

# 非对称签名

  • RS256(SHA256withRSA)

  • RS384(SHA384withRSA)

  • RS512(SHA512withRSA)

  • ES256(SHA256withECDSA)

  • ES384(SHA384withECDSA)

  • ES512(SHA512withECDSA)

# 依赖于BounyCastle的算法

  • PS256(SHA256WithRSA/PSS)
  • PS384(SHA384WithRSA/PSS)
  • PS512(SHA512WithRSA/PSS)

# 使用

# 创建预定义算法签名器

JWTSignerUtil中预定义了一些算法的签名器的创建方法,如创建HS256的签名器:

final JWTSigner signer = JWTSignerUtil.hs256("123456".getBytes());
JWT jwt = JWT.create().setSigner(signer);

# 创建自定义算法签名器

通过JWTSignerUtil.createSigner即可通过动态传入algorithmId创建对应的签名器,如我们如果需要实现ps256算法,则首先引入bcprov-jdk15to18包:

<dependency>
	<groupId>org.bouncycastle</groupId>
	<artifactId>bcpkix-jdk18on</artifactId>
	<version>${bouncycastle.version}</version>
</dependency>

再创建对应签名器即可:

String id = "ps256";
final JWTSigner signer = JWTSignerUtil.createSigner(id, KeyUtil.generateKeyPair(AlgorithmUtil.getAlgorithm(id)));

JWT jwt = JWT.create().setSigner(signer);

# 自行实现算法签名器

JWTSigner接口是一个通用的签名器接口,如果想实现自定义算法,实现此接口即可。

上次更新: 2025/05/06, 10:48:51
JWT工具-JWTUtil
JWT验证-JWTValidator

← JWT工具-JWTUtil JWT验证-JWTValidator→

Theme by Vdoing | Copyright © 2023-2025 Hutool | MulanPSL-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×