From 33a30da7b2795fc60ebab8e89760087f5dfdd3a0 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期二, 10 九月 2024 16:13:37 +0800 Subject: [PATCH] add:三端用户禁用后下线 --- src/main/java/com/mzl/flower/service/BaseService.java | 36 +++++++++++++++++++++++++++++++++++- 1 files changed, 35 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/BaseService.java b/src/main/java/com/mzl/flower/service/BaseService.java index 363b03c..94c2d4c 100644 --- a/src/main/java/com/mzl/flower/service/BaseService.java +++ b/src/main/java/com/mzl/flower/service/BaseService.java @@ -1,8 +1,10 @@ package com.mzl.flower.service; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.mzl.flower.base.cache.MarkupCacheClient; +import com.mzl.flower.base.cache.StringCacheClient; import com.mzl.flower.config.exception.ValidationException; import com.mzl.flower.config.security.SecurityUtils; import com.mzl.flower.constant.Constants; @@ -23,6 +25,10 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.security.oauth2.common.OAuth2AccessToken; +import org.springframework.security.oauth2.common.OAuth2RefreshToken; +import org.springframework.security.oauth2.provider.token.TokenStore; +import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.time.*; @@ -30,10 +36,10 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; import static java.time.format.DateTimeFormatter.BASIC_ISO_DATE; +@Service public class BaseService { @Autowired @@ -56,6 +62,16 @@ @Autowired private FlowerCategoryMapper flowerCategoryMapper; + + @Autowired + private StringCacheClient stringCacheClient; + + @Autowired + private TokenStore tokenStore; + + public static final String TOKEN_KEY = "TOKEN-KEY"; + + public static final String SEPARATOR = ":"; protected double getServiceFeeRate(List<FeeService> fees, Integer saleCount){ int sc = saleCount == null ? 0 : saleCount; @@ -631,4 +647,22 @@ return null; } + public void removeToken(String userId) { + String tokenCache = stringCacheClient.get(TOKEN_KEY + SEPARATOR + userId); + if (StringUtils.isNotBlank(tokenCache)) { + //强制删除token,下线 + if (StringUtils.isNotBlank(tokenCache) && StringUtils.isNotBlank(userId)) { + stringCacheClient.delete(TOKEN_KEY + SEPARATOR + userId); + String tokenValue = tokenCache.replace(OAuth2AccessToken.BEARER_TYPE, StrUtil.EMPTY).trim(); + OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue); + if (accessToken != null && StringUtils.isNotBlank(accessToken.getValue())) { + tokenStore.removeAccessToken(accessToken); + OAuth2RefreshToken refreshToken = accessToken.getRefreshToken(); + tokenStore.removeRefreshToken(refreshToken); + } + } + } + } + + } -- Gitblit v1.9.3