Webflux 之 session timout 修改

 

背景

webflux 采用 gateway 集成的微服务 默认session.timeout=30min

但是修改yml配置文件之后 发现session.timout并不生效

server: 
    servler:
        session:
            timeout: PT10M

调试源码,发现时间参数是传过去了

package org.springframework.boot.web.servlet.server;
​
public class Session {
        public void setTimeout(Duration timeout) {
        this.timeout = timeout;
    }
}

继续调试发现执行了context.clearConfigurationProperty();但是不确定是不是这个原因导致yml失效

package org.springframework.boot.context.properties.bind;
​
class JavaBeanBinder implements DataObjectBinder {
        private <T> boolean bind(DataObjectPropertyBinder propertyBinder, Bean<T> bean, BeanSupplier<T> beanSupplier,
            Context context) {
        boolean bound = false;
        for (BeanProperty beanProperty : bean.getProperties().values()) {
            bound |= bind(beanSupplier, propertyBinder, beanProperty);
            // 发现timeout属性调用了这个方法,里面的value被清空
            context.clearConfigurationProperty();
        }
        return bound;
    }
}

寻找相应的方法修改

1、获取登出的WebSession查看管理的session对象,发现管理类InMemoryWebSessionStore

public class InMemoryWebSessionStore implements WebSessionStore {
    
      private class InMemoryWebSession implements WebSession {
          //默认值30min
          private volatile Duration maxIdleTime = Duration.ofMinutes(30);
          
          public InMemoryWebSession(Instant creationTime) {
                this.creationTime = creationTime;
                this.lastAccessTime = this.creationTime;
          }
              
          @Override
          public Duration getMaxIdleTime() {
                return this.maxIdleTime;
            }
      }
}
//此处找不到其他地方调用的方法,而且只有构造方法

2、通过过滤器获取相应的webSession

@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class SessionGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
​
  private static final Logger LOGGER = LoggerFactory.getLogger(SessionGatewayFilterFactory.class);
​
  @Value("${server.servlet.session.timeout}")     //配置文件读取数据
  private Long min;
​
  @Override
  public GatewayFilter apply(Object config) {
    return new innerFilter(config);
  }
​
​
  private class innerFilter implements GatewayFilter {
​
    private Object config;
​
    public innerFilter(Object config) {
      this.config = config;
    }
​
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
     //由于webflux是非阻塞式编程,切记此处采用非阻塞方式获取Mono里面的对象,采用block()会抛出异常
      exchange.getSession().doOnNext(res -> res.setMaxIdleTime(Duration.ofMinutes(min))).subscribe();   //修改session时间
      return chain.filter(exchange);
    }
  }
}

参考:如何在Reactive Java中从Mono流获取字符串/对象?https://www.bilibili.com/read/cv5787745/

3、yml加上filter

#服务器相关配置
spring:
  cloud:
    gateway:
      default-filters:
        - TokenRelay
        - Session
​
server:
  port: 28180
  servlet:
    session:
      timeout: 80
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值