ssm項(xiàng)目實(shí)現(xiàn)用戶登陸持久化(token)
用戶登錄持久化就是每次訪問不用賬號密碼來校驗(yàn)身份,在用戶登錄第一次之后會(huì)返回一個(gè)token字符串,之后的訪問客戶端將這個(gè)token加到請求體里發(fā)給服務(wù)器就可以驗(yàn)證身份了。
利用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"; //此處隨便設(shè)置一個(gè)自己的加密符號
public static String createToken(int id, String username,
String type) throws Exception {
// 簽發(fā)時(shí)間
Date iatDate = new Date();
// 過期時(shí)間,7天時(shí)間
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) // 設(shè)置過期的日期
.withIssuedAt(iatDate) // 簽發(fā)時(shí)間
.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); //核實(shí)token
} catch (Exception e) {
throw new Exception("登錄過期");
}
return jwt.getClaims(); //返回的是解析完的token,是一個(gè)map,里面有id,username,type鍵值對
}
}
2、JedisUtil緩存token
首先講講Jedis,Jedis是集成了redis的一些命令操作,將其封裝的java客戶端,一般在其上封裝一層作為業(yè)務(wù)使用,封裝如下:
首先導(dǎo)入maven包,這里也需要啟動(dòng)redis服務(wù)
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
然后設(shè)計(jì)一個(gè)Jedis工具類將其封裝
import redis.clients.jedis.Jedis;
public class JedisUtils {
private static Jedis jedis;
//初始化
private static void init() {
jedis = new Jedis("localhost");
}
//在redis中設(shè)置鍵值對存儲(chǔ)
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存儲(chǔ)token
jedis.expire(String.valueOf(id), second); //設(shè)置token持續(xù)時(shí)間
}
public static String getToken(String id) {
JedisUtils.init();
String token = jedis.get(String.valueOf(id)); //獲取token
return token;
}
}
到此這篇關(guān)于ssm項(xiàng)目實(shí)現(xiàn)用戶登陸持久化(token)的文章就介紹到這了,更多相關(guān)ssm 用戶登陸持久化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解lombok @Getter @Setter 使用注意事項(xiàng)
這篇文章主要介紹了詳解lombok @Getter @Setter 使用注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Struts2實(shí)現(xiàn)上傳單個(gè)文件功能
這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)上傳單個(gè)文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
SpringBoot2.7?WebSecurityConfigurerAdapter類過期配置
這篇文章主要為大家介紹了SpringBoot2.7中WebSecurityConfigurerAdapter類過期應(yīng)該如何配置,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Springboot+MyBatis進(jìn)行日志輸出參考示例
這篇文章主要給大家介紹了關(guān)于Springboot+MyBatis進(jìn)行日志輸出的相關(guān)資料,在項(xiàng)目開發(fā)過程中,日志是必不可少的,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
Springboot整合Mybatispuls的實(shí)例詳解
這篇文章主要介紹了Springboot整合Mybatispuls的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
java.lang.NullPointerException異常的幾種原因及解決方案
本文主要介紹了java.lang.NullPointerException異常的幾種原因及解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

