{"id":26991655,"url":"https://github.com/ihaolin/session","last_synced_at":"2025-07-09T03:04:12.517Z","repository":{"id":57736573,"uuid":"48570416","full_name":"ihaolin/session","owner":"ihaolin","description":"轻量的分布式会话组件(A Lightweight Distributed Session Component)","archived":false,"fork":false,"pushed_at":"2015-12-25T07:04:29.000Z","size":657,"stargazers_count":29,"open_issues_count":0,"forks_count":25,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-02-16T13:50:14.874Z","etag":null,"topics":["distributed-session","redis","sessionfilter"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ihaolin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-25T07:00:33.000Z","updated_at":"2024-02-16T13:50:14.875Z","dependencies_parsed_at":"2022-08-24T03:01:09.234Z","dependency_job_id":null,"html_url":"https://github.com/ihaolin/session","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihaolin%2Fsession","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihaolin%2Fsession/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihaolin%2Fsession/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ihaolin%2Fsession/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ihaolin","download_url":"https://codeload.github.com/ihaolin/session/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247086023,"owners_count":20881160,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed-session","redis","sessionfilter"],"created_at":"2025-04-03T22:16:27.387Z","updated_at":"2025-04-03T22:16:27.905Z","avatar_url":"https://github.com/ihaolin.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Session\n\n轻量的分布式会话组件(A Lightweight Distribute Session Component)\n---\n\n+ 包引入(maven dependency):\n\t\n\t```xml\n\t\u003cdependency\u003e\n        \u003cgroupId\u003eme.hao0\u003c/groupId\u003e\n        \u003cartifactId\u003esession-redis\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.0\u003c/version\u003e\n    \u003c/dependency\u003e\n\t```\n\t\n+ 依赖包，注意引入项目时是否需要排除(exclude if necessary):\n\n\t```xml\n\t\u003cdependency\u003e\n        \u003cgroupId\u003eme.hao0\u003c/groupId\u003e\n        \u003cartifactId\u003esession-api\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.0\u003c/version\u003e\n    \u003c/dependency\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003eredis.clients\u003c/groupId\u003e\n        \u003cartifactId\u003ejedis\u003c/artifactId\u003e\n        \u003cversion\u003e2.7.2\u003c/version\u003e\n    \u003c/dependency\u003e\n\t\n\t```\n\n+ Servlet-Filter机制(Mechanism)\n\t\n\t![](filter-chain.png)\n\n+ SessionFilter如何工作(How SessionFilter Works):\n\t\n\t![](how-session-filter-works.png)\n\t\t\n+ 使用(Usage):\n\t\n\t+ 在**web.xml**中配置Filter(Config Filter in **web.xml**):\n\n\t\t```xml\n\t\t\u003cfilter\u003e\n\t        \u003cfilter-name\u003eRedisSessionFilter\u003c/filter-name\u003e\n\t        \u003cfilter-class\u003eme.hao0.session.redis.RedisSessionFilter\u003c/filter-class\u003e\n\t        \u003cinit-param\u003e\n\t        \t  \u003c!-- cookie中的sessionId名称 --\u003e\n\t        \t  \u003c!-- the session id in cookie --\u003e\n\t            \u003cparam-name\u003esessionCookieName\u003c/param-name\u003e\n\t            \u003cparam-value\u003escn\u003c/param-value\u003e\n\t        \u003c/init-param\u003e\n\t        \u003cinit-param\u003e\n\t        \t  \u003c!-- session过期时间(秒) --\u003e\n\t        \t  \u003c!-- session expired time in seconds --\u003e\n\t            \u003cparam-name\u003emaxInactiveInterval\u003c/param-name\u003e\n\t            \u003cparam-value\u003e1800\u003c/param-value\u003e\n\t        \u003c/init-param\u003e\n\t        \u003cinit-param\u003e\n\t        \t  \u003c!-- cookie上下文路径 --\u003e\n\t        \t  \u003c!-- cookie context path --\u003e\n\t            \u003cparam-name\u003ecookieContextPath\u003c/param-name\u003e\n\t            \u003cparam-value\u003e/\u003c/param-value\u003e\n\t        \u003c/init-param\u003e\n\t        \u003cinit-param\u003e\n\t        \t  \u003c!-- cookie域名 --\u003e\n\t        \t  \u003c!-- cookie domain --\u003e\n\t            \u003cparam-name\u003ecookieDomain\u003c/param-name\u003e\n\t            \u003cparam-value\u003esession.com\u003c/param-value\u003e\n\t        \u003c/init-param\u003e\n\t        \u003cinit-param\u003e\n\t        \t  \u003c!-- cookie保存时间(秒) --\u003e\n\t        \t  \u003c!-- cookie lifetime in seconds --\u003e\n\t            \u003cparam-name\u003ecookieMaxAge\u003c/param-name\u003e\n\t            \u003cparam-value\u003e1800\u003c/param-value\u003e\n\t        \u003c/init-param\u003e\n    \u003c/filter\u003e\n    \u003cfilter-mapping\u003e\n\t        \u003cfilter-name\u003eRedisSessionFilter\u003c/filter-name\u003e\n\t        \u003curl-pattern\u003e/*\u003c/url-pattern\u003e\n    \u003c/filter-mapping\u003e\n\t\t```\n\t+ 在**classpath**下配置**session.properties**(Config **session.properties** in **classpath**)\n\t\n\t\t```ruby\n\t\t# redis model: is sentinel or not, default is not sentinel\n\t\t# session.redis.mode=\t\n\t\t\n\t\t# session serialize class, default is JsonSerializer\n\t\t# session.serializer=\n\t\t\n\t\t# redis host\n\t\tsession.redis.host=localhost\n\t\t\n\t\t# redis port\n\t\tsession.redis.port=6379\n\t\t\n\t\t# redis max connections, default is 5\n\t\t# session.redis.pool.max.total=5\n\t\t\n\t\t# redis max idle connections, default is 2\n\t\t# session.redis.pool.max.idle=2\n\t\t\n\t\t# redis session id prefix\n\t\tsession.redis.prefix=sid\n\t\t``` \n\n+ Demo:\n\t\n\t+ 配置**/etc/hosts**(config **/etc/hosts**):\n\n\t\t```ruby\n\t\t127.0.0.1 demo1.session.com demo2.session.com\n\t\t```\n\n\t+ 假如使用**nginx**作web代理，配置server(config server if use **nginx**):\n\n\t\t```ruby\n\t\tupstream session_server {\n\t\t    server localhost:10000;\n\t\t    server localhost:10001;\n\t\t}\n\n\t\tserver {\n\t\t    listen 80;\n\t\t    server_name demo1.session.com demo2.session.com;\n\t\t\n\t\t    location / {\n\t\t       proxy_pass \thttp://session_server;\n\t\t       proxy_set_header   X-Real-IP $remote_addr;\n\t\t       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;\n\t\t       proxy_set_header   Host $http_host;\n\t\t    }\n\t\t}\n\t\t```\n\t\t\n\t+ 运行**session-demo**(run **session-demo**):\n\n\t\t```bash\n\t\t# demo1.session.com\n\t\tmvn clean jetty:run -Dmaven.test.skip -Djetty.port=10000 \n\t\t# demo2.session.com\n\t\tmvn clean jetty:run -Dmaven.test.skip -Djetty.port=10001 \n\t\t```\n\t\n\t+ 测试(Test):\n\t\t\n\t\t+ 用户首页(user index):\n\n\t\t\t```bash\n\t\t\thttp://demo1.session.com/users/index\n\t\t\thttp://demo2.session.com/users/index\n\t\t\t```\n\t\t\t\n\t\t+ 用户登录(user login):\n\n\t\t\t```bash\n\t\t\thttp://demo1.session.com/users/login?username=admin\u0026passwd=admin\n\t\t\thttp://demo2.session.com/users/login?username=admin\u0026passwd=admin\n\t\t\t```\t\t\t\t\n\t\t\n\t\t+ 用户登出(user logout):\n\n\t\t\t```bash\n\t\t\thttp://demo1.session.com/users/logout\n\t\t\thttp://demo2.session.com/users/logout\n\t\t\t```\t\n\n+ 使用其它的存储，如**Memcache**(Use other storage, **Memcache**, etc):\n\n\t1. 继承**SessionFilter**(inherit **SessionFilter**);\n\t2. 实现**SessionManager**(implement **SessionManager**)。\n\t\n+ 相关文档(References):\n\t\n\t+ [FilterChain机制(Filter Chain Mechanism)](http://otndnld.oracle.co.jp/document/products/as10g/101300/B25221_03/web.1013/b14426/filters.htm#BCFJCEFC)；\n\n\n+ 历史版本(Release History):\n\n\t+ 1.0.0:\n\t\t\n\t\t+ 基本功能实现(basic implementation)。\t\n+ 你是好人:\n\n\t+ 倘若你钱多人傻花不完，小弟乐意效劳😊，掏出你的**微信神器**做回好人吧:\n\t\t\n\t\t\u003cimg src=\"wechat.png\" width=\"200\"\u003e\n\t\n\t+ 倘若你还不够尽兴，继续掏出你的**支付宝神器**，疯狂扫吧:\n\n\t\t\u003cimg src=\"alipay.png\" width=\"200\"\u003e\n        ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihaolin%2Fsession","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fihaolin%2Fsession","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fihaolin%2Fsession/lists"}