在spring mvc中利用spring-session+redis實現分布式session


背景

  平時部署項目過程中可能會遇到一個程序發布到多台服務器上,通過Nginx代理請求的情況,那么某個用戶在一台服務器上登錄成功過后,就不用在其他服務器上再登錄了,這個時候就需要用到今天說的東西了。

1、maven包添加

 1 <dependency>
 2     <groupId>org.springframework.session</groupId>
 3     <artifactId>spring-session-data-redis</artifactId>
 4 <version>1.2.1.RELEASE</version>
 5 </dependency>
 6 
 7 <dependency>
 8 <groupId>org.springframework.session</groupId>
 9     <artifactId>spring-session</artifactId>
10     <version>1.2.1.RELEASE</version>
11 </dependency>

2、在spring-redis.xml中添加,其中重寫RedisSerializer文件,可以點擊這里查看

 1 <!-- spring session -->
 2     <bean id="stringRedisSerializer"
 3         class="org.springframework.data.redis.serializer.StringRedisSerializer" />
 4     <bean id="GenericToStringSerializer"
 5         class="centaline.small.course.tool.GenericFastJson2JsonRedisSerializer" />
 6     <bean id="redisHttpSessionConfiguration"
 7         class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
 8         <property name="maxInactiveIntervalInSeconds" value="28800"></property>
 9         <property name="redisNamespace" value="XX(自定義名稱)" />
10     </bean>
11     <bean id="springSessionDefaultRedisSerializer"
12         class="xx.xx.xx.xx.xx (重寫RedisSerializer文件路徑)" />
13 
14     <bean id="defaultCookieSerializer"  
15         class="org.springframework.session.web.http.DefaultCookieSerializer">  
16         <property name="cookieName" value="XX_SESSION_ID(自己項目的sessionID名稱)" />  
17         <property name="cookiePath" value="/" />  
18     </bean>  
19            
20      <bean id="cookieHttpSessionStrategy"  
21         class="org.springframework.session.web.http.CookieHttpSessionStrategy">  
22         <property name="cookieSerializer" ref="defaultCookieSerializer" />  
23     </bean> 

3、在登錄成功后設置session和平時一樣,附上工具類。

 1 public class SessionUtils {
 2 
 3     @Resource(name = "sessionRepository")
 4     private SessionRepository<ExpiringSession> sessionRepository;
 5 
 6     private static final String SESSION_NAME = "LOGIN_INFO_SESSION";
 7 
 8     @Autowired
 9     private HttpSession httpSession;
10 
11     public String getSessionId() {
12         String sessionId = httpSession.getId();
13         return sessionId;
14     }
15 
16     public LoginInfo getLoginInfo() {
17         LoginInfo loginInfo = (LoginInfo) httpSession.getAttribute(SESSION_NAME);
18         return loginInfo;
19     }
20 
21     public String setLoginInfo(LoginInfo loginInfo) {
22         String sessionId = httpSession.getId();
23         httpSession.setAttribute(SESSION_NAME, loginInfo);
24         return sessionId;
25     }
26 
27     public void logout() {
28         httpSession.removeAttribute(SESSION_NAME);
29     }
30 
31     public LoginInfo getLoginInfoBySessionId(String sessionId) {
32         ExpiringSession expiringSession = sessionRepository.getSession(sessionId);
33         LoginInfo loginInfo = (LoginInfo) expiringSession.getAttribute(SESSION_NAME);
34         return loginInfo;
35     }
36 }
SessionUtils

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM