码迷,mamicode.com
首页 > Web开发 > 详细

Web Service 之 http(二)

时间:2016-10-11 00:03:53      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:httpd

概述:

    本篇是http://1992tao.blog.51cto.com/11606804/1859597 的后续,将会继续介绍http-2.4的配置

========================================================================


12.基于用户和组的访问控制

Require user  USERLIST(用户名) 

Require group GRPLIST(组名)


http协议的认证方式

  • basic基本认证

  • digest摘要认证


http协议认证过程:

  • 认证质询

        WW-Authencate:响应码为401,拒绝客户端请求,并说明用户需要输入正确的账号和密码之后方可访问;

  • 认证:

        Authorization:客户端填入账号和密码,再次发送请求报文;认证通过,服务器发送响应内容;

basic认证机制的实现

  1)定义安全域

  <Directory "">
      Options None
      AllowOverride None
      AuthType Basic      # 认证方式
      AuthName "STRING"   # 认证名称,为字符串
      AuthUserFile ""     # 用户账号文件路径
      Require user USER1 USER2 ...  # 允许登陆的用户,
  </Directory>

 

 2)提供用户的账号文件

  • htpasswd命令用于维护此文件

  • 格式:htpasswd [options] "/PATH/TO/HT_PASSWD_FILE" username

  • 选项:

        -c:添加第一个用户时创建此文件;

        -m:以md5格式加密用户密码存放;

        -s:以sha格式加密用户密码存放;

        -D: 删除指定用户 


Require 使用方式

  • Require valid-user:所有位于AuthUserFile文件中定义的用户都允许登录;

  • Require user USER1 USER2:仅允许user1,user2等出现在AuthUserFile文件中定义的特定几个用户登录,这些用户为虚拟用户,即非系统用户;


基于组认证

  • 定义安全域如下:

  <Directory "">
      Options None
      AllowOverride None
      AuthType Basic      
      AuthName "STRING"   
      AuthUserFile "" 
      AuthGroupFile ""    
      Require group GROUPNAME1 GROUPNAME2 ...  
  </Directory>
  • 组账号文件:

    每行定义一个组:group_name: user1 user2 ...

演示如下:

1.基于用户认证

   1)创建用户账号文件,并添加用户

[root@centos7 httpd]# htpasswd -c -m /etc/httpd/conf/.htpasswd tao 
New password: 
Re-type new password: 
Adding password for user tao
[root@centos7 httpd]# htpasswd  -m /etc/httpd/conf/.htpasswd xiu 
New password: 
Re-type new password: 
Adding password for user xiu
[root@centos7 httpd]# cat /etc/httpd/conf/.htpasswd 
tao:$apr1$KYDxvZxS$VaQ0DVSvlJZlwuNFKUjR90
xiu:$apr1$oc3jfZq0$Fy4OyNCjFGYFmDRlH4mP8.

  2)为了演示效果创建一个目录

[root@centos7 conf.d]# mkdir /vhosts/www1/admin
[root@centos7 conf.d]# vim  /vhosts/www1/admin/admin.html
  1 <h1>TAOTAO XIUXIU</h1>

   3)编辑之前的虚拟主机配置文件,添加安全域

 [root@centos7 conf.d]# vim vhosts.conf
  1 <VirtualHost *:80>
  2         ServerName www1.taotao.com
  3         DocumentRoot "/vhosts/www1"
  4         <Directory "/vhosts/www1">
  5              Options None
  6              AllowOverride None
  7              Require all granted
  8         </Directory>
  9   # 添加安全域
 10         <Directory "/vhosts/www1/admin">
 11              Options None
 12              AllowOverride None
 13              AuthType Basic
 14              AuthName "Admin Area,Enter your name/pass"
 15              AuthUserFile "/etc/httpd/conf/.htpasswd"
 16              Require valid-user
 17          </Directory>
 18          ErrorLog "logs/www1-error_log"
 19          CustomLog "logs/www1-access_log" combined
 20 </VirtualHost>
[root@centos7 httpd]# httpd -t
Syntax OK
[root@centos7 httpd]# !sys
systemctl reload httpd

 通过浏览器访问如下:

技术分享

技术分享

2.基于组认证演示:

  1)在上例的基础上再创建两个用户,归为一个组里

# 在原来的基础上在创建两个用户tom和wang
[root@centos7 httpd]# htpasswd  -m /etc/httpd/conf/.htpasswd tom
New password: 
Re-type new password: 
Adding password for user tom
[root@centos7 httpd]# htpasswd  -m /etc/httpd/conf/.htpasswd wang
New password: 
Re-type new password: 
Adding password for user wang

[root@centos7 httpd]# cat /etc/httpd/conf/.htpasswd 
tao:$apr1$KYDxvZxS$VaQ0DVSvlJZlwuNFKUjR90
xiu:$apr1$oc3jfZq0$Fy4OyNCjFGYFmDRlH4mP8.
tom:$apr1$/AOJinXS$Zj.J1eWV/O9lEFn.4hdub1
wang:$apr1$YBD7fQtq$AbG8Gota3tUnFKwYX4bKd0

   2)创建组文件

[root@centos7 httpd]# vim conf/.htgroup
 admins:tom wang

  3)修改虚拟主机配置文件

  1 <VirtualHost *:80>
  2         ServerName www1.taotao.com
  3         DocumentRoot "/vhosts/www1"
  4         <Directory "/vhosts/www1">
  5              Options None
  6              AllowOverride None
  7              Require all granted
  8         </Directory>
  9 
 10         <Directory "/vhosts/www1/admin">
 11              Options None
 12              AllowOverride None
 13              AuthType Basic
 14              AuthName "Admin Area,Enter your name/pass"
 15              AuthUserFile "/etc/httpd/conf/.htpasswd"
 16              AuthgroupFile "/etc/httpd/conf/.htgroup"
 17              Require group admins
 18          </Directory>
 19          ErrorLog "logs/www1-error_log"
 20          CustomLog "logs/www1-access_log" combined
 21 </VirtualHost>
 
 [root@centos7 httpd]# httpd -t
Syntax OK
[root@centos7 httpd]# systemctl reload httpd

 通过浏览器访问如下:

技术分享

http协议

 1)事务:request/response

  • request:报文

      <method> <request-URL> <version>

       HEADERS


      <entity-body>

  • response:报文

      <version> <status状态码> <reason-phrase原因短语>

       HEADERS


      <entity-body>

  • headers:

       Name: Value1, value2, ...

       Name: Value1, value2, ...

       ......

 如图所示为百度的调试界面:

技术分享


2)首部(headers)分类

通用首部、请求首部、响应首部、实体首部、扩展首部


通用首部 

  • Connection: {close|keep-alive}表示:连接后立即断开或者保持连接

       一般而言,现在的浏览器在请求时一般都是keep-alive,而响应报文则取决于服务器端的配置是否启用了保持连接的功能来决定;

  • Date:报文创建的日期时间;

  • Via:显示报文中经过的中间节点;

  • Cache-Control:缓存控制;

  • Pragma:

请求首部 

  • Host:请求的服务器名称和端口号;

  • Referer:跳转至当前页面的上级资源; 

  • User-Agent:用户代理;

  • Client-IP:

◎Accept:可接收的MIME类型;

  • Accept-Language:可接收的语言

  • Accept-Encoding:所能够接收的编码格式,如gzip, defalte, 

  • Accept-Charset:所能接受的字符集

   ...

条件式请求首部:

  • Except:

  • If-Modified-Since:自从指定的时间之后,请求的资源是否发生过修改;

  • If-Unmodified-Since:

  • If-None-Match :本地缓存中存储的文档的ETag标签是否与服务器文档的ETag不匹配;

  • If-Match

安全相关的请求首部:

  • Authorization:向服务器发送认证信息,请求授权,如账号和密码

  • Cookie:

  • Cookie2


如下图所示为百度的request报文

技术分享   

响应首部 

信息性首部:

  • Age:响应持续时长

  • Server:服务器程序的名称和版本(建议隐藏

协商类首部:

  •  Accept-Range:服务器端可接受的请求类型范围

  •  Vary:其它首部列表

安全相关的首部:

  • WWW-Authenticate:认证质询,来自服务器的对客户端的质询认证表单;

  • Set-Cookie:

  • Set-Cookie2:

☉理解

 cookie (储存在用户本地终端上的数据)

  • 指某些网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密)

  • Cookie就是服务器暂存放在你计算机上的一笔资料,好让服务器用来辨认你的计算机。当你在浏网站的时候,Web服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文或是一些选择,都记录下来。当下次你再光临同一个网站,Web服务器会先看看有没有它上次留下Cookie资料,有的话,就会依据Cookie里的内容来判断使用者,送出特定的网页内容给你。

 session

  • Session 是 用于保持状态的基于 Web服务器的方法。Session 允许通过将对象存储在 Web服务器的内存中在整个用户会话过程中保持任何对象


如下图所示为百度的response报文

技术分享

实体首部 

  • Content-Encoding:内容编码;明文还是压缩的;

  • Content-Language:内容语言;

  • Content-Lenth:主体长度;

  • Content-Location:实体真正所处位置

  • Content-Type :主题的对象类型

  ...

  • Allow:列出对此实体可使用的请求方法;

  • Location:告诉客户端真正的实体位于何处

缓存相关:

  • Etag:实体的扩展标签

  • Last-Modified:最后一次修改时间

  • Expires:实体的过期时间


扩展首部 

  • X-Forwarded-For 为谁转发

 ...

3)url:Uniform Resource Locator

常用格式

  scheme://host:port/path <协议>://<主机名称或地址>[:port]/<URL>

完整格式:

 scheme://[<user>[:<password>]@<host>[:<port>]/<path>;<params>?<query>#frag

  • params:参数, ;param1=value1&param2=value2

  • query:查询字符串, ?field1=value1&field2=value2

  • frag:页面锚定,#frag_id, 例如#ch1



4)curl命令

★  curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST、PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。

语法格式:

   curl  [options]  [URL...]

选项

    -A/--user-agent <string> 设置用户代理发送给服务器

    --basic 使用HTTP基本认证

    -e/--referer <URL> 来源网址

    --cacert <file> CA证书 (SSL)

    --compressed 要求返回是压缩的格式

    -H/--header <line>自定义首部信息传递给服务器

    -I/--head 只显示响应报文首部信息

    --limit-rate <rate> 设置传输速度

    -u/--user <user[:password]>设置服务器的用户和密码

    -0/--http1.0 使用HTTP 1.0

    -X, --request <command>:自定义请求方法

另一个工具:elinks

  用法:elinks  [OPTION]... [URL]...

-dump: 不进入交互式模式,而直接将URL的内容输出至标准输出;


httpd-2.4基础配置

 1.使用mod_deflate模块压缩页面优化传输速度

适用场景

  • 节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持;

  • 压缩适于压缩的资源,例如文件文件

注意:

   不是所有的资源类型都适合压缩,适合压缩的文件是那些纯文本文件;

   设置输出过滤器(发送给响应报文用到的),过滤出来的执行压缩


演示如下:

  首先确保该模块已经安装

[root@centos7 ~]# httpd -M |grep deflate
 deflate_module (shared)

 首先查看一下是否有压缩机制

[root@centos7 httpd]# cp /var/log/lastlog /var/www/html/lastlog.txt 
[root@centos7 httpd]# curl -I  # 获取首部信息 
HTTP/1.1 200 OK
Date: Mon, 10 Oct 2016 14:10:26 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 10 Oct 2016 14:08:17 GMT
ETag: "8eef4-53e834aa771bb"
Accept-Ranges: bytes
Content-Length: 585460
Content-Type: text/plain; charset=UTF-8 # 为文本信息,是支持压缩的

[root@centos7 httpd]# curl --compress -I  # 要求首部返回压缩格式 
HTTP/1.1 200 OK
Date: Mon, 10 Oct 2016 14:10:55 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 10 Oct 2016 14:08:17 GMT
ETag: "8eef4-53e834aa771bb"
Accept-Ranges: bytes
Content-Length: 585460
Content-Type: text/plain; charset=UTF-8
# 此时后不支持压缩功能

  启动压缩模块配置,但是在压缩之前要先过滤,把适合压缩的文本文件过滤出来

# 创建文件 /etc/httpd/conf.d/deflate.conf
[root@centos7 httpd]# vim conf.d/deflate.conf
  1 SetOutputFilter DEFLATE  # 设置输出过滤器(发送给响应报文用到的),过滤出来的执行压缩
  2 # mod_deflate configuration      
  3                                  
  4 # Restrict compression to these MIME types
  5                                  
  6 # Restrict compression to these MIME types
  7 AddOutputFilterByType DEFLATE text/plain
  8 AddOutputFilterByType DEFLATE text/html
  9 AddOutputFilterByType DEFLATE application/xhtml+xml
 10 AddOutputFilterByType DEFLATE text/xml
 11 AddOutputFilterByType DEFLATE application/xml
 12 AddOutputFilterByType DEFLATE application/x-javascript
 13 AddOutputFilterByType DEFLATE text/javascript    
 14 AddOutputFilterByType DEFLATE text/css           
 15                                                  
 16 # Level of compression (Highest 9 - Lowest 1) 压缩级别                   
 17 DeflateCompressionLevel 9                                                
 18                                                                          
 19  # Netscape 4.x has some problems.  有些老的浏览器不支持压缩功能,要关闭掉
 20  BrowserMatch ^Mozilla/4  gzip-only-text/html
 21                                                                                                                          
 22 # Netscape 4.06-4.08 have some more problems   
 23 BrowserMatch  ^Mozilla/4\.0[678]  no-gzip      
 24                                                
 25  # MSIE masquerades as Netscape, but it is fine
 26 BrowserMatch \bMSI[E]  !no-gzip !gzip-only-text/html
 27 
~                                                                                                                                                                      
"conf.d/deflate.conf" [New] 27L, 973C written                                                                                                        
[root@centos7 httpd]# httpd -t
Syntax OK
[root@centos7 httpd]# !sys
systemctl reload httpd

 再次查看之前的文件

[root@centos7 httpd]# curl -I http://10.1.252.161/lastlog.txt
HTTP/1.1 200 OK
Date: Mon, 10 Oct 2016 14:35:05 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 10 Oct 2016 14:08:17 GMT
ETag: "8eef4-53e834aa771bb"
Accept-Ranges: bytes
Content-Length: 585460
Vary: Accept-Encoding 
Content-Type: text/plain; charset=UTF-8

[root@centos7 httpd]# curl --compress -I http://10.1.252.161/lastlog.txt
HTTP/1.1 200 OK
Date: Mon, 10 Oct 2016 14:35:54 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Mon, 10 Oct 2016 14:08:17 GMT
ETag: "8eef4-53e834aa771bb-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding #接受编码
Content-Encoding: gzip # gzip 压缩
Content-Length: 685
Content-Type: text/plain; charset=UTF-8




































Web Service 之 http(二)

标签:httpd

原文地址:http://1992tao.blog.51cto.com/11606804/1860418

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!