JWTUtils

Fast token generation and parsing based on springboot

License

License

GroupId

GroupId

com.hanzoy
ArtifactId

ArtifactId

utils
Last Version

Last Version

3.1
Release Date

Release Date

Type

Type

jar
Description

Description

JWTUtils
Fast token generation and parsing based on springboot
Project URL

Project URL

https://github.com/Hanzoy/JWTUtils
Source Code Management

Source Code Management

http://github.com/Hanzoy/JWTUtils

Download utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.hanzoy/utils/ -->
<dependency>
    <groupId>com.hanzoy</groupId>
    <artifactId>utils</artifactId>
    <version>3.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.hanzoy/utils/
implementation 'com.hanzoy:utils:3.1'
// https://jarcasting.com/artifacts/com.hanzoy/utils/
implementation ("com.hanzoy:utils:3.1")
'com.hanzoy:utils:jar:3.1'
<dependency org="com.hanzoy" name="utils" rev="3.1">
  <artifact name="utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.hanzoy', module='utils', version='3.1')
)
libraryDependencies += "com.hanzoy" % "utils" % "3.1"
[com.hanzoy/utils "3.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 2.4.1
com.auth0 : java-jwt jar 3.11.0
org.springframework.boot : spring-boot-configuration-processor Optional jar 2.4.1

Project Modules

There are no modules declared in this project.

JWTUtils

快速生成token工具包

JWTUtils2.2使用详解

引言

这是一个我自己写的一个工具类,用于快速生成Token的一个工具类,GitHub地址:https://github.com/Hanzoy/JWTUtils

该工具包提供了JWTUtils核心类和@Token注解,通过在实体类属性上使用@Token注解,可以快速根据注解标记的属性创建Token,也可以快速从token中解析出我们所需要的实体类

依赖引入

在pom.xml中加入依赖

<!-- https://mvnrepository.com/artifact/com.hanzoy/utils -->
<dependency>
    <groupId>com.hanzoy</groupId>
    <artifactId>utils</artifactId>
    <version>2.2</version>
</dependency>

基本使用

springboot配置

在springboot配置文件中添加token签名token有效期

image-20210311001631524

其中time属性单位为,支持如图所示的表达式

@Token

注解在属性

该注解用于标记需要生成token的属性,该注解可重复注解,多个注解时,后面加上对应的值可做到分组效果

import com.hanzoy.utils.Token;

public class People {
    private Long id;

    @Token
    @Token("group2")
    private String name;

    @Token
    @Token("group2")
    private int age;
    
    @Token
    private String userid;
    private String password;
}

上述示例中通过对token添加不同的值,将其分类为默认组和**"group2"**组

该注解是支持复杂类型的字段

JWTUtils类

该类是该工具包的核心类,该类通过springboot配置文件传入sign(签名)和time(有效期)

通过spring自动注入的方式获取该类

image-20210311001918456

createToken方法

image-20210311002043578

该方法传入一个对象,工具类会自动扫描获取到拥有@Token注解的字段,并将其写入token中,传入group可以扫描指定分组的字段

实例方法只需传入需要生成token的实例 t 就行了,如果需要特定组的在后面填上value即可,如:若使用上面的group2组,则在value上传入**"group2"**字符串即可

People people = new People();
people.setName("hanzoy");
people.setAge(19);
people.setUserid("hanzoy");
people.setPassword("123456");

String token1 = jwtUtils.createToken(people);
String token2 = jwtUtils.createToken(people, "group2");

当然该方法也支持传入一个map集合,但是map必须为**Map<String, ?>**的子类

createTokenFromMap方法

image-20210311002605091

该方法传入一个Map<String, ?>极其子类参数,上面的createToken方法接受到map类型参数时会自动调用该方法。

HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("name", "hanzoy");
hashMap.put("age", 19);
ArrayList<String> books = new ArrayList<>();
books.add("Chinese");
books.add("English");
hashMap.put("Books", books);

String token1 = jwtUtils.createTokenFromMap(hashMap);
String token2 = jwtUtils.createToken(hashMap);
createTokenCustomFields方法

image-20210311002923616

该方法传入一个对象以及需要写入token的字段名称,例如:

String token = jwtUtils.createTokenCustomFields(people, "name", "age", "userid");
checkToken方法

image-20210122173354028

该方法提供检验token签名和有效期的功能

getBean方法

image-20210311004153754

该方法提供了将token转化为对应的java对象的方法。

例如:

People people = jwtUtils.getBean(token, People.class);
getBeanAsMap方法

image-20210311004348019

该方法提供了将token转化为Map集合的方法,默认将其转化为**Map<String, Object>**类型,同样也支持自定义返回类型

例如:

Map<String, Object> map1 = jwtUtils.getBeanAsMap(token);
Map<String, String> map2 = jwtUtils.getBeanAsMap(token, String.class);
getValueFromToken方法

image-20210324193254379

该方法提供了从token中通过key值直接获取value值的方法。

例如:

String username = jwtUtils.getValueFromToken(token, "username");

Versions

Version
3.1
3.0
2.2
2.1
2.0
1.0