主要解决

session共享不同步问题

使用redis存储session

pom

1
2
3
4
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
#方式
spring:
session:
store-type: redis
#redis连接信息
redis:
host: localhost
port: 6379
server:
servlet:
session:
#超时
timeout: 30m

整合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jhj.gulimall.auth.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableRedisHttpSession //整合redis session
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class GulimallAuthServerApplication {


public static void main(String[] args) {

SpringApplication.run(GulimallAuthServerApplication.class, args);
}

}

注意

如果想要将对象 作为session 存入redis中 需要将对象实现序列化(implements Serializable)

在每个服务中使用的对象要放到common 防止序列化与反序列化失败

实现下面可忽略上面注意

放到公共里面 或者用到的都放

  1. 默认发的令牌 作用域为当前域
  2. 使用json的序列化方式进行对象数据序列化
    在这里插入图片描述
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    package com.jhj.gulimall.auth.server.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.session.web.http.CookieSerializer;
    import org.springframework.session.web.http.DefaultCookieSerializer;

    @Configuration
    public class GulimallSessionConfig {

    @Bean
    // 作用域 和名字
    public CookieSerializer cookieSerializer(){

    DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
    //作用域 全域 父域名
    defaultCookieSerializer.setDomainName("gulimall.com");
    defaultCookieSerializer.setCookieName("GULISESSION");
    return defaultCookieSerializer;
    }


    @Bean
    //Json序列化
    public RedisSerializer<Object> springSessionDefaultRedisSerializer(){

    return new GenericJackson2JsonRedisSerializer();
    }
    }

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.jhj.gulimall.auth.server.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class LoginController {


@GetMapping("/login.html")
public void loginPage(HttpSession session){

HashMap<Object, Object> map = new HashMap<>();
map.put("userName","aaa");
map.put("password","123456");
session.setAttribute("user",map);
}

@GetMapping("/reg.html")
public void regPage(HttpSession session){

System.out.println(session.getAttribute("user"));
}
}

效果

在这里插入图片描述

作者声明

1
如有问题,欢迎指正!