Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610159
  • 博文数量: 356
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2287
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-08 17:08
文章分类

全部博文(356)

文章存档

2023年(3)

2022年(7)

2021年(33)

2020年(47)

2019年(36)

2018年(221)

2017年(1)

2015年(1)

2013年(7)

我的朋友

分类: 服务器与存储

2020-03-19 17:53:35

将基本认证与加密数据传输(比如ssl)配合使用,向恶意用户隐藏用户名和密码,会使基本认证变得更加安全,这是一种常用的技巧。
基本认证:对“用户名:密码”串执行base64加密后传输
今天整理下基本认证------------
先给出web服务器与代理服务器认证的对比:
web服务器 代理服务器
Unauthorized status codeL401 unauthorized status code:407
www-Authenticate Proxy-Authenticate
Authorization Proxy-Authorization
Authentication-Info Proxy-Authentication-Info

如:服务器响应头:www-Authenticate:Basic realm=quoted-realm
客户端请求头:Authorization:Basic base64-username-an-password

缓存:共享的缓存收到包含Authorization首部的请求和转接那条请求产生的响应时,除非响应中提供了下列两种Cache-Control指令之一,否则一定不能将那条响应作为对任何其他请求的应答使用:
指令一:Cache-Control:must-revalidate  缓存可在应答后继请求使用那条响应的实体部分,但首先要用新请求首部与原始服务器再次进行验证
指令二:Cache-Control:public  在对任意后继请求的应答中都可以返回响应的实体部分

========
下面讲解服务器认证端的配置及用户请求认证信息的效果:
1.配置过程:
生成用户认证的用户名和密码:
执行:wget -c soft.ver.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh   【意思是在/usr/local/nginx下生成htpasswd.sh并执行htpasswd.sh
提示输入:
用户名liqing

密码liqing
文件名:liqing22

则在/usr/local/nginx/conf下生成下面的liqing22.conf,如果你的安装目录不是/usr/local/nginx/,则把生成的liqing22.conf放到你的安装目录的/conf/

在nginx中增加配置:
      location / {
            root   /usr/local/nginx1.7.11/html;
            index  index.html index.htm;
            auth_basic  "Authorized test";
            auth_basic_user_file "/usr/local/nginx1.7.11/conf/liqing22.conf";
       }


2. 发送用户请求:
[test1@cdntest-unKnown-qita_unKnown-6-249 curl1125]$ wget -e http_proxy=192.168.10.74:80  ""  -S
--2015-11-25 17:54:28--  
Connecting to 192.168.10.74:80... connected.
Proxy request sent, awaiting response... 
  HTTP/1.0 401 Unauthorized
  Server: DLC-3.0
  Date: Wed, 25 Nov 2015 09:54:49 GMT
  Content-Type: text/html
  Content-Length: 195
  WWW-Authenticate: Basic realm="Authorized test"
  Age: 0
Authorization failed.

服务器返回401,用户再次发送带着用户名密码的请求:
[test1@cdntest-unKnown-qita_unKnown-6-249 wget1126]$ wget -e http_proxy=192.168.10.74:80  ""  --debug --header "Authorization:Basic bGlxaW5nOmxpcWluZw=="  【红色的部分是base64(用户名:密码)的值】
Setting --header (header) to Authorization:Basic bGlxaW5nOmxpcWluZw==
DEBUG output created by Wget 1.12 on linux-gnu.
--2015-11-26 11:13:16--  
Connecting to 192.168.10.74:80... connected.
Created socket 3.
Releasing 0x00000000023b2d20 (new refcount 0).
Deleting unused 0x00000000023b2d20.

---request begin---
GET HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: nginxtest.mytest1.dnion.com:8080
Authorization: Basic bGlxaW5nOmxpcWluZw==

---request end---
Proxy request sent, awaiting response... 
---response begin---
HTTP/1.0 200 OK
Server: DLC-3.0
Date: Thu, 26 Nov 2015 03:13:28 GMT
Content-Type: text/html
Content-Length: 11
Last-Modified: Tue, 24 Nov 2015 08:13:23 GMT
ETag: "56541c23-b"
set-cookie: user=test;domain=dnion.com;path=/photo/;secure;expires=Wednesday,25-NUM-15 17:24:40 GMT
set-cookie: session_id=1;domain=mytest1.dnion.com;path=/
Cache-Control: no-cache=set-cookie
Accept-Ranges: bytes
Age: 0

---response end---
200 OK
cdm: 1 2 3 4 5 6 7 8Attempt to fake the path: /photo/, /html/test.html
cdm: 1 2 3 4 5 6 7 8
Stored cookie mytest1.dnion.com -1 (ANY) / [expiry none] session_id 1
Length: 11 [text/html]
Saving to: ?.est.html.1?
100%[================================================================================================================================================================>] 11          --.-K/s   in 0s      
Closed fd 3
2015-11-26 11:13:16 (1.44 MB/s) - ?.est.html.1?.saved [11/11]

【注意200的响应中没有  WWW-Authenticate: Basic realm="Authorized test",这个只存在401的响应中

请求的效果是:每次都回源,对请求内容不进行缓存,ats的log如下:
1448507692.144 16 192.168.10.75 TCP_MISS/200 420 GET - DIRECT/nginxtest.mytest1.dnion.com text/html -
1448507808.393 9 192.168.10.75 TCP_MISS/200 420 GET - DIRECT/nginxtest.mytest1.dnion.com text/html -

3. 对缓存进行设置:
不特殊设置情况下,如上内容不缓存每次都回源请求
在nginx源上配置Cache-Control:must-revalidate,则对实体会进行缓存,但是每次请求都会回源验证:

[test1@cdntest-unKnown-qita_unKnown-6-249 wget1126]$ wget -e http_proxy=192.168.10.74:80  ""  --debug --header "Authorization:Basic bGlxaW5nOmxpcWluZw=="
Setting --header (header) to Authorization:Basic bGlxaW5nOmxpcWluZw==
DEBUG output created by Wget 1.12 on linux-gnu.

--2015-11-26 11:28:19--  
Connecting to 192.168.10.74:80... connected.
Created socket 3.
Releasing 0x0000000000fdcd20 (new refcount 0).
Deleting unused 0x0000000000fdcd20.

---request begin---
GET HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: nginxtest.mytest1.dnion.com:8080
Authorization: Basic bGlxaW5nOmxpcWluZw==
---request end---
Proxy request sent, awaiting response... 
---response begin---
HTTP/1.0 200 OK
Server: DLC-3.0
Date: Thu, 26 Nov 2015 03:28:31 GMT
Content-Type: text/html
Content-Length: 35
Last-Modified: Thu, 26 Nov 2015 03:28:25 GMT
ETag: "56567c59-23"
Cache-Control: must-revalidate
Accept-Ranges: bytes
Age: 0
---response end---
200 OK
Length: 35 [text/html]
Saving to: ?.est.html.13?
100%[================================================================================================================================================================>] 35          --.-K/s   in 0s      
Closed fd 3
2015-11-26 11:28:19 (4.31 MB/s) - ?.est.html.13?.saved [35/35]

对应的ats的log如下;
1448508274.500 5 192.168.10.75 TCP_MISS/200 257 GET - DIRECT/nginxtest.mytest1.dnion.com text/html -
1448508276.548 2 192.168.10.75 TCP_REFRESH_HIT/200 257 GET - DIRECT/nginxtest.mytest1.dnion.com text/html -


4. 引出:
请求时的缓存指令包括no-cache、no-store、max-age、 max-stale、min-fresh、only-if-cached等。
响应消息中的指令包括public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age。

从我测试结果上来看:在nginx.config中配置的Cache-Control:must-revalidate的优先级高于Cache-Control:public,Cache-Control:public优先级高于Cache-Control:private【private每次都回源取内容且不缓存】。


备注:
转载于:
Nginx超级强大它可以单独为一个域名设置用户认证,方法也很简单我们只要生成用户认证的用户名和密码,然后再Nginx添加auth认证配置即可。

Nginx可以为某一个域名单独加用户认证,具体做法如下:

1. 生成用户认证的用户名和密码:

 代码如下 复制代码

#wget -c soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh

根据提示输入:

用户名:

密码:

文件名:

脚本会自动生成认证文件,auth.conf内容如下:

 代码如下 复制代码

/usr/local//conf/auth.conf

2. 为Nginx添加auth认证配置

下面以某域名下面的auth目录为例,在域名的server段里加上如下代码:

 代码如下 复制代码

location ^~ /auth/ {

location ~ .*.(php|php5)?$ {

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

 fcgi.conf;

}

auth_basic "Authorized users only";

auth_basic_user_file /usr/local/nginx/conf/auth.conf

}

auth_basic_user_file 为htpasswd文件的路径

3. 重启Nginx

访问 就会提示输入用户名和密码。


如果我们只想为目录增加用户认证上面方法显示不行,下面我来介绍具体目录用户认证

为目录增加用户认证( auth basic)

兼容的密码文件,因此我们需要通过apache的htpasswd生成密码文件。

首先查找你系统上的htpasswd

 代码如下 复制代码


find / –name htpasswd

一般CentOS都会装apache的,位置在:

 代码如下 复制代码

/usr/bin/htpasswd

如果没找到那就自行安装

 代码如下 复制代码


yum install apache

并找到htpasswd文件地址。

找到htpasswd文件后,我们来创建一个用户,比如这个用户叫:xiaoquan

 代码如下 复制代码


/usr/bin/htpasswd –c /usr/local/ngnix/conf/authdb xiaoquan

上面的命令在nginx的配置文件目录创建了用户为xiaoquan的authdb密码文件,当然你也可以创建的在其他地方,此处nginx配置文件使用比较方便。

上面的命令输入回车后会得到提示输入密码的提示信息,输入两次,即可添加成功。

接着修改nginx的配置文件,在某个需要加auth_basic的server配置下添加如下内容

 代码如下 复制代码


 
location /admin/ {
 
      auth_basic "QuanLei Auth.";
 
      auth_basic_user_file /usr/local/ngnix/conf/authdb;
 
}

最后让nginx使用最新的配置:

 代码如下 复制代码


/usr/local/ngnix/sbin/nginx -s reload

补充一下,如果你使用了集群环境,那么还需要加Proxy_Pass:

 代码如下 复制代码


 
location /admin/ {
 
      proxy_pass
 
      auth_basic "QuanLei Auth.";
 
      auth_basic_user_file /usr/local/ngnix/conf/authdb;
 
}















阅读(3405) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~