网站首页

JAVA 前后端分离jwt 工具类

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


}

相关推荐

  • 如何安装php7的event扩展

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

  • html js 前端下载文件并且给文件重命名

    今天有一个需求,甲方要求 下载的文件名为 “标题.doc”,而我数据库存储的是时间戳(123072747621318656.doc),同时我又不想修改后端代码了,想着让前端实现一下 前端下载并指定名称。下面是下载的实现代码<div onclick="downLoad()">downLoa...

  • centos7通过yum安装JDK1.8

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

  • php的性能优化

    1.尽量静态化:   如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍。   当然了,这个测试方法需要在十万级以上次执行,效果才明显。   其实静态方法和非静态方法的效率主要区别在内存:静态方法...

  • PHP性能优化方案

    常用性能优化方案1.使用单引号替换双引号,单引号在运行的时候不检查运行引号内部的变量,执行效率是双引号的两倍;2.使用PHP内置的数组操作方法,PHP内置的数组操作方法的运行效率是自行编写代码的10倍以上;3.使用字符串函数替换正则函数,例如:使用 str_replace 替换&...

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

  • Linux内核调优(大并发场景下)

    为了让系统能够支持更大的并发,除了必须安装event扩展(或libevent扩展)之外,优化linux内核也是重中之重,以下优化每一项都非常非常重要,请务必按逐一完成。打开文件 /etc/sysctl.conf,增加以下设置#该参数设置系统的TIME_WAIT的数量,如果超过默认值则会被立即清除 ...

  • CentOS下php安装imagick扩展

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