网站首页

JAVA 前后端分离jwt 工具类

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


}

相关推荐

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

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

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

  • js websocket

    <!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <script src="https://www.lizh...

  • 一款超级好用的驾校预约系统-公众号预约系统【驾校预约系统】

    “驾校教练微信预约系统”可用于“驾校微信预约系统”、“教练微信预约系统”,其主要预约对象是学车学员,学员可依据教练信息、自身空闲时间合理选择预约时间,及时掌握学车学时。本系统一共包含三个端(学员端/教练端/管理端):1、学员在线预约教练(可单笔支付,可购买课时);2、教练收到预约推送,教练可以自由开...

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

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

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

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

  • Intellij IDEA 快捷键整理

    【常规】Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ OR ],可以跑到大括号的开头与结尾Ctrl+F12,可以显示当前文件的结构Ctrl+F7...

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