您的位置:首页 > 其它

基于cookie和session的登录认证示例

2018-03-06 11:06 225 查看
登录认证示例

    需要知道几点
    一共有三次请求
        注意:form表单的action走的路径还是/login/
     第一次请求:url:http://127.0.0.1:8080/login get请求
       第一次请求:url:http://127.0.0.1:8080/login post请求 user pasw
       第一次请求:url:http://127.0.0.1:8080/index post请求 携带着cookie的了
       所以在index页面中就会取到cookie,因为这是的index里面已经有cookie了



in urls.pyfrom app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^login/', views.login),
url(r'^index/', views.index),
]
in views.py
from django.shortcuts import render,redirect,HttpResponse
from app01 import models
# Create your views here.
def login(request):
if request.method=="POST":
print("所有请求数据",request.POST)
username = request.POST.get("username")
password = request.POST.get("password")
# 查看数据库中的用户名和密码,对比用户输入的是否是数据库中的值
ret = models.UserInfo.objects.filter(username=username,password=password)
if ret:  #如果用户名和密码都正确,则登录成功
print(request.COOKIES)  #{'csrftoken': '1EaTcdQlxdwtR0eXu4uDqEHElEpOlDRJoSAd7TfA7cBDxAyxADVPbIKaZk6J0DVB'}
# 由于http协议是无状态的,你这次登录完就不知道是谁登录了,当别人知道你的主页url,就都可以登录了。那样就没有隐私了
# 这就得用到cookie了
obj = redirect("/index/")
obj.set_cookie("islogin",True)  #设置cookie值,注意这里的参数,一个是键,一个是值
obj.set_cookie("haiyan","344",20)  #20代表过期时间
obj.set_cookie("username", username)
return obj
else:
return render(request,"login.html")
else:
return render(request,"login.html")
def index(request):
is_login = request.COOKIES.get("islogin",None)  #得到cookie,有就得到,没有就得到none
if is_login:
username = request.COOKIES.get("username")
print(username)
return render(request,"index.html",{"username":username})
else:  #如果没有拿到值,就一直在登录页面就进不去
return redirect("/login/")
in models.py
class UserInfo(models.Model):
username =models.CharField(max_length=32)
password =models.CharField(max_length=32)in login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title>用户登录</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<style>
.c1{
margin-top: 100px;
}
.btn{
width: 130px;
}
.c2{
margin-left: 40px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="c1 col-md-5 col-md-offset-3">
<form class="form-horizontal" action="/login/" method="post" novalidate>
{% csrf_token %}
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="username" placeholder="Email" name="username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="password"
placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">登录</button>
<button type="submit" class="btn btn-success c2">注册</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
in index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width">
<title>Title</title>
</head>
<body>
<h1>hello{{ username }}</h1>
</body>
</html>

cookie存储到客户端
优点:数据存储在客户端。减轻服务端的压力,提高网站的性能
缺点:安全性不高,在客户端很容易被查看或破解用户会话信息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐