简单描述session共享问题

我这里提供两种解决方案
一、使用spring-session框架
二、使用token代替session

两种方案开展起来都比较容易,个人推荐第二种。
不过今天我要讲解的是第一种。

1.引入pom

			<dependency>
                  <groupId>org.springframework.session</groupId>
                  <artifactId>spring-session-data-redis</artifactId>
            </dependency>
            <dependency>
                  <groupId>org.apache.commons</groupId>
                  <artifactId>commons-pool2</artifactId>
            </dependency>
            <dependency>
                  <groupId>redis.clients</groupId>
                  <artifactId>jedis</artifactId>
            </dependency>

2.yml文件加入redis配置

redis:
 hostname: 188.131.155.46
 port: 6379
 password: 123456

3.加入配置类

public class SessionConfig {
      // 冒号后的值为没有配置文件时,制动装载的默认值
      @Value("${redis.hostname:localhost}")
      String hostName;
      @Value("${redis.port:6379}")
      int port;
      @Value("${redis.password:123456}")
      String passWord;
 
      @Bean
      public JedisConnectionFactory connectionFactory() {
            JedisConnectionFactory connection = new JedisConnectionFactory();
            connection.setPort(port);
            connection.setHostName(hostName);
            connection.setPassword(passWord);
            return connection;
      }
}

实现原理:
就是当Web服务器接收到http请求后,当请求进入对应的Filter进行过滤,将原本需要由web服务器创建会话的过程转交给Spring-Session进行创建,本来创建的会话保存在Web服务器内存中,通过Spring-Session创建的会话信息可以保存第三方的服务中,如:redis,mysql等。Web服务器之间通过连接第三方服务来共享数据,实现Session共享!

Spring Session 是一个轻量级的会话管理框架,它基于 Spring Security 和 Java Servlet API,旨在提供跨域的会话管理解决方案,简化在分布式应用中管理用户会话的需求。Spring Session 的主要目标是: 1. **跨域会话共享**:Spring Session 允许你在不同的域(通常是指不同域名或端口)之间共享同一个用户的会话,这对于现代的微服务架构和API网关非常有用。 2. **持久化会话**:支持会话数据的持久化,即使用户关闭浏览器,也可以通过服务器存储会话信息,再次访问时能够恢复登录状态。 3. **可配置性**:提供了多种会话存储机制,包括内存、Redis、Memcached、数据库等,可以根据应用需求灵活选择。 4. **与Spring Security集成**:无缝集成到Spring Security框架中,可以轻松管理用户认证和授权。 5. **可扩展性**:Spring Session 提供了对Spring Cloud的集成,使得在分布式环境中会话管理更加容易。 使用 Spring Session 的关键步骤包括: - 配置一个会话工厂,指定会话存储机制。 - 创建一个HttpSessionListener来监听会话创建和销毁事件。 - 配置Spring Security以使用Spring Session进行会话管理。 以下是一个简单的配置示例: ```java @Configuration @EnableCaching @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private HttpSessionStrategy httpSessionStrategy; @Bean public HttpSessionStrategy httpSessionStrategy() { return new SaveOnlyIfNewHttpSessionStrategy(); } @Override protected void configure(HttpSecurity http) throws Exception { // ... http .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and() .sessionAuthenticationStrategy(sessionAuthenticationStrategy()); } @Bean public SaveOnlyIfNewHttpSessionStrategy sessionAuthenticationStrategy() { return new SaveOnlyIfNewHttpSessionStrategy(httpSessionStrategy); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值