首页 > 作文

Java登录功能实现token生成与验证

更新时间:2023-04-03 23:48:33 阅读: 评论:0

一、token与cookie相比较的优势

1、支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的;2、无状态化,服务端无需存储token,只需要验证token信息是否正确即可,而ssion需要在服务端存储,一般是通过cookie中的ssion感恩节是什么节日id在服务端查找对应的ssion;3、无需绑定到一个特殊的身份验证方案(传统的用户名密码登陆),只需要生成的token是符合我们预期设定的即可;4、更适用于移动端(android,ios,小程序等等),像这种原生平台不支持cookie,比如说微信小程序,每一次请求都是一次会话,当然我们可以每次去手动为他添加cookie,详情请查看博主另一篇博客;5、避免csrf跨站伪造攻击,还是因为不依赖cookie;

二、基于jwt的token认证实现

jwt:json web token,其实token就是一段字符串,由三部分组成:header,payload,signature

1、引入依赖

<dependency>      <groupid>com.auth0</groupid>      <artifactid>java-jwt</artifactid>      <version>3.8.2</version></dependen愚人节搞笑短信cy>

2、设置密钥和生存时间

//设置过期时间private static final long expire_date=30*60*100000;//token秘钥private static final string token_cret = "zceqiubfksjbfjh2020bqwe";

3、实现签名方法

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;    }

4、验证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;        }    }

5、测试

直接用生成的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);    }

三、完整的token工具类代码

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 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图