网站首页
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.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();
}
}
相关推荐
-
mysqlbinlog 保存为sql文件。
执行如下命令:mysqlbinlog -vv --base64-output=decode-rows binlog路径 --result-file=要保存的sql路径例如:/www/server/mysql/bin/mysqlbinlog -vv --base64-output=decode-row...
-
Linux内核调优(大并发场景下)
为了让系统能够支持更大的并发,除了必须安装event扩展(或libevent扩展)之外,优化linux内核也是重中之重,以下优化每一项都非常非常重要,请务必按逐一完成。打开文件 /etc/sysctl.conf,增加以下设置#该参数设置系统的TIME_WAIT的数量,如果超过默认值则会被立即清除 ...
-
.htaccess文件设置某目录下所有文件禁止访问
如网站,有些目录下的文件不允许被下载则需要设置.htaccess文件为了减少服务器压力:应将apache配置文件<Directory /> AllowOverride All</Directory> 最好修改成指定目录: <...
-
docker 容器支持中文
客户有个需求:将table内容转换成图片显示;后端语言采用java;使用组件 HtmlImageGenerator结果发现,windows下中文不乱码,但是用docker部署到linux就乱码了所以先尝试不使用docker,结果还是乱码,然后给服务器安装中文字体(安装方法自行百度),安装好...
-
PHP性能优化方案
常用性能优化方案1.使用单引号替换双引号,单引号在运行的时候不检查运行引号内部的变量,执行效率是双引号的两倍;2.使用PHP内置的数组操作方法,PHP内置的数组操作方法的运行效率是自行编写代码的10倍以上;3.使用字符串函数替换正则函数,例如:使用 str_replace 替换&...
-
centos7通过yum安装JDK1.8
安装之前先检查一下系统有没有自带open-jdk命令:rpm -qa |grep javarpm -qa |grep jdkrpm -qa |grep gcj如果没有输入信息表示没有安装。如果安装可以使用rpm -qa | grep java |&n...
-
如何安装php7的event扩展
最近api系统遇到了高并发的瓶颈,想通过workerman重构。在看workerman文档时发现这么一句话:Event扩展不是必须的,当业务需要支撑上万并发连接时,推荐安装Event,能够支持巨大的并发连接。如果业务并发连接比较低,例如1000并发连接,则可以不用安装。如果无法安装Event扩展,可...
-
Scheduled 定时任务
Scheduled 定时任务1 cron表达式指定定时器执行时间// 固定每天1点执行,无论上一次执行完没有,到时间会再执行。@Scheduled(cron = "0 0 1/1 * ?")//每一个小时执行一次@Scheduled(cron = "0 0 * * * ?") //每天上午...