现在都比较流行使用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
- @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);
- }
- 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,