1 Star 3 Fork 1

凌康 / final-session

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

final-session

final-session 一个轻量级分布式session框架,它可以无限水平扩展你的集群。

轻巧、易于配置、低入侵是 final-session 的设计理念,她轻盈而美。

core

支持redis、数据库存储会话session,推荐使用redis存储方案。通过自定义生成不同集群ID,读写访问不同的redis集群,从而实现节点无限扩展,架构图如下:

集群架构图

依赖

<dependency>
    <groupId>top.lingkang</groupId>
    <artifactId>final-session</artifactId>
    <version>4.1.0</version>
</dependency>

注意,springboot3.0+ 版本内嵌的tomcat是javaEE9+,应该使用下面的依赖

<!--springboot 3.0 + 版本使用 或 javaEE 9+版本 -->
<dependency>
    <groupId>top.lingkang</groupId>
    <artifactId>final-session-jakarta</artifactId>
    <version>4.1.0</version>
</dependency>

solon:https://gitee.com/lingkang_top/final-session/tree/master/final-session-solon

<!-- solon 2.5.x + -->
<dependency>
    <groupId>top.lingkang</groupId>
    <artifactId>final-session-solon</artifactId>
    <version>4.1.0</version>
</dependency>

已经上传maven公共仓库,可直接引用:https://repo1.maven.org/maven2/top/lingkang/final-session

在spring中配置

继承FinalSessionConfigurerAdapter类进行配置。

@Order(-1995)
@Component
public class WebSessionConfig extends FinalSessionFilter {
    @Override
    protected void configurer(FinalSessionConfig config) {
        // 对项目进行配置
    }
}

提示:必须在所有过滤器的前

其他配置说明

1. 会话名称和会话ID

默认为cookie存储,名称为fs,会话id生成规则是UUID,可以定制

@Configuration
@Order(-1995)
public class WebSessionConfig extends FinalSessionFilter {
    @Override
    protected void configurer(FinalSessionConfig config) {
        // 对项目进行配置
        // 设置cookie的名称,默认session id存储在cookie中
        config.setSessionName("token");
        // 自定义session存活时长 单位:毫秒
        config.setMaxValidTime(19951995L);
        // 配置id生成规则
        config.setIdGenerate(new IdGenerate<HttpServletRequest>() {
            @Override
            public String generateId(HttpServletRequest request, FinalSessionConfig config) {
                // 自定义id的值,可以根据不同id前缀访问不同redis集群,从而实现集群无限扩展
                return UUID.randomUUID().toString();
            }
        });
    }
}

2. 自定义会话ID的获取方式(适用前后端分离)

当我们使用 单体应用+前后端分离 时,为识别当前会话是否登录,
我们可以实现 FinalSessionId 接口让id标识同时从请求头、请求参数、cookie中获取session id

@Configuration
@Order(-1995)
public class WebSessionConfig extends FinalSessionFilter {
    @Override
    protected void configurer(FinalSessionConfig config) {
        // 自定义ID的获取、设置方式
        properties.setSessionHandler(new FinalSessionHandler<HttpServletRequest, HttpServletResponse>() {
            @Override
            public String getSessionId(HttpServletRequest request, String name) {
                // 获取会话id的方式,可以通过 请求头、请求参数、cookie中获取
                // 从cookie中
                // 从参数中
                // 从请求头中
                return request.getHeader("token");// 这只是一个demo
            }

            @Override
            public void setSessionId(HttpServletResponse response, String name, String value) {
                // 前后端分离,可以不设置session id到cookie
            }
        });
    }
}

3. 会话的存储

通过存储于dbnosql中,实现分布式会话存储。

3.1 存储于redis中

集群共享会话:session存储于redis中

存储于数据库

集群共享会话:session存储于数据库中

final-session准备了许多可扩展接口,你可以尽情发挥你的创新想法!

其他

有问题issuesPR,也可以邮箱:ling-kang@qq.com

也能打赏支持我:
pay

MIT License Copyright (c) 2022 凌康 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

final-session 一个轻量级分布式session框架,它可以无限水平扩展你的集群,使集群会话变得简单。 展开 收起
Java
MIT
取消

发行版 (3)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/lingkang_top/final-session.git
git@gitee.com:lingkang_top/final-session.git
lingkang_top
final-session
final-session
master

搜索帮助