这篇“springSession怎么通过Redis统计在线用户数量”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Spring
这篇“springSession怎么通过Redis统计在线用户数量”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“SpringSession怎么通过Redis统计在线用户数量”文章吧。
因为系统原先的逻辑是使用Spring Session加上Redis做的会话共享实现的单点登录,登录之后会在session设置一个key值表示用户已经登录过,同时重写httpservletRequestWrapper 设置remoteUser数据值
class RemoteUserRequestWrapper extends HttpServletRequestWrapper {String userCode;RemoteUserRequestWrapper(HttpServletRequest request) {super(request);this.userCode = (String) request.getSession().getAttribute(org.apache.commons.lang3.StringUtils.isBlank(sessionKeyName)?DEFAULT_SESSION_KEY_NAME:sessionKeyName);}@Overridepublic String getRemoteUser() {return userCode;}}
Spring Session缓存在redis里的数据
这个ssoLoginUser
key是自己登录时候设置的,根据业务修改,经过测试,在登出系统时候,session设置过期获取removeAttribute不能清redis里的key数据,所以只能在登出系统逻辑加上:
Set<String> keys = RedisUtils.redisTemplate.keys("spring:session:sessions:*");for(String key : keys){if(key.indexOf("expires")==-1){String s = (String)RedisUtils.redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser");if(request.getRemoteUser().equals(s)) {logger.info("loginusername:{}",s)RedisUtils.redisTemplate.opsForHash().delete(key, "sessionAttr:ssoLoginUser");}}}
进行数据统计:
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); List<Map<String,Object>> data = new ArrayList<Map<String, Object>>(); Set<String> keys = redisTemplate.keys("spring:session:sessions:*"); for(String key : keys){ if(key.indexOf("expires")==-1){ String s = (String)redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser"); if(StringUtils.isNotBlank(s)) { System.out.println(s); Map<String,Object> map = new HashMap<String,Object>(16); map.put("usercode", s); list.add(map); } } } return list;
pom.xml:
<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId><version>1.2.2.RELEASE</version><type>pom</type></dependency><dependency><groupId>biz.paluch.redis</groupId><artifactId>lettuce</artifactId><version>3.5.0.Final</version></dependency>
RedisUtils.java:
package com.common.utils.redis;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.WEB.context.ContextLoader;import org.springframework.web.context.WebApplicationContext;import java.util.Collection;import java.util.List;import java.util.Map;import java.util.concurrent.TimeUnit;public class RedisUtils {private RedisUtils() {}@SuppressWarnings("unchecked")public static RedisTemplate<String, Object> redisTemplate =ContextLoader.getCurrentWebApplicationContext().getBean(RedisTemplate.class);public static boolean expire(final String key, final long timeout) {return expire(key, timeout, TimeUnit.SECONDS);}public static boolean expire(final String key, final long timeout, final TimeUnit unit) {Boolean ret = redisTemplate.expire(key, timeout, unit);return ret != null && ret;}public static boolean del(final String key) {redisTemplate.delete(key);return true;}public static long del(final Collection<String> keys) {redisTemplate.delete(keys);return 0;}public static void set(final String key, final Object value) {redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES);}// 存储普通对象操作public static void set(final String key, final Object value, final long timeout) {redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);}public static Object get(final String key) {return redisTemplate.opsForValue().get(key);}// 存储Hash操作public static void hPut(final String key, final String hKey, final Object value) {redisTemplate.opsForHash().put(key, hKey, value);}public static void hPutAll(final String key, final Map<String, Object> values) {redisTemplate.opsForHash().putAll(key, values);}public static Object hGet(final String key, final String hKey) {return redisTemplate.opsForHash().get(key, hKey);}public static List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {return redisTemplate.opsForHash().multiGet(key, hKeys);}// 存储Set相关操作public static long sSet(final String key, final Object... values) {Long count = redisTemplate.opsForSet().add(key, values);return count == null ? 0 : count;}public static long sDel(final String key, final Object... values) {Long count = redisTemplate.opsForSet().remove(key, values);return count == null ? 0 : count;}// 存储List相关操作public static long lPush(final String key, final Object value) {Long count = redisTemplate.opsForList().rightPush(key, value);return count == null ? 0 : count;}public static long lPushAll(final String key, final Collection<Object> values) {Long count = redisTemplate.opsForList().rightPushAll(key, values);return count == null ? 0 : count;}public static long lPushAll(final String key, final Object... values) {Long count = redisTemplate.opsForList().rightPushAll(key, values);return count == null ? 0 : count;}public static List<Object> lGet(final String key, final int start, final int end) {return redisTemplate.opsForList().range(key, start, end);}}
以上就是关于“SpringSession怎么通过Redis统计在线用户数量”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。
--结束END--
本文标题: SpringSession怎么通过Redis统计在线用户数量
本文链接: https://lsjlt.com/news/355802.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0