网站首页

JAVA 前后端分离jwt 工具类

中文Lee 2021/08/15 2375人围观
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();
}


}

相关推荐

  • CentOS下php安装imagick扩展

    1、安装ImageMagic[root@localhost download]# wget http://www.imagemagick.org/download/ImageMagick.tar.gz[root@localhost download]# tar -xzvf ImageMagick[r...

  • JAVA 前后端分离jwt 工具类

    package com.lup.util;import com.auth0.jwt.JWT;import com.auth0.jwt.algorithms.Algorithm;import com.auth0.jwt.interfaces.DecodedJWT;import com.auth0.jw...

  • 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...

  • php7下安装event扩展

    一·、安装支持库libevent,需要编译高版本(这里以最新版本release-2.1.8-stable为例)1. wget -c https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent...

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

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

  • 面试还搞不懂redis,快看看这40道面试题(含答案和思维导图)

    1、什么是 Redis?.2、Redis 的数据类型?3、使用 Redis 有哪些好处?4、Redis 相比 Memcached 有哪些优势?5、Memcache 与 Redis 的区别都有哪些?6、Redis 是单进程单线程的?7、一个字符串类型的值能存储最大容量是多少?8、Redis 的持久化机...

  • 如何安装php7的event扩展

    最近api系统遇到了高并发的瓶颈,想通过workerman重构。在看workerman文档时发现这么一句话:Event扩展不是必须的,当业务需要支撑上万并发连接时,推荐安装Event,能够支持巨大的并发连接。如果业务并发连接比较低,例如1000并发连接,则可以不用安装。如果无法安装Event扩展,可...

  • 编程的程序员们,你们有语言崇拜么?比如PHP是世界上最好的语言

    有个有关程序员语言界的段子:问,你如何让一个论坛的人吵起来?答,PHP是世界上最好的语言。“PHP是世界上最好的语言”,虽然身在IT界,但说实话,这句话直到去年才明白,还是公司里招了个PHP程序员,问的他这句话到底是什么意思。大学中学的语言的话,FoxBase、C、C++,VB,Java,Ruby,...