search
HomeDatabaseRedisDetailed explanation of Redis implementation of Session sharing

Detailed explanation of Redis implementation of Session sharing

Redis implements session sharing

In the past few days, I have been working on such a small module for session sharing. After checking a lot of information, I feel that it is too confusing and I have never been able to find what I want. Almost all implementation methods are different from what I thought. Here, I will summarize how I use Redis to implement sessions. It is shared to facilitate future inquiries, and I hope it can provide some help to friends who have needs in this regard.

Related topic recommendations: php session (including pictures, texts, videos, cases)

Let’s talk first Let’s take a look at my development environment: nginx, redis, tomcat, use moven to build the project, and run the jetty server. So here, the following will also cover how to use maven to build a war package and deploy it to run on tomcat.

Redis is a key-value database. Saving and retrieving values ​​depends entirely on the key. I’m going to say a long sentence here because it’s original and professional. I won’t paste it here. I want to know more about it. For the official introduction, you can search by yourself.

Configuration in pom.xml:

<!-- redis -->  
<dependency>  
<groupId>redis.clients</groupId>  
<artifactId>jedis</artifactId>  
<version>2.8.1</version>  
</dependency>  
<dependency>  
<groupId>org.springframework.data</groupId>  
<artifactId>spring-data-redis</artifactId>  
<version>1.7.2.RELEASE</version>  
</dependency>

Configuration in applicationContext-redis.xml

<!-- redis 客户端配置 -->  
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
       <property name="maxTotal" value="${jedisPoolConfig.maxTotal}"/>  
       <property name="maxIdle" value="${jedisPoolConfig.maxIdle}"/>  
       <property name="maxWaitMillis" value="${jedisPoolConfig.maxWaitMillis}"/>  
       <property name="testWhileIdle" value="true"/>  
       <property name="testOnBorrow" value="false"/>  
       <property name="testOnReturn" value="false"/>  
   </bean>  
   <bean id="readJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
       <property name="hostName" value="${jedis.read.host}" />  
       <property name="port" value="${jedis.read.port}" />  
       <property name="password" value="${jedis.read.password}" />  
       <property name="timeout" value="${jedis.read.timeout}" />  
       <property name="database" value="${jedis.read.database}" />  
       <property name="poolConfig" ref="jedisPoolConfig" />  
   </bean>  
   <bean id="writeJedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
       <property name="hostName" value="${jedis.write.host}" />  
       <property name="port" value="${jedis.write.port}" />  
       <property name="password" value="${jedis.write.password}" />  
       <property name="timeout" value="${jedis.write.timeout}" />  
       <property name="database" value="${jedis.write.database}" />  
       <property name="poolConfig" ref="jedisPoolConfig" />  
   </bean>  
   <bean id="readRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
       <property name="connectionFactory" ref="readJedisConnectionFactory" />  
   </bean>  
   <bean id="writeRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
       <property name="connectionFactory" ref="writeJedisConnectionFactory" />  
   </bean>

After the configuration is completed, start the code implementation:

In LoginController:

The first step, introduce RedisTemplate

@Autowired  
@Qualifier("writeRedisTemplate")  
private StringRedisTemplate writeTemplate;

You only need to introduce writeRedisTemplate here. When logging in, it is only responsible for writing. Only when refreshing again, after passing the filter, do you need to read

The second step, normal login process, after successful login, the request also needs to save the session information

##The third step, set the cookie value, store the key value in redis as the key value for saving userSession information in the cookie, refresh the browser At this time, the filter can get the key value from the cookie, and then go to redis to get the corresponding value, that is, userSession

String domain = request.getServerName();  
       String cookieId=MD5Util.MD5Encode("uasLoginer", "UTF-8");  
       //生成token,用作session在redis存储中的key值      
       StringredisSessionKey= UUID.randomUUID().toString();  
       Cookie uasLoginer = new Cookie(cookieId, redisSessionKey);  
       if (domain.startsWith("uas.")) {  
    uasLoginer.setDomain(domain.substring(4,domain.length()));  
}else {  
    uasLoginer.setDomain(domain);  
}  
       uasLoginer.setMaxAge(60000);  
       uasLoginer.setPath("/");  
       response.addCookie(uasLoginer);

here Cookie cross-domain setDomain and setPath settings

The fourth step, store the userSession information into redis

The value written to redis in RedisTemplate must be of String type, and the userSession object needs to be converted into a Json string

userSessionString = JSON.toJSONString(userSession);

在转Json的时候,遇到问题,导入import com.alibaba.fastjson.JSON;一直失败,发现pom中没有依赖Json的关系,如果有遇到相同的问题,可以检查下在pom.xml中是否有关于json的依赖关系,没的话,在pom.xml中导入json的依赖关系,如下:

<dependency>  
    <groupId>net.sf.json-lib</groupId>  
    <artifactId>json-lib</artifactId>  
    <version>2.3</version>  
    <classifier>jdk15</classifier>  
</dependency>

写入redis的代码如下:

writeTemplate.opsForHash().put(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey,redisSessionKey, userSessionString);  
writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey, 1800L, TimeUnit.SECONDS);//设置redis中值的有效期

完成这一操作,用户的session信息已经存入到redis中,可在redis中查看是否存入。

第五步:进入页面后,刷新页面,请求会经过过滤器,在Filter.Java中读取redis的值并进行一些处理

在过滤器这里,就无法通过注解的方式引入redisTemplate,可以通过如下的方式引入:

BeanFactory beans = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());  
     StringRedisTemplate readTemplate = (StringRedisTemplate) beans.getBean("readRedisTemplate");  
     StringRedisTemplate writeTemplate = (StringRedisTemplate) beans.getBean("writeRedisTemplate");

过滤器从cookie中取出redis的key值,用readTemplate读出value值

String cookid=MD5Util.MD5Encode("uasLoginer", "UTF-8");  
Cookie[] cookies = req.getCookies();  
String redisSessionKey = "";  
if(cookies != null){  
    for (Cookie cookie : cookies) {  
        if(cookie.getName().equals(cookid)){  
            redisSessionKey = cookie.getValue() ;  
        }  
    }  
}  
UserSession userSession = null;  
String userSessionString = (String) readTemplate.boundHashOps(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey).get(redisSessionKey);  
if(null != userSessionString ){  
    @SuppressWarnings("static-access")  
    JSONObject obj = new JSONObject().fromObject(userSessionString);//将json字符串转换为json对象  
    userSession = (UserSession)JSONObject.toBean(obj,UserSession.class);  
    writeTemplate.expire(UasContants.REDIS_USER_SESSION_KEY+"_"+redisSessionKey, 1800L, TimeUnit.SECONDS);  
    request.getSession().setAttribute(UasContants.USER_SESSION, userSession);  
}  
if (userSession != null) {  
    chain.doFilter(req, res);  
    return;  
}else {  
    res.sendRedirect(UasContants.LOGIN_URL);  
    return;  
}

在这里,另外附上关于web.xml关于LoginFilter的配置,有需要的可以参考下:

<!-- Spring监听器 -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<filter>  
    <filter-name>loginFilter</filter-name>  
    <filter-class>com.sfbest.uas.filter.LoginFilter</filter-class>  
    <init-param>  
        <param-name>excludePaths</param-name>  
        <param-value>/login,/user/login,/user/auth</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>loginFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

按照上面的配置,就可以用redis实现session共享的功能,但我在开发的时候,遇到一个蛋疼的问题,在测试环境上,

把项目部署在两台tomcat服务器上的时候,cookie里一直存不进去redis的key值,单台可以存进去,经过长期的检测,

终于发现是nginx配置出的问题,引以为戒,深深的阴影。下面我贴出我正常运行时nginx的配置代码

upstream uassessiontest.d.com {  
        server 10.103.16.226:8088;  
        server 10.103.16.226:8089;  
        }  
       server {  
                 log_format  sf_uastest  '$remote_addr - $remote_user [$time_local] "$request" '  
                                        '$status $body_bytes_sent "$http_referer" '  
                                        '"$http_user_agent" $http_cookie';  
                listen 80;  
                server_name uassessiontest.d.com;  
                access_log /var/log/nginx/uassessiontest.log sf_uastest; 
  
                location / {  
                        rewrite ^/$ /uas/ break;  
                        proxy_pass http://uassessiontest.d.com;  
                }  
        }

红色的为当初少配的部分,这些部分是的作用是往浏览器端写入cookie值。

相关学习推荐:redis视频教程

The above is the detailed content of Detailed explanation of Redis implementation of Session sharing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
Redis: How It Acts as a Data Store and ServiceRedis: How It Acts as a Data Store and ServiceApr 24, 2025 am 12:08 AM

Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

Redis vs. Other Databases: A Comparative AnalysisRedis vs. Other Databases: A Comparative AnalysisApr 23, 2025 am 12:16 AM

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.

Redis's Role: Exploring the Data Storage and Management CapabilitiesRedis's Role: Exploring the Data Storage and Management CapabilitiesApr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis: Understanding NoSQL ConceptsRedis: Understanding NoSQL ConceptsApr 21, 2025 am 12:04 AM

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

Redis: Real-World Use Cases and ExamplesRedis: Real-World Use Cases and ExamplesApr 20, 2025 am 12:06 AM

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis: Exploring Its Features and FunctionalityRedis: Exploring Its Features and FunctionalityApr 19, 2025 am 12:04 AM

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

Is Redis a SQL or NoSQL Database? The Answer ExplainedIs Redis a SQL or NoSQL Database? The Answer ExplainedApr 18, 2025 am 12:11 AM

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis: Improving Application Performance and ScalabilityRedis: Improving Application Performance and ScalabilityApr 17, 2025 am 12:16 AM

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),