欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot+jwt實(shí)現(xiàn)在線用戶功能(示例代碼)

 更新時(shí)間:2024年12月23日 09:51:55   作者:修羅-zero  
這篇文章主要介紹了Springboot+jwt實(shí)現(xiàn)在線用戶功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

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就是過期了
            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就是過期了
            if(flag){
                //移除
                countMap.remove(entry.getKey());
            }else{
                userIdList.add(entry.getKey());
            }
        }
        return userIdList;
    }
}

2.定義一個(gè)攔截器,主要在驗(yàn)證通過攔截器的時(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是否過期

/*
    * 根據(jù)token判斷是否過期
    * */
    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();//獲取過期時(shí)間
            long diffTime = timeoutDate.getTime() - new Date().getTime();//獲取過期時(shí)間與當(dāng)前時(shí)間的時(shí)間差(毫秒)
            System.out.println("過期時(shí)間與當(dāng)前時(shí)間的時(shí)間差(毫秒):"+diffTime);
            if(diffTime < 0){
                //已過期
                return true;
            }else{
                //未過期
                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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java開發(fā)中常用記錄

    Java開發(fā)中常用記錄

    這篇文章主要介紹了Java-編程式事務(wù)、Java-Stream、Linux常用命令,需要的朋友可以參考下
    2023-05-05
  • 教你如何將Springboot項(xiàng)目成功部署到linux服務(wù)器

    教你如何將Springboot項(xiàng)目成功部署到linux服務(wù)器

    這篇文章主要介紹了如何將Springboot項(xiàng)目成功部署到linux服務(wù)器上,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Java實(shí)現(xiàn)的日歷功能完整示例

    Java實(shí)現(xiàn)的日歷功能完整示例

    這篇文章主要介紹了Java實(shí)現(xiàn)的日歷功能,結(jié)合完整實(shí)例形式分析了Java日歷功能相關(guān)的日期時(shí)間獲取、計(jì)算、顯示等操作技巧,需要的朋友可以參考下
    2019-02-02
  • 使用自定義注解實(shí)現(xiàn)redisson分布式鎖

    使用自定義注解實(shí)現(xiàn)redisson分布式鎖

    這篇文章主要介紹了使用自定義注解實(shí)現(xiàn)redisson分布式鎖,具有很好的參考價(jià)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單汽車租賃系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • 深入了解Java語言中的并發(fā)性選項(xiàng)有何不同

    深入了解Java語言中的并發(fā)性選項(xiàng)有何不同

    這篇文章主要介紹了深入了解Java語言中的并發(fā)性選項(xiàng)有何不同,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下
    2019-06-06
  • 詳解JAVA里面獲取map的key和value的方法

    詳解JAVA里面獲取map的key和value的方法

    這篇文章主要介紹了詳解JAVA里面獲取map的key和value的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 基于Spring實(shí)現(xiàn)文件上傳功能

    基于Spring實(shí)現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Spring實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • SpringBoot如何配置tomcat access日志

    SpringBoot如何配置tomcat access日志

    access日志記錄了每一個(gè)HTTP請(qǐng)求的信息,包括請(qǐng)求的來源、請(qǐng)求的資源、響應(yīng)狀態(tài)碼等,常常用來做數(shù)據(jù)統(tǒng)計(jì)、性能監(jiān)控,比如通過分析訪問日志,可以發(fā)現(xiàn)性能瓶頸和優(yōu)化機(jī)會(huì),提升應(yīng)用的響應(yīng)速度等,這篇文章主要介紹了SpringBoot配置tomcat access日志,需要的朋友可以參考下
    2024-05-05
  • 詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)

    詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)

    這篇文章主要為大家詳細(xì)介紹了springboot的接口如何優(yōu)雅的接收時(shí)間類型參數(shù),文中為大家整理了三種常見的方法,希望對(duì)大家有一定的幫助
    2023-09-09

最新評(píng)論