jwt:json web token,其实token就是一段字符串,由三部分组成:header,payload,signature
<dependency> <groupid>com.auth0</groupid> <artifactid>java-jwt</artifactid> <version>3.8.2</version></dependen愚人节搞笑短信cy>
//设置过期时间private static final long expire_date=30*60*100000;//token秘钥private static final string token_cret = "zceqiubfksjbfjh2020bqwe";
public static string token (string urname,string password){ string token = ""; try { //过期时间 date date = new date(system.currenttimemillis()+expire_date); //秘钥及加密算法 algorithm algorithm = algorithm.hmac256(token_cret); //设置头部信息 map<string,object> header = new hashmap<>(); header加快迅雷下载速度.put("typ","jwt"); header.put("alg","hs256"); //携带urname,password信息,生成签名 token = jwt.create() .withheader(header) .withclaim("urname",urname) .withclaim("password",password).withexpiresat(date) .sign(algorithm); }catch (exception e){ e.printstacktrace(); return null; } return token; }public static boolean verify(string token){ /** * @desc 验证token,通过返回true * @create 2019/1/18/018 9:39 * @params [token]需要校验的串 **/ try { algorithm algorithm = algorithm.hmac256(token_cret); jwtverifier verifier = jwt.require(algorithm).build(); decodedjwt jwt = verifier.verify(token); return true; }catch (exception e){ e.printstacktrace(); return fal; } }直接用生成的token去验证,成功
public static void main(string[] args) { string urname ="zhangsan"; string password = "123"; string token = token(urname,password); system.out.println(token); boolean b = verify(token); system.out.println(b); }package xxx.utils; //你的包 import com.auth0.jwt.jwt;import com.auth0.jwt.jwtverifier;import com.auth0.jwt.algorithms.algorithm;import com.auth0.jwt.interfaces.decodedjwt; import java.util.date;import 手机推广营销java.util.hashmap;import java.util.map;/** * @desc 使用token验证用户是否登录 * @author zm **/public class tokenutils { //设置过期时间 private static final long expire_date=30*60*100000; //token秘钥 private static final string token_cret = "zcfasfhuauuhufguguwu2020bqwe"; public static string token (string urname,string password){ string token = ""; try { //过期时间 date date = new date(system.currenttimemillis()+expire_date); //秘钥及加密算法 algorithm algorithm = algorithm.hmac256(token_cret); //设置头部信息 map<string,object> header = new hashmap<>(); header.put("typ","jwt"); header.put("alg","hs256"); //携带urname,password信息,生成签名 token = jwt.create() .withheader(header) .withclaim("urname",urname) .withclaim("password",password).withexpiresat(date) .sign(algorithm); }catch (exception e){ e.printstacktrace(); return null; } return token; } public static boolean verify(string token){ /** * @desc 验证token,通过返回true * @params [token]需要校验的串 **/ try { algorithm algorithm = algorithm.hmac256(token_cret); jwtverifier verifier = jwt.require(algorithm).build(); decodedjwt jwt = verifier.verify(token); return true; }catch (exception e){ e.printstacktrace(); return fal; } } public static void main(string[] args) { string urname ="zhangsan"; string password = "123"; string token = token(urname,password); system.out.println(token); boolean b = verify("eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyjwyxnzd22yzci6ij等我们老了eymyisimv4cci6mtu3ode5nzqxmywidxnlcm5hbwuioij6agfuz3nhbij9.iytzt0tisqqzhghsnuaqhgv8ld7idjuyjn3mgbulmjg"); system.out.println(b); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-03 23:48:32,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7329c341510a1b5a8b53186ed182fd7f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java登录功能实现token生成与验证.doc
本文 PDF 下载地址:Java登录功能实现token生成与验证.pdf
| 留言与评论(共有 0 条评论) |