网站首页
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();
}
}
相关推荐
-
.htaccess文件设置某目录下所有文件禁止访问
如网站,有些目录下的文件不允许被下载则需要设置.htaccess文件为了减少服务器压力:应将apache配置文件<Directory /> AllowOverride All</Directory> 最好修改成指定目录: <...
-
centos上libreoffice+unoconv安装步骤,实现word转pdf(可以php读取pdf页码)
php读取docx页码比较难操作,并且读取doc格式的页码数更难搞了,所以先将doc/docx/pptx/ppt 先转换为pdf,然后通过pdf读取页码就比较精确了一、libreoffice安装1、yum search libreoffice查询一下系统自带的安装包安装libreoffi...
-
Intellij IDEA 快捷键整理
【常规】Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Shift+Click,可以关闭文件Ctrl+[ OR ],可以跑到大括号的开头与结尾Ctrl+F12,可以显示当前文件的结构Ctrl+F7...
-
CentOS下php安装imagick扩展
1、安装ImageMagic[root@localhost download]# wget http://www.imagemagick.org/download/ImageMagick.tar.gz[root@localhost download]# tar -xzvf ImageMagick[r...
-
一款超级好用的驾校预约系统-公众号预约系统【驾校预约系统】
“驾校教练微信预约系统”可用于“驾校微信预约系统”、“教练微信预约系统”,其主要预约对象是学车学员,学员可依据教练信息、自身空闲时间合理选择预约时间,及时掌握学车学时。本系统一共包含三个端(学员端/教练端/管理端):1、学员在线预约教练(可单笔支付,可购买课时);2、教练收到预约推送,教练可以自由开...
-
.htaccess中301强制跳转到带www前缀或不带www的域名
相信很多站长朋友都有这样的的问题出现。即带www前缀的域名与不带www前缀的顶级域名收录情况是不同的。这个问题主要是由于搜索引擎对于顶级域名与二级域名权重判定不同造成的。毫无疑问地,唯一的域名能够给你带来更多的好处。不管它是带www还是不带www。因为,这样无论用户还是搜索引擎都会记住你网站的唯一域...
-
php的性能优化
1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍。 当然了,这个测试方法需要在十万级以上次执行,效果才明显。 其实静态方法和非静态方法的效率主要区别在内存:静态方法...
-
java 生成缩略图 imageIO异常:Unsupported Image Type, 不支持图像类型【附解决办法】
最近再做图片生成缩略图功能,发现大部分的图片都可以生成缩略图,但是偶尔有几个图片会报异常:Unsupported Image Type;几经折腾,发现报异常的图片格式为CMYK 格式,我们常见的图片格式都是RGB格式的,所以我们要把CMYK格式的图片转换成RGB格式的,网上有些办法转化后图片颜色会丢...