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

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

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


			org.springframework.session
			spring-session-data-redis
			
		

		
			org.springframework.boot
			spring-boot-starter-redis
			1.4.7.RELEASE
		


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等,欢迎点赞,关注,加群多交流



你可能感兴趣的:(spring,boot)