软件测试 | Cookie和Session

接下来继续另外有意思的话题,在不考虑数据库验证的情况下,假如通过“admin”登录,然后,在登录成功页面显示“嘿,admin 你好!”。这是一般系统都会提供的一个小功能,接下来我们将分别通过Cookie和Sessio来实现它。

Cookie机制:Cookie分发通过扩展HTTP协议来实现的,服务器通过在HTTP的响应头中加上一行特殊的指示来提示浏览器按照提示生成相应的Cookie。然而纯粹的客户端脚本如JavaScript或者VBScript也可以生成Cookie。而Cookie的使用则是由浏览器按照一定的原则在后台自动发送给服务器。浏览器检查所有存储的Cookie,如果某个Cookie所生命的作用范围大于等于将要请求的资源所在的位置,则把该Cookie附在请求资源的HTTP请求上发送给服务器。

Session机制:Session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。

Cookie的使用

继续修改.../sign/view.py文件

......
#登录动作
def login_action(request):
   if request.method == 'POST':
      username = request.POST.get('username','')
      password = request.POST.get('password','')
      if username == 'admin' and password == 'admin123':
          response = HttpResponseRedirect('/event_manage')
          response.set_cookie('user',username,3600)  #添加浏览器 cookie
          return response
     else:
          return render(request,'index.html',{'error':'username or password error!'})

#发布会管理
def event_manage(request):
    username = request.COOKIES.get('user','') #读取浏览器cookie
    return render(request,"event_name.html",{"user":username})

当用户登录成功后,在跳转到event_manage视图函数的过程中,通过set_cookie()方法向浏览器中添加Cookie信息。

这里给set_cookie()方法传了三个参数:第一个参数“user”用于表示写入浏览器的Cookie名,第二个参数username是由用户在登录页上输入的用户名(即“admin”),第三个参数3600用于设置Cookie信息在浏览器中的保持时间,默认单位为秒。

在event_manage视图函数中,通过request.COOKIES来读取Cookie名为“user”的值。并且通过render将它和event_manage.html页面一起返回。

修改.../templates/event_manage.html页面,添加<div>标签来显示用户名。

....
   <div style="float:right;">
     <a>嘿!{{ user }} 欢迎</a><hr/>
   <div>
....

重新登录,将会看到如图3.6所示的页面。

3.6 登录页显示Cookie里的用户名

通过Firebug工具查看浏览器里存放的Cookie信息,如图3.7所示。

图3.7 通过Firebug查看浏览器Cookie信息

Session的使用

Cookie固然好,但存在一定的安全隐患。Cookie像我们以前使用的存折,用户的存钱、取钱记录都会保存在这张存折上(即浏览器中会保存所有用户信息),而有非分想法的人可能会去修改存折上的数据(这个比喻忽略掉了银行服务器同样会记录用户存取款的信息)。

Session相比要安全很多。Session就想是银行卡,客户拿到的只是一个银行卡号(即浏览器只保留一个Session),用户的存钱、取钱记录是根据银行卡号保存在银行的系统里(即Web服务端),只得到一个Sessionid并没有什么意义。

在Django中使用Session和Cookie类似。只需将Cookie的几步操作替换为Session操作即可。修改.../sign/views.py文件。

....
# 登录动作
def login_action(request):
   if request.method == 'POST':
   username = request.POST.get('username','')
   password = requset.POST.get('password','')
   if username == 'admin' and password == 'admin123':
      response = HttpResponseRedirect('/event_manage/')
      # response.set_cookie('user',username,3600)  #添加浏览器 cookie
      request.session['user'] = username #将session信息记录到浏览器
      return response
   else:
      return render(request,'index.html',{'error':'username or password error!'})

#发布会管理
def event_mansge(request):
    # username = request.COOKIES.get('user','') #读取浏览器cookie
    username = request.session.get('user','') #读取浏览器session
    return reder(request,"event_manage.html",{"user":username})

再次尝试登录,不出意外的话将会得到一个错误:

“no such table:django_session”

这个错误跟Session的机制有关,既然要从Web服务器端来记录用户的信息,那么一定要有存放用户sessionid对应信息的地方才行。所以,我们需要创建django_session表。别着急!Django已经帮我们准备好这些常用的表,只需将他们生成即可,是不是很贴心。

\guest> python3 manage.py migrate
Operations to perform:
 Apply all migrations:admin,auth,contenttypes,sessions
Running migrations:
 Applying contenttypes.0001_initial...OK
 Applying auth.0001_initial...OK
 Applying admin.0001_initial...OK
 Applying admin.0002_logentry_remove_auto_add...OK
 Applying contenttypes.0002_remove_content_type_name....OK
 Applying auth.0002_alter_permission_name_max_length...OK
 Applying auth.0003_alter_user_email_max_length...OK
 Applying auth.0004_alter_user_username_opts...OK
 Applying auth.0005_alter_user_last_login_null....OK
 Applying auth.0006_require_contenttypes_0002...OK
 Applying auth.0007_alter_validators_add_error_messages...OK
 Applying auth.0008_alter_user_username_max_length...OK
 Applying sessions.0001_initial...OK

通过“migrate”命令进行数据迁移。等等!我们好像并没有配置数据库啊,为什么已经生成了数据库表呢?这是因为Django已经默认设置SQLite3数据库。在.../settings.py文件,查看SQLite3数据库的配置。

.....
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
   'default':{
      'ENGINE':'django.db.backends.sqlite3',
       'NAME':os.path.json(BASE_DIR,'db.sqlite3'),
  }
}
......

在guest项目的根目录下已经生成了一个db.sqlite3的数据库文件,此时,先来验证Session功能是否生效,重新登录。如图3.8所示,通过Firebug查看Seesionid.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值