ssm項目實現(xiàn)用戶登陸持久化(token)
用戶登錄持久化就是每次訪問不用賬號密碼來校驗身份,在用戶登錄第一次之后會返回一個token字符串,之后的訪問客戶端將這個token加到請求體里發(fā)給服務器就可以驗證身份了。
利用Jedis和JWT創(chuàng)建用戶token
1、JWT創(chuàng)建token
maven依賴:
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.3.0</version>
</dependency>
創(chuàng)建JWT工具類
用于創(chuàng)建token和解析token
import com.auth0.jwt.JWT;
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 String SECRET = "qiang"; //此處隨便設置一個自己的加密符號
public static String createToken(int id, String username,
String type) throws Exception {
// 簽發(fā)時間
Date iatDate = new Date();
// 過期時間,7天時間
Calendar nowTime = Calendar.getInstance();
nowTime.add(Calendar.HOUR, 24 * 7);
Date experiesDate = nowTime.getTime();
Map<String, Object> map = new HashMap<String, Object>();
map.put("alg", "HS256");
map.put("typ", "JWT");
String token = JWT.create()
.withHeader(map)
.withClaim("id", id)
.withClaim("username", username)
.withClaim("type", type)
.withExpiresAt(experiesDate) // 設置過期的日期
.withIssuedAt(iatDate) // 簽發(fā)時間
.sign(Algorithm.HMAC256(SECRET)); // 加密
return token;
}
/**
* 解密
*/
public static Map<String, Claim> verifyToken(String token) throws Exception {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
DecodedJWT jwt = null;
try {
jwt = verifier.verify(token); //核實token
} catch (Exception e) {
throw new Exception("登錄過期");
}
return jwt.getClaims(); //返回的是解析完的token,是一個map,里面有id,username,type鍵值對
}
}
2、JedisUtil緩存token
首先講講Jedis,Jedis是集成了redis的一些命令操作,將其封裝的java客戶端,一般在其上封裝一層作為業(yè)務使用,封裝如下:
首先導入maven包,這里也需要啟動redis服務
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
然后設計一個Jedis工具類將其封裝
import redis.clients.jedis.Jedis;
public class JedisUtils {
private static Jedis jedis;
//初始化
private static void init() {
jedis = new Jedis("localhost");
}
//在redis中設置鍵值對存儲
public static void setToken(String id, String token, int day) {
int second = day * 60 * 60 * 24;
JedisUtils.init();
jedis.set(String.valueOf(id), token); //根據(jù)id存儲token
jedis.expire(String.valueOf(id), second); //設置token持續(xù)時間
}
public static String getToken(String id) {
JedisUtils.init();
String token = jedis.get(String.valueOf(id)); //獲取token
return token;
}
}
到此這篇關于ssm項目實現(xiàn)用戶登陸持久化(token)的文章就介紹到這了,更多相關ssm 用戶登陸持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
datax-web在windows環(huán)境idea中模塊化打包部署操作步驟
這篇文章主要介紹了datax-web在windows環(huán)境idea中模塊化打包部署操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05
一文詳細springboot實現(xiàn)MySQL數(shù)據(jù)庫的整合步驟
Spring Boot可以很方便地與MySQL數(shù)據(jù)庫進行整合,下面這篇文章主要給大家介紹了關于springboot實現(xiàn)MySQL數(shù)據(jù)庫的整合步驟,文中通過圖文以及代碼介紹的非常詳細,需要的朋友可以參考下2024-03-03
spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息(推
這篇文章主要介紹了spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息的相關資料,需要的朋友可以參考下2018-01-01
Java實現(xiàn)BP神經(jīng)網(wǎng)絡MNIST手寫數(shù)字識別的示例詳解
這篇文章主要為大家詳細介紹了Java實現(xiàn)BP神經(jīng)網(wǎng)絡MNIST手寫數(shù)字識別的相關方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01

