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;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
@ -29,7 +27,7 @@ http {
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;
init_by_lua_file lua/initialize.lua;
upstream local {
server 127.0.0.1:8081;
}
@ -61,9 +59,7 @@ http {
upstream typesense {
server 127.0.0.1:8108;
}
init_by_lua_file lua/settings.lua;
server {
listen 443 ssl;
server_name unraid.amass.fun;
@ -263,7 +259,9 @@ http {
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法
# access_by_lua_file lua/authentication.lua;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
@ -337,28 +335,15 @@ http {
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 {
content_by_lua_file lua/profile.lua;
}
location /video {
access_by_lua_file lua/access.lua;
proxy_pass http://local;
location /api/v1/login {
default_type 'application/json; charset=utf-8';
content_by_lua_file lua/login.lua;
}
location /phtot_gallery {
proxy_pass http://local;
}
location ~ /api/v1/.*$ {
proxy_pass http://local;
}
@ -416,10 +401,6 @@ http {
root amass_blog;
}
location ^~ /index/ {
try_files $uri /index.html;
}
location /wechat {
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 = {"个人笔记"};
user_infomation = {

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

@ -1,21 +1,57 @@
local cjson = require "cjson"
local password_path = "password.txt"
ngx.req.read_body()
local body = ngx.req.get_body_data()
local json = cjson.decode(body)
local password = json["password"];
local result = {}
if (password == user_password) then
result.code = 0
result.message = "succuess"
local session = require"resty.session".start()
session.data.identify = "myself"
session.cookie.idletime = 120
if not body then
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say("No body found")
return
end
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()
else
result.code = 1
result.message = "faliure"
reply.status = -100
reply.message = "登录失败"
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"
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() {
if [ ! -d ${build_path} ]; then
mkdir ${build_path}
fi
/opt/Qt/Tools/CMake/bin/cmake \
-G Ninja \
-S ${base_path} \
-B ${build_path} \
${cmake_exe} -G Ninja -S ${base_path} -B ${build_path} \
-DCMAKE_BUILD_TYPE=Debug \
-DBOOST_ROOT=${libraries_root}/boost_1_85_0
}
@ -24,9 +27,7 @@ function build() {
if [ $? -ne 0 ]; then
exit 1
fi
/opt/Qt/Tools/CMake/bin/cmake \
--build ${build_path} \
--target all
${cmake_exe} --build ${build_path} --target all
if [ $? -ne 0 ]; then
exit 1
fi
@ -46,7 +47,7 @@ function deploy() {
nohup ./HttpServer >logs/HttpServer.log 2>&1 &"
}
function init(){
function init() {
scp -r /opt/Libraries/boost_1_85_0 root@amass.fun:/opt/Libraries/
}