1.实现cookie登录。

This commit is contained in:
amass 2024-06-16 06:50:10 +00:00
parent cd1e47f34d
commit 702b4a3358
6 changed files with 88 additions and 62 deletions

View File

@ -2,9 +2,7 @@
user root; user root;
worker_processes 1; worker_processes 1;
#error_log logs/error.log; error_log logs/error.log info;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #pid logs/nginx.pid;
events { events {
@ -29,7 +27,7 @@ http {
gzip on; gzip on;
gzip_types application/octet-stream text/markdown text/plain application/json application/x-javascript text/css application/xml text/javascript application/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_types application/octet-stream text/markdown text/plain application/json application/x-javascript text/css application/xml text/javascript application/javascript application/x-httpd-php image/jpeg image/gif image/png;
init_by_lua_file lua/initialize.lua;
upstream local { upstream local {
server 127.0.0.1:8081; server 127.0.0.1:8081;
} }
@ -62,8 +60,6 @@ http {
server 127.0.0.1:8108; server 127.0.0.1:8108;
} }
init_by_lua_file lua/settings.lua;
server { server {
listen 443 ssl; listen 443 ssl;
server_name unraid.amass.fun; server_name unraid.amass.fun;
@ -264,6 +260,8 @@ http {
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法 ssl_prefer_server_ciphers on; #使用服务器端的首选算法
# access_by_lua_file lua/authentication.lua;
location / { location / {
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; proxy_set_header Upgrade $http_upgrade;
@ -337,26 +335,13 @@ http {
index index.html index.htm; index index.html index.htm;
} }
location /lua {
default_type text/html;
content_by_lua_file lua/helloworld.lua;
}
location = /api/login {
content_by_lua_file lua/login.lua;
}
location = /blog/profile { location = /blog/profile {
content_by_lua_file lua/profile.lua; content_by_lua_file lua/profile.lua;
} }
location /video { location /api/v1/login {
access_by_lua_file lua/access.lua; default_type 'application/json; charset=utf-8';
proxy_pass http://local; content_by_lua_file lua/login.lua;
}
location /phtot_gallery {
proxy_pass http://local;
} }
location ~ /api/v1/.*$ { location ~ /api/v1/.*$ {
@ -416,10 +401,6 @@ http {
root amass_blog; root amass_blog;
} }
location ^~ /index/ {
try_files $uri /index.html;
}
location /wechat { location /wechat {
proxy_pass http://local; proxy_pass http://local;
} }

View File

@ -0,0 +1,13 @@
local session, err, exists = require "resty.session".open()
if exists and session:get("authenticated") then
ngx.log(ngx.INFO, session:get("account"), " 访问")
else
local server = ""
if ngx.var.server_port == "80" then
server = ngx.var.host
else
server = ngx.var.host .. ":" .. ngx.var.server_port
end
local target_url = ngx.var.scheme .. "://" .. server .. ngx.var.request_uri
ngx.redirect('https://amass.fun/LoginPage?next=' .. ngx.escape_uri(target_url))
end

View File

@ -1,14 +0,0 @@
ngx.log(ngx.ERR, "cookie a:", ngx.var.cookie_a)
ngx.header['Set-Cookie'] = {'a=32; path=/ ', 'b=4; path=/'}
ngx.header['Content-Type'] = "text/html; charset=utf-8";
local session = require"resty.session".start()
ngx.log(ngx.ERR, "cookie a:", ngx.var.cookie_a)
session.data.name = "OpenResty Fan"
session.cookie.path = "/123"
session:save()
ngx.say("<p>123Hello,world 中国</p>")

9
Server/lua/settings.lua → Server/lua/initialize.lua Executable file → Normal file
View File

@ -1,3 +1,12 @@
require "resty.session".init({
cookie_http_only=true,
remember = true,
storage = "cookie",
remember_rolling_timeout = 3600,
})
app_version="0.0.1"
private_folder = {"个人笔记"}; private_folder = {"个人笔记"};
user_infomation = { user_infomation = {

60
Server/lua/login.lua Executable file → Normal file
View File

@ -1,21 +1,57 @@
local cjson = require "cjson" local cjson = require "cjson"
local password_path = "password.txt"
ngx.req.read_body() ngx.req.read_body()
local body = ngx.req.get_body_data() local body = ngx.req.get_body_data()
local json = cjson.decode(body)
local password = json["password"];
local result = {}
if (password == user_password) then if not body then
result.code = 0 ngx.status = ngx.HTTP_BAD_REQUEST
result.message = "succuess" ngx.say("No body found")
local session = require"resty.session".start() return
session.data.identify = "myself" end
session.cookie.idletime = 120
local ok, json_data = pcall(cjson.decode, body)
if not ok then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say("Invalid JSON")
return
end
local user_account = json_data.account
local user_password = json_data.password
local reply = {}
local file = io.open(password_path, "r")
if not file then
ngx.log(ngx.INFO, "无法打开文件: ", password_path)
reply.status = -1000
reply.message = "服务器错误,找不到 " .. password_path;
ngx.say(cjson.encode(reply))
return
end
local credentials = {}
for line in file:lines() do
local account, password = line:match("([^=]+)=([^=]+)")
if account and password then
credentials[account] = password
end
end
file:close()
local session = require "resty.session".start()
if credentials[user_account] == user_password then
reply.status = 0
reply.message = "登录成功"
session:set("account", user_account)
session:set("authenticated", true)
session:save() session:save()
else else
result.code = 1 reply.status = -100
result.message = "faliure" reply.message = "登录失败"
end end
ngx.say(cjson.encode(result));
ngx.say(cjson.encode(reply))

View File

@ -5,14 +5,17 @@ build_path=${base_path}/build
libraries_root="/opt/Libraries" libraries_root="/opt/Libraries"
server_location=/root/HttpServer server_location=/root/HttpServer
if command -v cmake >/dev/null 2>&1; then
cmake_exe=cmake
else
cmake_exe=/opt/Qt/Tools/CMake/bin/cmake
fi
function cmake_scan() { function cmake_scan() {
if [ ! -d ${build_path} ]; then if [ ! -d ${build_path} ]; then
mkdir ${build_path} mkdir ${build_path}
fi fi
/opt/Qt/Tools/CMake/bin/cmake \ ${cmake_exe} -G Ninja -S ${base_path} -B ${build_path} \
-G Ninja \
-S ${base_path} \
-B ${build_path} \
-DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_BUILD_TYPE=Debug \
-DBOOST_ROOT=${libraries_root}/boost_1_85_0 -DBOOST_ROOT=${libraries_root}/boost_1_85_0
} }
@ -24,9 +27,7 @@ function build() {
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
/opt/Qt/Tools/CMake/bin/cmake \ ${cmake_exe} --build ${build_path} --target all
--build ${build_path} \
--target all
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
exit 1 exit 1
fi fi
@ -46,7 +47,7 @@ function deploy() {
nohup ./HttpServer >logs/HttpServer.log 2>&1 &" nohup ./HttpServer >logs/HttpServer.log 2>&1 &"
} }
function init(){ function init() {
scp -r /opt/Libraries/boost_1_85_0 root@amass.fun:/opt/Libraries/ scp -r /opt/Libraries/boost_1_85_0 root@amass.fun:/opt/Libraries/
} }