1. 程式人生 > >spring-session和redis解決spring cloud中session不一致性問題

spring-session和redis解決spring cloud中session不一致性問題

       現在都比較流行使用spring boot來進行開發,使用spring cloud來搭建分散式。在搭建的時候會涉及到一個關鍵的問題,session統一的問題。使用zuul作為閘道器轉發來呼叫其他模組,zuul中的session和其他模組的session會不一致,同時如果是前後端分離,還存在跨域的問題下面會給出解決的方法。這樣會導致使用者登入時候,沒法儲存使用者的資訊,session會存在問題。解決的辦法採用的是spring-session和redis,關鍵點如下:

      1,引入spring-session和redis的包,閘道器和其他模組都需要映入:

<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
			<!--<version>2.0.0.RELEASE</version>-->
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
			<version>1.4.7.RELEASE</version>
		</dependency>
2,開啟spring-session和redis
      在spring boot的主類上開啟redis管理session的註解,閘道器和其他模組都需要開啟:
package com.jack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
@EnableRedisHttpSession
public class ZuulgatewayApplication {

	public static void main(String[] args) {
		SpringApplication.run(ZuulgatewayApplication.class, args);
	}

	/*@Bean
	public CorsFilter corsFilter() {
		final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		//config.addAllowedMethod("OPTIONS");
		//config.addAllowedMethod("HEAD");
		//config.addAllowedMethod("GET");
		//config.addAllowedMethod("PUT");
		//config.addAllowedMethod("POST");
		//config.addAllowedMethod("DELETE");
		//config.addAllowedMethod("PATCH");
		config.addAllowedMethod("*");
		source.registerCorsConfiguration("*//**", config);
	 return new CorsFilter(source);
	 }*/

}
   
@EnableRedisHttpSession

     上面的是開啟註解

   3,配置redis

      閘道器和其他模組都需要配置redis,如下:

spring:
  application:
    name: zuulgateway
  redis:
    host:  xxx.xxx.x.xx
    database: 8
    pool:
      max-active: 8
      min-idle: 1
      max-idle: 1
      max-wait: -1

  4,解決前後端分離跨域問題,後臺需要新增如下程式碼:
@Bean
	public CorsFilter corsFilter() {
		final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		final CorsConfiguration config = new CorsConfiguration();
		config.setAllowCredentials(true);
		config.addAllowedOrigin("*");
		config.addAllowedHeader("*");
		//config.addAllowedMethod("OPTIONS");
		//config.addAllowedMethod("HEAD");
		//config.addAllowedMethod("GET");
		//config.addAllowedMethod("PUT");
		//config.addAllowedMethod("POST");
		//config.addAllowedMethod("DELETE");
		//config.addAllowedMethod("PATCH");
		config.addAllowedMethod("*");
		source.registerCorsConfiguration("*//**", config);
	 return new CorsFilter(source);
	 }

   5,解決前後端分離,前端呼叫介面需要新增的引數:
var url = 'http://xxx.xx.xx.xx:7010/login';
			var param ={'username':'xxx','password':'xxxx'};
			$.ajax({
				type: 'POST',
				url: url,
				data: param,
				//dataType: "json",
				//contentType: 'application/json',
				 dataType: "json",
				// 允許攜帶證書
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				success: function(data) {
					console.log(data);
					if(data.code == 0) {
						//alert("登入成功");
						layer.alert(JSON.stringify(data), {
							title: '登入成功'
						});
						location.href = './html/wx.html';
					}
				},
				error: function(xhr, textStatus) {
					console.log("登入錯誤" + textStatus);
				}

			});
     解決跨域的關鍵程式碼:
// 允許攜帶證書
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
     
       通過上面關鍵的幾個步驟就解決了統一session的問題,並且解決了跨域的問題了,這裡沒有給出詳細的程式碼,如果需要參考詳細的程式碼,返回git地址:https://github.com/wj903829182/springcloud4/tree/master/zuulgateway

      學習交流歡迎加群:331227121

      後面的部落格會涉及到spring boot,spring security,oauth2.0,token,JWT等,歡迎點贊,關注,加群多交流