帮我实现一套完整的html登陆,跳转页面及登出,支持token保存和验证,后端是tornado,给出代码
时间: 2023-05-18 22:05:45 浏览: 83
前端代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
$(document).ready(function() {
$("#login-form").submit(function(event) {
event.preventDefault();
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url: "/login",
type: "POST",
data: JSON.stringify({username: username, password: password}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
if (data.success) {
window.location.href = "/home";
} else {
alert(data.message);
}
},
error: function(xhr, status, error) {
alert("Error: " + error);
}
});
});
$("#logout").click(function(event) {
event.preventDefault();
$.ajax({
url: "/logout",
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
if (data.success) {
window.location.href = "/";
} else {
alert(data.message);
}
},
error: function(xhr, status, error) {
alert("Error: " + error);
}
});
});
});
</script>
</head>
<body>
<div class="container">
<h2>Login</h2>
<form id="login-form">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" placeholder="Enter password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<br>
<button id="logout" class="btn btn-default">Logout</button>
</div>
</body>
</html>
```
后端代码:
```python
import tornado.ioloop
import tornado.web
import json
import uuid
# In-memory database
users = {
"admin": "admin",
"user1": "password1",
"user2": "password2"
}
# In-memory token database
tokens = {}
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
token = self.get_secure_cookie("token")
if token:
return tokens.get(token.decode("utf-8"))
class LoginHandler(BaseHandler):
def get(self):
self.render("login.html")
def post(self):
data = json.loads(self.request.body.decode("utf-8"))
username = data["username"]
password = data["password"]
if username in users and users[username] == password:
token = str(uuid.uuid4())
tokens[token] = username
self.set_secure_cookie("token", token)
self.write(json.dumps({"success": True}))
else:
self.write(json.dumps({"success": False, "message": "Invalid username or password"}))
class HomeHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
self.render("home.html")
class LogoutHandler(BaseHandler):
def post(self):
token = self.get_secure_cookie("token")
if token:
del tokens[token.decode("utf-8")]
self.clear_cookie("token")
self.write(json.dumps({"success": True}))
else:
self.write(json.dumps({"success": False, "message": "Not logged in"}))
def make_app():
return tornado.web.Application([
(r"/", LoginHandler),
(r"/home", HomeHandler),
(r"/logout", LogoutHandler),
], cookie_secret="mysecret")
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
```
在这个例子中,我们使用了一个基于内存的数据库来存储用户信息和token信息。在实际应用中,我们应该使用一个真正的数据库来存储这些信息。此外,我们还使用了Tornado的安全cookie来存储token信息,以便在不同的请求之间共享。
阅读全文