Springboot+jwt實(shí)現(xiàn)在線用戶功能(示例代碼)
1.定義OnlineCounter用于記錄在線人員
package com.example.demo.config;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/*
* 在線用戶
* */
@Component
public class OnlineCounter {
private static Map countMap = new ConcurrentHashMap<String,Object>();
public void insertToken(String token){
//獲取當(dāng)前時(shí)間(毫秒)
//解析token
String userId = TokenUtil.getUserId(token);
countMap.put(userId,token);
}
/*
* 獲取當(dāng)前在線總數(shù)
* */
public Integer getOnlineCount(){
int onlineCount = 0;
//獲取countMap的迭代器
Iterator iterator = countMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Object> entry = (Map.Entry<String,Object>) iterator.next();
String token = (String) entry.getValue();
boolean flag = TokenUtil.hasExpiresAt(token);//返回true就是過(guò)期了
if(flag){
//移除
countMap.remove(entry.getKey());
}else{
onlineCount++;
}
}
return onlineCount;
}
/*
* 獲取當(dāng)前在線用戶列表
* */
public List<String> getOnlineUserList(){
List<String> userIdList = new ArrayList<>();
//獲取countMap的迭代器
Iterator iterator = countMap.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String,Object> entry = (Map.Entry<String,Object>) iterator.next();
String token = (String) entry.getValue();
boolean flag = TokenUtil.hasExpiresAt(token);//返回true就是過(guò)期了
if(flag){
//移除
countMap.remove(entry.getKey());
}else{
userIdList.add(entry.getKey());
}
}
return userIdList;
}
}2.定義一個(gè)攔截器,主要在驗(yàn)證通過(guò)攔截器的時(shí)候調(diào)用上面的方法插入一個(gè)新用戶
@component
public class JWTInterceptor implements HandlerInterception{
@Autowired
private OnlineCounter onlineCounter;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
return true;
} else {
//jwt業(yè)務(wù)邏輯代碼。。。。。。
//記錄在線人員
onlineCounter.insertUser(userId);
return true;
}
}
}

工具類需要新增方法用于判斷當(dāng)前token是否過(guò)期
/*
* 根據(jù)token判斷是否過(guò)期
* */
public static boolean hasExpiresAt(String token){
//創(chuàng)建token驗(yàn)證器
try{
JWTVerifier jwtVerifier=JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();
DecodedJWT decodedJWT=jwtVerifier.verify(token);
Date timeoutDate = decodedJWT.getExpiresAt();//獲取過(guò)期時(shí)間
long diffTime = timeoutDate.getTime() - new Date().getTime();//獲取過(guò)期時(shí)間與當(dāng)前時(shí)間的時(shí)間差(毫秒)
System.out.println("過(guò)期時(shí)間與當(dāng)前時(shí)間的時(shí)間差(毫秒):"+diffTime);
if(diffTime < 0){
//已過(guò)期
return true;
}else{
//未過(guò)期
return false;
}
}catch (Exception e){
return true;
}
}3.定義個(gè)控制器 獲取在線人員數(shù)量OnlineController
@ApiController(value = "/sys/online")
public class OnlineController {
@Autowired
private OnlineCounter onlineCounter;
/**
* 獲取當(dāng)前用戶在線人數(shù)
*
* @return
*/
@GetMapping(value = "/getOnlineCount")
public int getRealOnlineCount() {
Integer onlines = onlineCounter.getOnlineCount();
return onlines;
}
}
調(diào)用接口,結(jié)果如下:

到此這篇關(guān)于Springboot+jwt實(shí)現(xiàn)在線用戶功能的文章就介紹到這了,更多相關(guān)Springboot jwt在線用戶內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成?JWT實(shí)現(xiàn)用戶登錄認(rèn)證的項(xiàng)目實(shí)踐
- SpringBoot結(jié)合JWT實(shí)現(xiàn)用戶登錄、注冊(cè)、鑒權(quán)
- springBoot整合jwt實(shí)現(xiàn)token令牌認(rèn)證的示例代碼
- springboot中通過(guò)jwt令牌校驗(yàn)及前端token請(qǐng)求頭進(jìn)行登錄攔截實(shí)戰(zhàn)記錄
- SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的流程及示例
- SpringSecurity角色權(quán)限控制(SpringBoot+SpringSecurity+JWT)
相關(guān)文章
教你如何將Springboot項(xiàng)目成功部署到linux服務(wù)器
這篇文章主要介紹了如何將Springboot項(xiàng)目成功部署到linux服務(wù)器上,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
使用自定義注解實(shí)現(xiàn)redisson分布式鎖
這篇文章主要介紹了使用自定義注解實(shí)現(xiàn)redisson分布式鎖,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
深入了解Java語(yǔ)言中的并發(fā)性選項(xiàng)有何不同
這篇文章主要介紹了深入了解Java語(yǔ)言中的并發(fā)性選項(xiàng)有何不同,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06
詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)
這篇文章主要為大家詳細(xì)介紹了springboot的接口如何優(yōu)雅的接收時(shí)間類型參數(shù),文中為大家整理了三種常見的方法,希望對(duì)大家有一定的幫助2023-09-09

