Spring Session Data Redis - 从 Redis Store 获取有效会话、当前用户 [英] Spring Session Data Redis - Get Valid Sessions, Current User from Redis Store

查看:271
本文介绍了Spring Session Data Redis - 从 Redis Store 获取有效会话、当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,在分布式 Web 应用程序中,是否可以使用 RedisOperationSessionRepository 从 Redis Store 获取有效会话.(我的意思是我不想编写显式代码将其放入 Redis 存储然后稍后阅读它,我想了解框架或 spring-data-redis 库是否提供).

我知道 Spring Redis 能够恢复会话,如果会话仍然有效(因为它由 Redis 支持),服务器重启也会保留登录名

我正在寻找的功能之一是获取当前在应用程序中所有可能的登录用户.我知道 SessionRegistryImpl 和这个方法.但是我注意到这个方法没有Redis支持,服务器重启后,登录用户没有返回.

 public ListgetAllPrincipals() {返回新的 ArrayList(principals.keySet());}

我可以尝试的功能之一是 Spring Session 1.1.0,Spring session 可以通过用户名查找.

  1. http://docs.spring.io/spring-session/docs/1.1.0.M1/reference/html5/guides/findbyusername.html
  2. https://spring.io/blog/2015/11/17/spring-session-1-1-0-m1-released

我试过了,它确实返回了有效的会话结果,但问题是我仍然需要知道使用此应用程序的所有当前有效用户名.(我不知道如何使用 Redis Store 获取它们,我再次可以将它们存储在 Redis 中并获取它们,但我想知道是否有更好的方法).

这是一段代码,如果我知道会话 ID,这是我可以从当前使用系统的众多用户之一中获取当前用户的方式.

 final Session session = redisOperationsSessionRepository.getSession(sessionid);最终对象 obj = session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);if (obj instanceof SecurityContext) {最终 SecurityContext 上下文 = (SecurityContext) obj;最终身份验证身份验证 = context.getAuthentication();如果(身份验证!= null){最终对象主体 = authentication.getPrincipal();if (principal != null && principal instanceof CurrentUser) {返回(当前用户)主体;}}}

现在我可以使用上面的逻辑来获取所有当前用户,但我又应该获取所有有效的会话 ID,我不知道如何从 Redis 存储中获取.

新更新:https://github.com/spring-projects/spring-session/issues/255在此链接中,我可能可以获取所有会话 ID 并在 RedisOperationSessionRepository 中查找活动会话,但可能会导致性能问题.

我不确定我是否说清楚了,但是我们不能使用 spring session api 告诉 Redis 什么,只需给我所有有效的会话及其当前登录的当前用户.(基于上次访问时间或类似的东西).

谢谢

解决方案

Redis 基本上是一个键值存储,不提供这样的查询功能.

但是,您应该能够使用 KEYS 请求列出所有会话,然后在最后一个活动的基础上过滤它们,但存在您提到的 github 问题中提到的缺点.

您可能应该考虑在支持关系查询的数据存储中记录用户活动,将 Redis 保持为快速会话存储.

My question is, in distributed web application is it possible to get the valid sessions from Redis Store using RedisOperationSessionRepository. (I mean I don't want to write explicit code for putting it into Redis store and then later read it, I want to understand if framework or spring-data-redis library provides that).

I am aware that Spring Redis is able to restore sessions and server restarts also preserve the login if the session is still valid (as it is backed by Redis)

One of the functionality I am looking for is to get all the possible log in users currently in the application. I am aware of SessionRegistryImpl, and this method. but what I noticed that this method is not backed by Redis and after server restarts, log in users are not returned.

 public List<Object> getAllPrincipals() {
    return new ArrayList<Object>(principals.keySet());
}

One of the functionality I can try is from Spring Session 1.1.0, Spring session find by username.

  1. http://docs.spring.io/spring-session/docs/1.1.0.M1/reference/html5/guides/findbyusername.html
  2. https://spring.io/blog/2015/11/17/spring-session-1-1-0-m1-released

I tried and it indeed returns me valid session result, but the problem is I still need to know all the current valid user names that are using this application. (I don't know how to get them using Redis Store, again I can store in Redis and get them, but I want to know if there is better approach).

This is the piece of code, this is the way I can get current user from one of the many users that are currently using the system, if I know the session id.

    final Session session = redisOperationsSessionRepository.getSession(sessionid);

    final Object obj = session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
    if (obj instanceof SecurityContext) {
        final SecurityContext context = (SecurityContext) obj;
        final Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            final Object principal = authentication.getPrincipal();
            if (principal != null && principal instanceof CurrentUser) {
                return (CurrentUser) principal;
            }
        }
    }

Now I can use above logic to get all the current user, but again I should all the valid session ids, which I don't know how to get from Redis store.

New Update : https://github.com/spring-projects/spring-session/issues/255 Here in this link, I can probably get all the session ids and look for active sessions in RedisOperationSessionRepository, but might result in performance issues.

I am not sure if I made myself clear, but can't we tell something to Redis using spring session api, just give me all the valid sessions and their current user that are currently log in. (based on last accessed time or something like that).

Thank you

解决方案

Redis is basically a key-value store and does not provide such querying features.

However, you should be able to list all session using a KEYS request, then filter them on a last-activity basis, but with drawbacks mentioned in the github issue you have mentioned.

You should probably consider logging users activities in a datastore that supports relational querying, keeping Redis as a fast session store.

这篇关于Spring Session Data Redis - 从 Redis Store 获取有效会话、当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆