spring-session和redis解决spring cloud中session不一致性问题

现在都比较流行使用spring boot来进行开发,使用spring cloud来搭建分布式。在搭建的时候会涉及到一个关键的问题,session统一的问题。使用zuul作为网关转发来调用其他模块,zuul中的session和其他模块的session会不一致,同时如果是前后端分离,还存在跨域的问题下面会给出解决的方法。这样会导致用户登入时候,没法保存用户的信息,session会存在问题。解决的办法采用的是spring-session和redis,关键点如下:

      1,引入spring-session和redis的包,网关和其他模块都需要映入:

[html]  view plain  copy
  1. <dependency>  
  2.             <groupId>org.springframework.session</groupId>  
  3.             <artifactId>spring-session-data-redis</artifactId>  
  4.             <!--<version>2.0.0.RELEASE</version>-->  
  5.         </dependency>  
  6.   
  7.         <dependency>  
  8.             <groupId>org.springframework.boot</groupId>  
  9.             <artifactId>spring-boot-starter-redis</artifactId>  
  10.             <version>1.4.7.RELEASE</version>  
  11.         </dependency>  

2,开启spring-session和redis

      在spring boot的主类上开启redis管理session的注解,网关和其他模块都需要开启:  
[java]  view plain  copy
  1. package com.jack;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;  
  6. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;  
  7. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
  8.   
  9. @SpringBootApplication  
  10. @EnableDiscoveryClient  
  11. @EnableZuulProxy  
  12. @EnableRedisHttpSession  
  13. public class ZuulgatewayApplication {  
  14.   
  15.     public static void main(String[] args) {  
  16.         SpringApplication.run(ZuulgatewayApplication.class, args);  
  17.     }  
  18.   
  19.     /*@Bean 
  20.     public CorsFilter corsFilter() { 
  21.         final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
  22.         final CorsConfiguration config = new CorsConfiguration(); 
  23.         config.setAllowCredentials(true); 
  24.         config.addAllowedOrigin("*"); 
  25.         config.addAllowedHeader("*"); 
  26.         //config.addAllowedMethod("OPTIONS"); 
  27.         //config.addAllowedMethod("HEAD"); 
  28.         //config.addAllowedMethod("GET"); 
  29.         //config.addAllowedMethod("PUT"); 
  30.         //config.addAllowedMethod("POST"); 
  31.         //config.addAllowedMethod("DELETE"); 
  32.         //config.addAllowedMethod("PATCH"); 
  33.         config.addAllowedMethod("*"); 
  34.         source.registerCorsConfiguration("*//**", config); 
  35.      return new CorsFilter(source); 
  36.      }*/  
  37.   
  38. }  
   
[java]  view plain  copy
  1. @EnableRedisHttpSession  

     上面的是开启注解

   3,配置redis

      网关和其他模块都需要配置redis,如下:

[java]  view plain  copy
  1. spring:  
  2.   application:  
  3.     name: zuulgateway  
  4.   redis:  
  5.     host:  xxx.xxx.x.xx  
  6.     database: 8  
  7.     pool:  
  8.       max-active: 8  
  9.       min-idle: 1  
  10.       max-idle: 1  
  11.       max-wait: -1  
  4,解决前后端分离跨域问题,后台需要添加如下代码:
[java]  view plain  copy
  1. @Bean  
  2.     public CorsFilter corsFilter() {  
  3.         final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();  
  4.         final CorsConfiguration config = new CorsConfiguration();  
  5.         config.setAllowCredentials(true);  
  6.         config.addAllowedOrigin("*");  
  7.         config.addAllowedHeader("*");  
  8.         //config.addAllowedMethod("OPTIONS");  
  9.         //config.addAllowedMethod("HEAD");  
  10.         //config.addAllowedMethod("GET");  
  11.         //config.addAllowedMethod("PUT");  
  12.         //config.addAllowedMethod("POST");  
  13.         //config.addAllowedMethod("DELETE");  
  14.         //config.addAllowedMethod("PATCH");  
  15.         config.addAllowedMethod("*");  
  16.         source.registerCorsConfiguration("*//**", config);  
  17.      return new CorsFilter(source);  
  18.      }  
   5,解决前后端分离,前端调用接口需要添加的参数:
[html]  view plain  copy
  1. var url = 'http://xxx.xx.xx.xx:7010/login';  
  2.             var param ={'username':'xxx','password':'xxxx'};  
  3.             $.ajax({  
  4.                 type: 'POST',  
  5.                 url: url,  
  6.                 data: param,  
  7.                 //dataType: "json",  
  8.                 //contentType: 'application/json',  
  9.                  dataType: "json",  
  10.                 // 允许携带证书  
  11.                 xhrFields: {  
  12.                     withCredentials: true  
  13.                 },  
  14.                 crossDomain: true,  
  15.                 success: function(data) {  
  16.                     console.log(data);  
  17.                     if(data.code == 0) {  
  18.                         //alert("登入成功");  
  19.                         layer.alert(JSON.stringify(data), {  
  20.                             title: '登入成功'  
  21.                         });  
  22.                         location.href = './html/wx.html';  
  23.                     }  
  24.                 },  
  25.                 error: function(xhr, textStatus) {  
  26.                     console.log("登入错误" + textStatus);  
  27.                 }  
  28.   
  29.             });  
     解决跨域的关键代码:
[html]  view plain  copy
  1. // 允许携带证书  
  2.                 xhrFields: {  
  3.                     withCredentials: true  
  4.                 },  
  5.                 crossDomain: true,  
         通过上面关键的几个步骤就解决了统一session的问题,并且解决了跨域的问题了,这里没有给出详细的代码,如果需要参考详细的代码,返回git地址: https://github.com/wj903829182/springcloud4/tree/master/zuulgateway

猜你喜欢

转载自blog.csdn.net/justlpf/article/details/80350897