当前位置:网站首页>JJWT tool class
JJWT tool class
2022-08-02 03:32:00 【Tom has no Cat】
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
JJWT 工具类
package com.example.utils.jwt;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
/** * @author zpc * @version 1.0 * @date 2022/2/19 13:56 */
@Component
@ConfigurationProperties(prefix = "jwt.config")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class JwtUtil {
private String key;
private long ttl;
/** * 生成 JWT * @param id * @param subject * @param roles 角色 * @return */
public String createJWT(String id, String subject, String roles) {
long nowMillis = System.currentTimeMillis();
Date nowDate = new Date(nowMillis);
HashMap<String, Object> map = new HashMap<>();
map.put("alg", "HS256");
map.put("typ", "JWT");
JwtBuilder builder = Jwts.builder()
.setHeaderParams(map)
// 设置 id
.setId(id)
// 设置用户名
.setSubject(subject)
// 设置 jwt 签发时间
.setIssuedAt(nowDate)
// Set the signature algorithm used for signing and the key used for signing
.signWith(SignatureAlgorithm.HS256, key)
.claim("roles", roles);
if (ttl > 0) {
builder.setExpiration(new Date(nowMillis + ttl));
}
return builder.compact();
}
/** * 解析 JWT * @param jwtStr * @return */
public Claims parseJWT(String jwtStr) {
return Jwts.parser()
.setSigningKey(key)
.parseClaimsJws(jwtStr)
.getBody();
}
}
application.yml 配置
jwt:
config:
ttl: 设置过期时间
key: Set the key used for signing
边栏推荐
猜你喜欢
About cross-domain issues
通过PS 2021 将网页图标抠下来
5. Hezhou Air32F103_LCD_key
磷脂-聚乙二醇-叠氮,DSPE-PEG-Azide,DSPE-PEG-N3,MW:5000
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
Week 7 Review
API 低代码开发:接口大师,一套开发、管理和提供接口的产品框架
RHCSA第三天
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
[Remote Control Development Basic Tutorial 3] Crazy Shell Open Source Formation UAV-ADC (Joystick Control)
随机推荐
CV-Model【4】:MobileNet v3
(转帖)HashCode总结(2)
关于跨域问题
MySQL两阶段提交串讲
STL入门基础 map和set容器
LeetCode:1161. 最大层内元素和【BFS层序遍历】
青蛙跳台阶:我如何得知它是一道斐波那契数列题?——应用题破题“三板斧”
黑马案例--实现 clock 时钟的web服务器
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
RHCSA第三天
【深度学习】从LeNet-5识别手写数字入门深度学习
CV-Model [4]: MobileNet v3
rem adaptation
JJWT工具类
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
手把手带你 Unity 入门之从零创建一个时钟(GameObjects 与 Scripts)
Redis的集群模式
About cross-domain issues
Using WebShell to get Shell Skills
HCIP-第十一天-MPLS+BGP