一直對serssion的共享有着很大的疑惑,對於我如今的工做的地方,所在的部門,由於沒有前臺頁面,純屬後臺。因此,不會存在session的共享問題。可是出於好奇,也是內心的疑惑,今天也動手實驗了下。web
我用的spring-boot-starter-redis版本是1.3.8.RELEASE,spring-session-data-redis版本是1.2.1.RELEASE,這個jar包主要是爲了實現session共享作準備。redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>spring
添加application.properties文件json
#tomcat地址
server.port=8084
#redis地址
spring.redis.host=192.168.8.128
spring.redis.port=6379
spring.redis.timeout=20000tomcat
添加InitConfig類,讀取對應的application.propertiessession
@Configuration
@PropertySource({
"classpath:application.properties"
})
public class InitConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("/application.properties"));
return propertySourcesPlaceholderConfigurer;
}
}mvc
添加WebConfig類,配置springmvcapp
@Configuration
@EnableWebMvc
@ComponentScan(basePackages={"com.zd.testdata.web.controller"},useDefaultFilters = false, includeFilters = @ComponentScan.Filter(value = {Controller.class, ControllerAdvice.class}))
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
private ObjectMapper objectMapper;
@Bean
public FilterRegistrationBean characterEncodingFilterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
registrationBean.setFilter(characterEncodingFilter);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
@Bean
public ServletRegistrationBean mvcDispatcherRegistration() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
dispatcherServlet.setApplicationContext(applicationContext);
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(dispatcherServlet, "/");
servletRegistrationBean.setName("springMvc");
return servletRegistrationBean;
}
protected MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
jsonConverter.setObjectMapper(objectMapper);
return jsonConverter;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customJackson2HttpMessageConverter());
converters.add(new StringHttpMessageConverter());
}
@Override
public void addFormatters(FormatterRegistry registry) {
super.addFormatters(registry);
DateFormatter dateFormatter = new DateFormatter("yyyy-MM-dd HH:mm:ss");
dateFormatter.setLenient(true);
registry.addFormatter(dateFormatter);
}
}ide
添加controller
@RestController
@RequestMapping(value = "/admin/v1")
public class RedisController {
@RequestMapping(value = "/first", method = RequestMethod.GET)
public Map<String, Object> firstResp (HttpServletRequest request){
Map<String, Object> map = new HashMap<String,Object>();
request.getSession().setAttribute("request Url", request.getRequestURL());
map.put("request Url", request.getRequestURL());
return map;
}
@RequestMapping(value = "/sessions", method = RequestMethod.GET)
public Object sessions (HttpServletRequest request){
Map<String, Object> map = new HashMap<String,Object>();
map.put("sessionId", request.getSession().getId());
map.put("message", request.getSession().getAttribute("map"));
return map;
}
} spring-boot
啓動兩份項目的tomcat,端口分別是8081和8084,訪問
http://localhost:8081/admin/v1/sessions
http://localhost:8084/admin/v1/sessions
返回均是
{"sessionId":"894ed7c3-45a6-4437-a539-51a7ba946992","message":null}
至此,經過redis共享session就此結束。很簡短。也只是實現了,歡迎你們斧正。