依赖
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.4.0</version>
</dependency>
工具类(含包)
package cn.jeefast.utils;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.time.DateUtils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
public class JwtUtils {
private static final String SECRET = "org.qiqiang.secret";
private static final String ISSUER = "zyh";
/**
* 生成token
*
* @param claims
* @return
*/
public static String createToken(Map<String, String> claims) throws Exception {
try {
Algorithm algorithm = Algorithm.HMAC256(SECRET);
JWTCreator.Builder builder = JWT.create()
.withIssuer(ISSUER)
//设置过期时间为2小时
.withExpiresAt(DateUtils.addHours(new Date(), 2));
claims.forEach(builder::withClaim);
return builder.sign(algorithm);
} catch (IllegalArgumentException e) {
throw new Exception("生成token失败");
}
}
/**
* 验证jwt,并返回数据
*/
public static Map<String, String> verifyToken(String token) throws Exception {
Algorithm algorithm;
Map<String, Claim> map;
try {
algorithm = Algorithm.HMAC256(SECRET);
JWTVerifier verifier = JWT.require(algorithm).withIssuer(ISSUER).build();
DecodedJWT jwt = verifier.verify(token);
map = jwt.getClaims();
} catch (Exception e) {
throw new Exception("鉴权失败");
}
Map<String, String> resultMap = new HashMap<>(map.size());
map.forEach((k, v) -> resultMap.put(k, v.asString()));
return resultMap;
}
}
调用方法
//储存
Map<String, String> map = new HashMap<>();
map.put("uid", userId+"");
map.put("name", mobile);
String token="";
try {
token = JwtUtils.createToken(map);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//jwt验证
Map<String, String> res = JwtUtils.verifyToken(token);