网站首页

JAVA 前后端分离jwt 工具类

中文Lee 2021/08/15 3525人围观
JAVA  
package com.lup.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import org.springframework.util.DigestUtils;

import java.util.Calendar;
import java.util.Date;

public class JwtUtil {

//加密secret
private static final String SECRET = "秘钥越复杂约好";
//过期时间,秒数 默认两个小时
private static final Integer TIME_OUT_SECOND = 3600 * 2;
//需要重新生成的秒数 如果token的时间超过这个 则重新生成token
private static final Integer NEED_CREATE_SECOND = 3600;

/**

* @param accountId * @param accountName

* @param password
* @return
*/
public static String createToken(String accountId, String accountName, String password) {
Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.SECOND, TIME_OUT_SECOND);

//这里说获取客户端的真实ip地址

String ip = Lib.getIPAddress();
String userAgent = Lib.getUserAgent();
String token = JWT.create()
.withClaim("accountId", accountId)
.withClaim("accountName", accountName)
.withClaim("ip", ip)
.withClaim("userAgent", userAgent)
.withClaim("key", DigestUtils.md5DigestAsHex(password.getBytes()))
.withExpiresAt(calendar.getTime())
.sign(Algorithm.HMAC256(SECRET));
return token;
}


/**
* 验证是否修改过密码
*
* @param decodedJWT
* @param password
* @return
*/
public static boolean isUpdatedPassword(DecodedJWT decodedJWT, String password) {
String oldPwd = decodedJWT.getClaim("key").asString();
String newPwd = DigestUtils.md5DigestAsHex(password.getBytes());
return oldPwd.equals(newPwd) ? false : true;
}


/**
* 是否需要重新生成token (为了延续token时长)
*
* @param decodedJWT
* @return
*/
public static boolean needCreate(DecodedJWT decodedJWT) {
Date timeoutDate = decodedJWT.getExpiresAt();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, TIME_OUT_SECOND - NEED_CREATE_SECOND);
if (timeoutDate.before(calendar.getTime())) {
return true;
}
return false;
}


/**
* 获取token信息 如果token有误则返回null,校验token
*
* @param token
* @return
*/
public static DecodedJWT getTokenInfo(String token) {
try {
return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
} catch (Exception e) {
// e.printStackTrace();
return null;
}
}

public static String verify(String token) {
try {
Algorithm algorithm = Algorithm.HMAC256(SECRET);
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT jwt = verifier.verify(token);
String accountId = jwt.getClaim("accountId").asString();
return accountId;
} catch (Exception e) {
return null;
}
}

/**
* 获取用户ID
*
* @param decodedJWT
* @return
*/
public static String getAccountId(DecodedJWT decodedJWT) {
return decodedJWT.getClaim("accountId").asString();
}

/**
* 获取用户账号
*
* @param decodedJWT
* @return
*/
public static String getAccountName(DecodedJWT decodedJWT) {
return decodedJWT.getClaim("accountName").asString();
}

/**
* 获取用户代理信息
*
* @param decodedJWT
* @return
*/
public static String getUserAgent(DecodedJWT decodedJWT) {
return decodedJWT.getClaim("userAgent").asString();
}

/**
* 获取用户代理信息
*
* @param decodedJWT
* @return
*/
public static String getIp(DecodedJWT decodedJWT) {
return decodedJWT.getClaim("ip").asString();
}


}

相关推荐

  • .htaccess中301强制跳转到带www前缀或不带www的域名

    相信很多站长朋友都有这样的的问题出现。即带www前缀的域名与不带www前缀的顶级域名收录情况是不同的。这个问题主要是由于搜索引擎对于顶级域名与二级域名权重判定不同造成的。毫无疑问地,唯一的域名能够给你带来更多的好处。不管它是带www还是不带www。因为,这样无论用户还是搜索引擎都会记住你网站的唯一域...

  • docker下php容器 curl本机无法访问【curl: (7) Failed to connect to x.x.x.x port 80: Host is unreachable)】

    问题描述:centos 7.9 服务器上 使用docker容器部署了php环境,但是使用curl的时候 ,访问其他机器ip正常,但是curl本机ip 出现 curl: (7) Failed to connect to x.x.x.x port 80: Host is unreachable...

  • centos7通过yum安装JDK1.8

    安装之前先检查一下系统有没有自带open-jdk命令:rpm -qa |grep javarpm -qa |grep jdkrpm -qa |grep gcj如果没有输入信息表示没有安装。如果安装可以使用rpm -qa | grep java |&n...

  • Scheduled 定时任务

    Scheduled 定时任务1 cron表达式指定定时器执行时间// 固定每天1点执行,无论上一次执行完没有,到时间会再执行。@Scheduled(cron = "0 0 1/1 * ?")//每一个小时执行一次@Scheduled(cron = "0 0 * * * ?") //每天上午...

  • centos7刚安装的docker 1.13.1启动报错Docker failed to start

    yum install docker device-mapper-event-libs -y   安装dockersystemctl start docker 报错解决 关闭seliunx  修改文件 /etc/sysconfig/docker加上=false...

  • [LuPHP] PHPMVC精简框架第一讲(基本MVC框架配置)

    LuPHP是一个精简版的PHPMVC框架,该框架经历了N个高可用系统的测验(数据存储量千万级别,并发1000),该框架结合了市面上流行的PHP框架,去除了臃肿的代码;本框架遵循即用即写的原则,做到项目中绝不存在多余(实际根本用不上)代码!该框架作者历经10年以上项目研发经验,总结并写出一套适合自己的...

  • 编程语言排行榜2019年12月 TIOBE编程语言排行榜2019年最新版

     TIOBE已经公布了编程语言排行榜2019年12月的数据,编程语言12月的排名有了新的变化,Java比C的指数高了2%,与上个月的0.2%相比,前进很多,Python继续占领第三名,下面一起来看看2019年12月编程语言排行榜。  2019年12月编程语言排行榜看点:  首先,Java比上个月的指...

  • centos7/linux 服务器 添加新硬盘并挂载

    一、查看现有磁盘设备  fdisk -l  发现/dev/sdb 为新加的硬盘;二、开始分区  fdisk /dev/sdb  fdisk -l    #再次查看分区情况,已经有了/dev/sdb1三、创建文件系统,并格式化  mkfs.ext4 /dev/sdb1四、将新分区挂在到文件系统  mk...