基于cookie和session的登录认证示例
2018-03-06 11:06
288 查看
登录认证示例
需要知道几点
一共有三次请求
注意: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
class UserInfo(models.Model):
username =models.CharField(max_length=32)
password =models.CharField(max_length=32)in login.html
cookie存储到客户端
优点:数据存储在客户端。减轻服务端的压力,提高网站的性能
缺点:安全性不高,在客户端很容易被查看或破解用户会话信息
需要知道几点
一共有三次请求
注意: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存储到客户端
优点:数据存储在客户端。减轻服务端的压力,提高网站的性能
缺点:安全性不高,在客户端很容易被查看或破解用户会话信息
相关文章推荐
- JavaWeb--Servlet过滤器Filter和SpringMVC的HandlerInterceptor(Session和Cookie登录认证)
- React学习笔记_基于Cookie的登录认证
- WEB后台--基于Token的WEB后台登录认证机制(并讲解其他认证机制以及cookie和session机制)
- 利用cookie实现登陆知网与抽屉网 基于session自动登录抽屉网 cookie与session区别
- 基于Token的WEB后台登录认证机制(并讲解其他认证机制以及cookie和session机制)
- JavaWeb使用Session和Cookie实现登录认证
- 基于Cookie和session,实现单点登录
- WEB后台--基于Token的WEB后台登录认证机制(并讲解其他认证机制以及cookie和session机制)
- 在基于vue的webpack脚手架开发中使用了代理转发,结果浏览器发出的请求中不带cookie导致登录时总是session失效怎么办?
- 单点登录三个方法及原理:共享Session、基于OpenId的单点登录、基于Cookie的OpenId存储方案
- 单点登录三个方法及原理:共享Session、基于OpenId的单点登录、基于Cookie的OpenId存储方案
- CORS跨域、Cookie传递SessionID实现单点登录后的权限认证的移动端兼容性测试报告
- 使用Session和Cookie实现登录认证
- JavaWeb日记——Session和Cookie实现登录认证
- session和cookie的使用方法、区别,和分别实现验证登录状态
- SSO 基于Cookie+fliter实现单点登录 实例解析(一)
- SSO 基于Cookie+fliter实现单点登录(SSO):工作原理
- 如何检查 ASP.NET 基于cookie 的 Session 是否失效
- 基于Cookie跨域的单点登录问题