spring-session+redis,实现session共享

最近研究session共享,接触spring-session,特此记录!

1、所需jar包

spring-session-1.3.0.RELEASE.jar

spring-session-data-redis-1.3.0.RELEASE.jar

spring-data-redis-1.7.1.RELEASE.jar

2、配置文件

spring-session.xml




    
        
        
        
        
        
    

    
    
    

    
    
        
        
    

applicatonContext.xml




    

    


web.xml



  
    index.jsp
  

  
      contextConfigLocation
      classpath:config/applicatonContext.xml,classpath:config/spring-session.xml
  
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
  
      springSessionRepositoryFilter
      org.springframework.web.filter.DelegatingFilterProxy
  
  
      springSessionRepositoryFilter
      /*
  

  
    requestContextFilter
    org.springframework.web.filter.RequestContextFilter
  
  
    requestContextFilter
    /*
  
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
      encoding
      utf-8
    
    
      forceEncoding
      true
    
  

  
    encodingFilter
    /*
  
  
    org.springframework.web.context.request.RequestContextListener
  
  
    mvc-dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:config/spring-*.xml
    
    1
  
  
    mvc-dispatcher
    /
  
    

3、后台测试代码

package com.hnac.hzinfo.zyj;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class SessionTest {

    @RequestMapping("/setUrl")
    @ResponseBody
    public Map setUrl(HttpServletRequest request){
        request.getSession().setAttribute("url", request.getRequestURL());
        Map map = new HashMap<>();
        map.put("url", request.getRequestURL());
        return map;
    }

    @RequestMapping("/getSession")
    @ResponseBody
    public Map getSession(HttpServletRequest request){
        Map map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        map.put("url", request.getSession().getAttribute("url"));
        return map;
    }

}

session可在RedisDesktopManager查看。

以下几篇文章供参考:

https://www.jianshu.com/p/ece9ac8e2f81

https://blog.csdn.net/qq_15092079/article/details/81939799

https://blog.csdn.net/qq_39669058/article/details/90235990

 

你可能感兴趣的:(Accumulate)