vue+axios+springboot+redis 实现session 共享

来源:互联网 发布:java 面试项目 编辑:程序博客网 时间:2024/06/06 01:17
太坑了!!!!
终于弄好了,分享给大家
步骤一:

     首先我们添加@EnableRedisHttpSession来开启spring session支持,配置如下:

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@Configuration@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400 * 30)public class RedisSessionConfig {@Beanpublic JedisConnectionFactory connectionFactory() {return new JedisConnectionFactory();}}


步骤二:

     在pom.xml文件中添加:

<dependency><groupId>org.springframework.session</groupId><artifactId>spring-session</artifactId></dependency><dependency><groupId>org.springframework.session</groupId><artifactId>spring-session-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>


步骤三:

     在application.properties中配置redis服务器的位置,在这里,我们就用本机:

spring:  session:    store-type: REDIS  redis:    host: localhost    port: 6379

步骤四:
     在axios加上:

import axios from 'axios';axios.defaults.withCredentials=true;//让ajax携带cookieVue.prototype.$axios = axios;


步骤五:

然后在后端接口方法上添加:

res.header('Access-Control-Allow-Origin', '*');res.header('Access-Control-Allow-Credentials', 'true');res.header('Access-Control-Allow-Methods', req.header('Access-Control-Request-Method'));res.header('Access-Control-Allow-Headers', req.header('Access-Control-Request-Headers'));

或者用spring4.2版本的注解:

@CrossOrigin


最后可以直接在方法取session里的值啦!!!!





原创粉丝点击