在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bungle/lua-resty-session开源软件地址(OpenSource Url):https://github.com/bungle/lua-resty-session开源编程语言(OpenSource Language):Lua 99.9%开源软件介绍(OpenSource Introduction):lua-resty-sessionlua-resty-session is a secure, and flexible session library for OpenResty. Hello World with lua-resty-sessionworker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
default_type text/html;
location / {
content_by_lua '
ngx.say("<html><body><a href=/start>Start the test</a>!</body></html>")
';
}
location /start {
content_by_lua '
local session = require "resty.session".start()
session.data.name = "OpenResty Fan"
session:save()
ngx.say("<html><body>Session started. ",
"<a href=/test>Check if it is working</a>!</body></html>")
';
}
location /test {
content_by_lua '
local session = require "resty.session".open()
ngx.say("<html><body>Session was started by <strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/modify>Modify the session</a>.</body></html>")
';
}
location /modify {
content_by_lua '
local session = require "resty.session".start()
session.data.name = "Lua Fan"
session:save()
ngx.say("<html><body>Session modified. ",
"<a href=/modified>Check if it is modified</a>!</body></html>")
';
}
location /modified {
content_by_lua '
local session = require "resty.session".open()
ngx.say("<html><body>Session was modified by <strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/destroy>Destroy the session</a>.</body></html>")
';
}
location /destroy {
content_by_lua '
require "resty.session".destroy()
ngx.say("<html><body>Session was destroyed. ",
"<a href=/check>Is it really so</a>?</body></html>")
';
}
location /check {
content_by_lua '
local session = require "resty.session".open()
ngx.say("<html><body>Session was really destroyed, you are known as ",
"<strong>",
session.data.name or "Anonymous",
"</strong>! <a href=/>Start again</a>.</body></html>")
';
}
}
} InstallationJust place Using OpenResty Package Manager (opm)$ opm get bungle/lua-resty-session Using LuaRocks$ luarocks install lua-resty-session LuaRocks repository for About The Defaults
The data part is encrypted with AES-algorithm (by default it uses OpenSSL Session identifier length is by default 16 bytes (randomly generated data with OpenSSL
Cookie parts are encoded with cookie safe Base64 encoding without padding (we also support pluggable
encoders). Before encrypting and encoding the data part, the data is serialized with JSON encoding
(so you can use basic Lua types in data, and expect to receive them back as the same Lua types).
JSON encoding is done by the bundled OpenResty cJSON library (Lua cJSON). We do support pluggable
serializers as well, though only serializer currently supplied is JSON. Cookie's path scope is by
default For session data we do support pluggable storage adapters. The default adapter is Notes About Turning Lua Code Cache OffIn issue (#15) it was raised that there may
be problems of using Nginx: lua_code_cache off; The problem is caused by the fact that by default we do generate session secret automatically with a random generator (on first use of the library). If the code cache is turned off, we regenerate the secret on each request. That will invalidate the cookies aka making sessions non-functioning. The cure for this problem is to define the secret in Nginx or in Lua code (it is a good idea to always have session secret defined). Nginx: set $session_secret 623q4hR325t36VsCD3g567922IC0073T; Lua: local session = require "resty.session".start{ secret = "623q4hR325t36VsCD3g567922IC0073T" }
-- or
local session = require "resty.session".new()
session.secret = "623q4hR325t36VsCD3g567922IC0073T" About LockingWith some storage adapters we implement Pluggable Session StrategiesStrategies can be a bit cumbersome to do with just configuration, and that's why you can
implement them only with the code. Currently
The Strategy can be selected with configuration (if no configuration is present, the set $session_strategy regenerate; To implement a custom strategy, please checkout the existing ones. Basically you need to implement at least these functions:
Pluggable HMAC AlgorithmsIf your strategy happens to be using HMAC can be selected with configuration (if no configuration is present, the set $session_hmac sha1; To implement your own, you need to implement this interface: Pluggable Storage AdaptersWith version 2.0 we started to support pluggable session data storage adapters. We do currently have support for these backends:
Here are some comparisons about the backends:
¹ Can be configured lock-less. ² HMAC is stored on a client but the data is stored on a server. That means that you are unable to edit cookie if you cannot edit server side storage as well, and vice-versa. The storage adapter can be selected from Nginx config like this: set $session_storage shm; Or with Lua code like this: local session = require "resty.session".new() -- OR .open() | .start()
-- After new you cannot specify storage as a string,
-- you need to give actual implementation
session.storage = require "resty.session.storage.shm".new(session)
-- or
local session = require "resty.session".new({
storage = "shm"
}) Cookie Storage AdapterCookie storage adapter is the default adapter that is used if storage adapter has not been configured. Cookie adapter does not have any settings. Cookie adapter can be selected with configuration (if no configuration is present, the cookie adapter is picked up): set $session_storage cookie; NOTE: If you store large amounts of data in a cookie, this library will automatically split the cookies to 4k chars chunks. With large cookies, you may need to adjust your Nginx configuration to accept large client header buffers. E.g.: large_client_header_buffers 4 16k; Shared Dictionary Storage AdapterShared dictionary uses OpenResty shared dictionary and works with multiple worker processes, but it isn't a good
choice if you want to run multiple separate frontends. It is relatively easy to configure and has some added
benefits on security side compared to Shared dictionary adapter can be selected with configuration: set $session_storage shm; But for this to work, you will also need a storage configured for that: http {
lua_shared_dict sessions 10m;
} Additionally you can configure the locking and some other things as well: set $session_shm_store sessions;
set $session_shm_uselocking on;
set $session_shm_lock_exptime 30; # (in seconds)
set $session_shm_lock_timeout 5; # (in seconds)
set $session_shm_lock_step 0.001; # (in seconds)
set $session_shm_lock_ratio 2;
set $session_shm_lock_max_step 0.5; # (in seconds) The keys stored in shared dictionary are in form:
Memcache Storage AdapterMemcache storage adapter stores the session data inside Memcached server. It is scalable and works with web farms. Memcache adapter can be selected with configuration: set $session_storage memcache; Additionally you can configure Memcache adapter with these settings: set $session_memcache_prefix sessions;
set $session_memcache_connect_timeout 1000; # (in milliseconds)
set $session_memcache_send_timeout 1000; # (in milliseconds)
set $session_memcache_read_timeout 1000; # (in milliseconds)
set $session_memcache_socket unix:///var/run/memcached/memcached.sock;
set $session_memcache_host 127.0.0.1;
set $session_memcache_port 11211;
set $session_memcache_uselocking on;
set $session_memcache_spinlockwait 150; # (in milliseconds)
set $session_memcache_maxlockwait 30; # (in seconds)
set $session_memcache_pool_name sessions;
set $session_memcache_pool_timeout 1000; # (in milliseconds)
set $session_memcache_pool_size 10;
set $session_memcache_pool_backlog 10; The keys stored in Memcached are in form:
Redis Storage AdapterRedis storage adapter stores the session data inside Redis server. It is scalable and works with web farms. Redis adapter can be selected with configuration: set $session_storage redis; Additionally you can configure Redis adapter with these settings: set $session_redis_prefix sessions;
set $session_redis_database 0;
set $session_redis_connect_timeout 1000; # (in milliseconds)
set $session_redis_send_timeout 1000; # (in milliseconds)
set $session_redis_read_timeout 1000; # (in milliseconds)
set $session_redis_socket unix:///var/run/redis/redis.sock;
set $session_redis_host 127.0.0.1;
set $session_redis_port 6379;
set $session_redis_ssl off;
set $session_redis_ssl_verify off;
set $session_redis_server_name example.com; # for TLS SNI
set $session_redis_username username;
set $session_redis_password password;
set $session_redis_uselocking on;
set $session_redis_spinlockwait 150; # (in milliseconds)
set $session_redis_maxlockwait 30; # (in seconds)
set $session_redis_pool_name sessions;
set $session_redis_pool_timeout 1000; # (in milliseconds)
set $session_redis_pool_size 10;
set $session_redis_pool_backlog 10;
set $session_redis_cluster_name redis-cluster;
set $session_redis_cluster_dict sessions;
set $session_redis_cluster_maxredirections 5;
set $session_redis_cluster_nodes '127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006'; Note: To use luarocks install kong-redis-cluster
# OR
luarocks install lua-resty-redis-cluster
# OR install this manually https://github.com/steve0511/resty-redis-cluster The keys stored in Redis are in form:
DSHM Storage AdapterDSHM storage adapter stores the session data inside Distributed Shared Memory server based on Vertx and Hazelcast. It is scalable and works with web farms. The DSHM lua library and the DSHM servers should be installed conforming with the documentation here. DSHM adapter can be selected with configuration: set $session_storage dshm; Additionally you can configure DSHM adapter with these settings: set $session_dshm_region sessions;
set $session_dshm_connect_timeout 1000; # (in milliseconds)
set $session_dshm_send_timeout 1000; # (in milliseconds)
set $session_dshm_read_timeout 1000; # (in milliseconds)
set $session_dshm_host 127.0.0.1;
set $session_dshm_port 4321;
set $session_dshm_pool_name sessions;
set $session_dshm_pool_timeout 1000; # (in milliseconds)
set $session_dshm_pool_size 10;
set $session_dshm_pool_backlog 10; The keys stored in DSHM are in form:
The Implementing a Storage AdapterIt is possible to implement additional storage adapters using the plugin architecture in You need to implement APIs you need
The You have to place your adapter inside To configure session to use your adapter, you can do so with Nginx configuration (or in Lua code): # Just an example. Pull request for MySQL support is greatly welcomed.
set $session_storage mysql; Pluggable CiphersWith version 2.1 we started to support pluggable ciphers. We currently have support for these ciphers:
The cipher adapter can be selected from Nginx config like this: set $session_cipher aes; Or with Lua code like this: local session = require "resty.session".start{ cipher = "aes" } AES CipherAES Cipher uses AES adapter can be selected with configuration: set $session_cipher aes; Additionally you can configure Memcache adapter with these settings: set $session_aes_size 256;
set $session_aes_mode "cbc";
set $session_aes_hash "sha512";
set $session_aes_rounds 1; Here follows the description of each setting: size
mode
hash
rounds
None CipherNone cipher disables encryption of the session data. This can be handy if you want to debug things or want you session management as light as possible, or perhaps share the session data with some other process without having to deal with encryption key management. In general it is better to have encryption enabled in a production. None adapter can be selected with configuration: set $session_cipher none; There isn't any settings for None adapter as it is basically a no-op adapter. Implementing a Cipher AdapterIf you want to write your own cipher adapter, you need to implement these three methods:
If you do not use say salt or aad (associated data) in your cipher, you can ignore them.
If you don't use You have to place your adapter inside Pluggable SerializersCurrently we only support JSON serializer, but there is a plugin architecture that you can use to plugin your own serializer. The serializer is used to serialize session data in a form that can be later deserialized and stored in some of our supported storages. The supported serializer names are:
You need only to implement two functions to write an adapter:
You have to place your adapter inside To configure session to use your adapter, you can do so with Nginx configuration (or in Lua code): set $session_serializer json;
全部评论
专题导读
上一篇:NLua/KeraLua: C# Native bindings of Lua 5.4 (compatible with .NET/iOS/Mac/Androi ...发布时间:2022-08-16下一篇:ElunaLuaEngine/Eluna: Eluna Lua Engine © for WoW Emulators发布时间:2022-08-16热门推荐
热门话题
阅读排行榜
|
请发表评论