更新新的登录示例
This commit is contained in:
@@ -1,4 +0,0 @@
|
|||||||
local dkjson = require("dkjson")
|
|
||||||
|
|
||||||
|
|
||||||
response:send("这是需要权限的接口!可以看到这条消息说明你拥有权限。")
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
print("init fast web success!")
|
|
||||||
-- 增加拦截器
|
|
||||||
interceptor.add("/api/*.*","/api/lib/interceptor.lua")
|
|
||||||
return true
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
require "website"
|
|
||||||
|
|
||||||
if string.sub(request:filepath(),1,12) == "/api/public/" then
|
|
||||||
--公共开放接口
|
|
||||||
return true
|
|
||||||
elseif string.sub(request:filepath(),1,11) == "/api/admin/" then
|
|
||||||
--管理员目录,需要验证权限
|
|
||||||
print(param("key"),"\t")
|
|
||||||
if param("key") == nil or param("key") ~= "123456" then
|
|
||||||
response:send("key不正确")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
return true
|
|
||||||
else
|
|
||||||
response:send("仅允许访问 /api/public 和 /api/admin 目录")
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
|
|
||||||
response:send("这是一个公共public接口,可以随意访问")
|
|
||||||
50
www/api/user.lua
Normal file
50
www/api/user.lua
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
require("api.website")
|
||||||
|
local dkjson = require("api.dkjson")
|
||||||
|
|
||||||
|
-- 登录
|
||||||
|
local function login()
|
||||||
|
-- 获取session
|
||||||
|
local session = session()
|
||||||
|
-- 验证session有效
|
||||||
|
if session:check() then
|
||||||
|
reply(201,"你已经登录过了")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 验证账号密码
|
||||||
|
if param("username") ~= "fastweb" or param("password") ~= "123456" then
|
||||||
|
reply(201,"账号或密码不正确")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 生成TOKEN
|
||||||
|
local token = make_software_guid()
|
||||||
|
session:init(request,token)
|
||||||
|
|
||||||
|
|
||||||
|
-- 填充用户信息
|
||||||
|
session:set("login_time",time.now_time("%Y-%m-%d %H:%M:%S"))
|
||||||
|
|
||||||
|
reply(200,"",{
|
||||||
|
token = token
|
||||||
|
})
|
||||||
|
end
|
||||||
|
-- 获取个人信息
|
||||||
|
local function getinfo()
|
||||||
|
local session = session()
|
||||||
|
if session:check() == false then
|
||||||
|
reply(401,"登录信息已过期")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
reply(200,"",{
|
||||||
|
login_time = session:get("login_time")
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
-- 判断请求类型
|
||||||
|
if param("action") == "login" then
|
||||||
|
login()
|
||||||
|
elseif param("action") == "getinfo" then
|
||||||
|
getinfo()
|
||||||
|
end
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
local dkjson = require 'dkjson'
|
local dkjson = require("api.dkjson")
|
||||||
|
|
||||||
function json(data)
|
function json(data)
|
||||||
response:header("Content-Type","application/json")
|
response:header("Content-Type","application/json")
|
||||||
response:send(dkjson.encode(data))
|
response:send(dkjson.encode(data))
|
||||||
end
|
end
|
||||||
function session()
|
function session()
|
||||||
return request:session(request:token())
|
local token = request:header("token")
|
||||||
|
return request:session(token)
|
||||||
end
|
end
|
||||||
function param(name)
|
function param(name)
|
||||||
return request:param(name,false)
|
return request:param(name,false)
|
||||||
152
www/index.html
152
www/index.html
@@ -1,65 +1,113 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="zh-CN">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Fast Web</title>
|
<title>用户登录</title>
|
||||||
<script src="js/jquery-3.4.1.min.js"></script>
|
|
||||||
<style>
|
<style>
|
||||||
.button-container {
|
body {
|
||||||
margin-bottom: 10px;
|
font-family: Arial, sans-serif;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
margin: 0;
|
||||||
}
|
}
|
||||||
.button-container button {
|
.login-container {
|
||||||
margin-right: 10px;
|
background-color: white;
|
||||||
|
padding: 30px;
|
||||||
|
border-radius: 10px;
|
||||||
|
box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
|
||||||
|
text-align: center;
|
||||||
|
width: 350px;
|
||||||
|
}
|
||||||
|
.login-container h1 {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
font-size: 24px;
|
||||||
|
}
|
||||||
|
.login-container input[type="text"], .login-container input[type="password"] {
|
||||||
|
width: 100%;
|
||||||
|
padding-top: 15px;
|
||||||
|
padding-bottom: 15px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 5px;
|
||||||
|
}
|
||||||
|
.login-container button {
|
||||||
|
width: 100%;
|
||||||
|
padding: 15px;
|
||||||
|
background-color: #007BFF;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
color: white;
|
||||||
|
font-size: 16px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.login-container button:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
||||||
|
.error-message {
|
||||||
|
color: red;
|
||||||
|
margin-top: 10px;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
<script src="/js/jquery-3.4.1.min.js"></script>
|
||||||
|
<script>
|
||||||
|
var token = ""
|
||||||
|
function login() {
|
||||||
|
const username = $('#username').val();
|
||||||
|
const password = $('#password').val();
|
||||||
|
if (!username || !password) {
|
||||||
|
alert('请输入用户名和密码');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: '/api/user.lua?action=login',
|
||||||
|
type: 'POST',
|
||||||
|
contentType: 'application/json',
|
||||||
|
data: JSON.stringify({ username: username, password: password }),
|
||||||
|
success: function(response) {
|
||||||
|
if (response.code === 200) {
|
||||||
|
token = response.data.token;
|
||||||
|
fetchUserInfo();
|
||||||
|
} else {
|
||||||
|
$('#errorMessage').text(response.msg || '登录失败,请重试');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchUserInfo() {
|
||||||
|
$.ajax({
|
||||||
|
url: '/api/user.lua?action=getinfo',
|
||||||
|
type: 'GET',
|
||||||
|
headers:{
|
||||||
|
"token":token
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.code === 200) {
|
||||||
|
const userInfo = response.data;
|
||||||
|
alert(`用户信息:\n登录时间: ${userInfo.login_time}`);
|
||||||
|
} else {
|
||||||
|
alert('获取用户信息失败,请重试');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
alert('获取用户信息失败,请重试');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Fast Web</h1>
|
<div class="login-container">
|
||||||
<div class="button-container">
|
<h1>用户登录</h1>
|
||||||
<button id="admin-btn">需要权限地址</button>
|
<input type="text" id="username" placeholder="请输入用户名">
|
||||||
<button id="public-btn">公共接口</button>
|
<input type="password" id="password" placeholder="请输入密码">
|
||||||
|
<button onclick="login()">确认</button>
|
||||||
|
<div id="errorMessage" class="error-message"></div>
|
||||||
</div>
|
</div>
|
||||||
<form id="interceptor-form">
|
|
||||||
<label for="path">访问路径:</label>
|
|
||||||
<input type="text" id="path" name="path" required>
|
|
||||||
<br><br>
|
|
||||||
<label for="key">Key:</label>
|
|
||||||
<input type="text" id="key" name="key" required>
|
|
||||||
<br><br>
|
|
||||||
<button type="submit">提交</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
$(document).ready(function(){
|
|
||||||
$('#admin-btn').click(function(){
|
|
||||||
$('#path').val('/api/admin/admin.lua');
|
|
||||||
});
|
|
||||||
|
|
||||||
$('#public-btn').click(function(){
|
|
||||||
$('#path').val('/api/public/info.lua');
|
|
||||||
});
|
|
||||||
|
|
||||||
$('#interceptor-form').on('submit', function(event){
|
|
||||||
event.preventDefault();
|
|
||||||
var path = $('#path').val();
|
|
||||||
var key = $('#key').val();
|
|
||||||
var data = JSON.stringify({ key: key });
|
|
||||||
|
|
||||||
$.ajax({
|
|
||||||
url: path,
|
|
||||||
method: 'POST',
|
|
||||||
contentType: 'application/json',
|
|
||||||
data: data,
|
|
||||||
success: function(response) {
|
|
||||||
alert('请求成功: ' + response);
|
|
||||||
},
|
|
||||||
error: function(jqXHR, textStatus, errorThrown) {
|
|
||||||
alert('请求失败: ' + textStatus + ' - ' + errorThrown);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user