Jwt令牌设置介绍
Jwt令牌设置介绍
一、总述
本文将介绍一种主流的安全的回话跟踪技术——Jwt令牌。

二、代码实现
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import io.jsonwebtoken.Jwts;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;//@SpringBootTest
class ForestFireDetectionSystemApplicationTests {@Testpublic void testGenJwt(){// 生成 256 位(32 字节)安全密钥byte[] keyBytes = Keys.secretKeyFor(SignatureAlgorithm.HS256).getEncoded();String base64Key = javax.xml.bind.DatatypeConverter.printBase64Binary(keyBytes);Map<String,Object>claims = new HashMap<>();claims.put("id",8);claims.put("name","daming");String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256,Keys.hmacShaKeyFor(keyBytes))//设置签名算法
// 设置自定义数据(载合),将数据存储在map集合中.setClaims(claims)
// 设置令牌的有效期。这是毫秒级别的.setExpiration(new Date(System.currentTimeMillis() + 3600*1000))
// 调用compact生成jwt令牌.compact();System.out.println(jwt);}}