QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 1306|回复: 0

[JAVA/JSP] ssm项目实现用户登陆持久化——token

[复制链接]

等级头衔

积分成就    金币 : 2802
   泡泡 : 1516
   精华 : 6
   在线时间 : 1242 小时
   最后登录 : 2024-4-28

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-4-21 19:11:13 | 显示全部楼层 |阅读模式
       用户登录持久化就是每次访问不用账号密码来校验身份,在用户登录第一次之后会返回一个token字符串,之后的访问客户端将这个token加到请求体里发给服务器就可以验证身份了。" [. @& T5 Z( C' j+ r
       利用Jedis和JWT创建用户token- n  C6 ~. f( E6 N
1、JWT创建token! c0 H& K" {" M9 Z6 Q* U( C. \3 j
       maven依赖:
& ^; Q, O. D4 a% O3 Z: `
  1.         <dependency>
  2.             <groupId>com.auth0</groupId>
  3.             <artifactId>java-jwt</artifactId>
  4.             <version>3.3.0</version>
  5.         </dependency>
      创建JWT工具类,用于创建token和解析token
( R! l. X7 F7 b! w& Q( \1 k. Y
  1. import com.auth0.jwt.JWT;
  2. import com.auth0.jwt.JWTVerifier;
  3. import com.auth0.jwt.algorithms.Algorithm;
  4. import com.auth0.jwt.interfaces.Claim;
  5. import com.auth0.jwt.interfaces.DecodedJWT;
  6. public class JWTUtils {
  7.     /**
  8.      * 公钥
  9.      */
  10.     private static String SECRET = "qiang";  //此处随便设置一个自己的加密符号
  11.     public static String createToken(int id, String username,
  12.                                       String type) throws Exception {
  13.         // 签发时间
  14.         Date iatDate = new Date();
  15.         // 过期时间,7天时间
  16.         Calendar nowTime = Calendar.getInstance();
  17.         nowTime.add(Calendar.HOUR, 24 * 7);
  18.         Date experiesDate = nowTime.getTime();
  19.         Map<String, Object> map = new HashMap<String, Object>();
  20.         map.put("alg", "HS256");
  21.         map.put("typ", "JWT");
  22.         String token = JWT.create()
  23.                 .withHeader(map)
  24.                 .withClaim("id", id)
  25.                 .withClaim("username", username)
  26.                 .withClaim("type", type)
  27.                 .withExpiresAt(experiesDate) // 设置过期的日期
  28.                 .withIssuedAt(iatDate) // 签发时间
  29.                 .sign(Algorithm.HMAC256(SECRET)); // 加密
  30.         return token;
  31.     }
  32.     /**
  33.      * 解密
  34.      */
  35.     public static Map<String, Claim> verifyToken(String token) throws Exception {
  36.         JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
  37.         DecodedJWT jwt = null;
  38.         try {
  39.             jwt = verifier.verify(token);  //核实token
  40.         } catch (Exception e) {
  41.             throw new Exception("登录过期");
  42.         }
  43.         return jwt.getClaims();  //返回的是解析完的token,是一个map,里面有id,username,type键值对
  44.     }
  45. }
2、JedisUtil缓存token# ^- L0 w7 l$ t) X. ^
       首先讲讲Jedis,Jedis是集成了redis的一些命令操作,将其封装的java客户端,一般在其上封装一层作为业务使用,封装如下:; _# N: l' l& W1 g6 O/ K2 q
       首先导入maven包,这里也需要启动redis服务6 {" |( y8 F3 {/ F4 d7 Q
  1.         <dependency>
  2.             <groupId>redis.clients</groupId>
  3.             <artifactId>jedis</artifactId>
  4.             <version>2.9.0</version>
  5.         </dependency>
      然后设计一个Jedis工具类将其封装4 v" N* V/ w! m6 [" X
  1. import redis.clients.jedis.Jedis;
  2. public class JedisUtils {
  3.     private static Jedis jedis;
  4.     //初始化
  5.     private static void init() {
  6.         jedis = new Jedis("localhost");
  7.     }
  8.     //在redis中设置键值对存储
  9.     public static void setToken(String id, String token, int day) {
  10.         int second = day * 60 * 60 * 24;
  11.         JedisUtils.init();
  12.         jedis.set(String.valueOf(id), token); //根据id存储token
  13.         jedis.expire(String.valueOf(id), second);  //设置token持续时间
  14.     }
  15.     public static String getToken(String id) {
  16.         JedisUtils.init();
  17.         String token = jedis.get(String.valueOf(id));  //获取token
  18.         return token;
  19.     }
  20. }
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-4-29 01:41

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表