token主要用在会话管理,防止表单提交和防止CSRF攻击,同时token支持跨域访问,无状态,不存储session信息。
一个JWT实际上就是一个字符串,它由三部分组成:头部、载荷与签名。将这三段信息文本用.链接一起就构成了Jwt字符串。
{
"typ": "JWT",
"alg": "HS256"
}
{
"password": "1234567890",
"name": "John Doe",
"admin": true
}
通过对token的解析可以获得有效数据。
使用场景:将DTO转换成VO,DTO转成Entity等各类对象相互转换 官方介绍:
Both types are rather similar, only the seat count attributes have different
names and the type attribute is of a special enum type in the Car class
but is a plain string in the DTO.
@Data
@Accessors(chain = true)
public class User {
private Long id;
private String username;
private String password;
private Integer sex;
private LocalDate birthday;
private LocalDateTime createTime;
private String config;
private String test;
}
@Data
@Accessors(chain = true)
public class UserVo {
private Long id;
private String username;
private String password;
private Integer gender;
private LocalDate birthday;
private String createTime;
private List<UserConfig> config;
private String test; // 测试字段
@Data
public static class UserConfig {
private String field1;
private Integer field2;
}
}
同等解决方案:BeanUtils工具类的copyProperty以及调用getter/setter方法进行属性赋值 MapStruct优点:
source:代表转换的源 target:代表转换的目标
MapStruct:参考文章
使用MapStruct无法遵循isXXX命名规则,官方认为这个不是bug也不会为此做出改变。