首页 > 其他分享 >src.backend.utils.JwtUtil

src.backend.utils.JwtUtil

时间:2024-02-09 14:23:08浏览次数:30  
标签:src return String utils static import JwtUtil public subject

package com.oep.backend.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.SecureDigestAlgorithm;
import org.springframework.stereotype.Component;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.UUID;

@Component
public class JwtUtil {
    public static final long JWT_TTL = 60 * 60 * 1000L * 24 * 14;  // 有效期14天
    public static final String JWT_KEY = "SDFGjhdsfalshdfHFdsjkdsfds12jqyhy3wppwpuqt31afasdfac2h0y01j02q08yi15";
    //加密算法
    private final static SecureDigestAlgorithm<SecretKey, SecretKey> signatureAlgorithm = Jwts.SIG.HS256;
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String createJWT(String subject) {
        JwtBuilder builder = getJwtBuilder(subject, null, getUUID());
        return builder.compact();
    }

    private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) {
        SecretKey secretKey = generalKey();
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        if (ttlMillis == null) {
            ttlMillis = JwtUtil.JWT_TTL;
        }

        long expMillis = nowMillis + ttlMillis;
        Date expDate = new Date(expMillis);
        return Jwts.builder()
                .id(uuid)
                .subject(subject)
                .issuer("sg")
                .issuedAt(now)
                .signWith(secretKey, signatureAlgorithm)
                .expiration(expDate);
    }

    public static SecretKey generalKey() {
        byte[] encodeKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
        return new SecretKeySpec(encodeKey, 0, encodeKey.length, "HmacSHA256");
    }

    public static Claims parseJWT(String jwt) throws Exception {
        SecretKey secretKey = generalKey();
        return Jwts.parser()
                .verifyWith(secretKey)
                .build()
                .parseSignedClaims(jwt)
                .getPayload();
    }
}

标签:src,return,String,utils,static,import,JwtUtil,public,subject
From: https://www.cnblogs.com/twinkler/p/18012452

相关文章

  • src.serviceImpl.UserDetailsServiceImpl
    packagecom.oep.backend.serviceImpl;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.oep.backend.mapper.AccountMapper;importcom.oep.backend.pojo.Account;importcom.oep.backend.serviceImpl.utils.UserDetailsImpl;importor......
  • src.backend.serviceImpl.UserDetailsServiceImpl
    packagecom.oep.backend.serviceImpl;importcom.oep.backend.pojo.Account;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importorg.springframework.security.core.GrantedAuthority;importorg.springframework.securit......
  • StringUtils中isNotEmpty()与isNotBlank()的区别
    首先说明Strings1="",s2=null;变量s1是非空,且长度为0(中间没有空格);变量s2是空,且s2.length()会报错,java.lang.NullPointerException。 所以:isNotEmpty(str)相等于str!=null&&str.length()>0isNotBlank(str)相当于str!=null&&str.length......
  • Mybatis Plus java.lang.NoSuchMethodError: com.baomidou.mybatisplus.core.toolkit.
    问题描述在进行SpringBoot整合MybatisPlus时提示10:49:08.390[restartedMain]DEBUGorg.springframework.boot.context.logging.ClasspathLoggingApplicationListener-Applicationfailedtostartwithclasspath:[file:/D:/%e7%99%be%e5%ba%a6%e7%bd%91%e7%9b%98/Vue......
  • edusrc-appkey泄露
    edusrc-appkey泄露简介AppKey泄露是指应用程序的应用密钥(AppKey)或API密钥被非法访问或公开的安全事件。AppKey是应用程序与第三方服务或API进行安全通信的凭证,通常用于身份验证和数据加密。如果这些密钥泄露,攻击者可以利用它们来访问敏感数据、执行未授权的操作或冒充合法的服务......
  • pyqt报错、python报错:src/pyaudio/device_api.c:9:10: fatal error: portaudio.h: 没
    报错信息:-DNDEBUG-fwrapv-O2-Wall-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-I/usr/local/include-I/usr/include-I/home/devil/anaconda3/envs/91/include/python3.10-csrc/pyaudio/d......
  • pyqt报错、python报错:src/pyaudio/device_api.c:9:10: fatal error: portaudio.h: 没
    报错信息:-DNDEBUG-fwrapv-O2-Wall-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-O2-isystem/home/devil/anaconda3/envs/91/include-fPIC-I/usr/local/include-I/usr/include-I/home/devil/anaconda3/envs/91/include/python3.10-csrc/pyaudio......
  • edusrc—freemarker模板注入RCE
    freemarker简述FreeMarker是一款模板引擎:即一种基于模板和要改变的数据,并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件信息收集开局一个登入框弱口令admin/123456登入......
  • xiaomir3g-openwrt-usbutils
    1、官方方式打开SSH;获取root权限;2、刷入openwrt官网包,在openwrtwiki;3、进入后台192.168.1.1,root,密码空;4、国内换镜像源,别用清华,中科大,阿里,腾讯的,他们的都不全,只用官方的镜像源;查找很简单,用设置源的地址,浏览器打开,记录镜像地址; 5、更新软件包;6、安装汉化包:luci-i18n-base......
  • ROS串口通信报错:IO Exception (13): Permission denied, file /tmp/binarydeb/ros-noe
    ROS在串口通信时,当我们插入USB后,catkin_make之后,报错:IOException(13):Permissiondenied,file/tmp/binarydeb/ros-noetic-serial-1.2.1/src/impl/unix.cc,line151.[ERROR][1705845384.528602780]:Unabletoopenport这是usb权限不够的原因我们首先查看接口设备:l......