广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 21392 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为IE收藏   收藏主题   上一主题 | 下一主题   
aa 手机 会员卡 葫芦墩家族
个人文章 个人相簿 个人日记 个人地图 个人商品
特殊贡献奖 社区建设奖
头衔:            
站长
级别: 站长 该用户目前不上站
推文 x1963 鲜花 x5260
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片
推文 x0
[Linux] [转贴]Apache 网页认证.htaccess 设定方法
.htaccess
.htaccess 可放在任何一网页目录内,当该目录在 httpd.conf 设定不是 AllowOverride None 便可使用 .htaccess 的功能了,而设定作 AllowOverride All 更可使用 .htaccess 的所有功能了
.htaccess 可用作对该目录及其子目录进行取存控制,而不用修改 httpd.conf 及不用重新启动伺服器软件
--------------------------------------------------------------------------------
httpd.conf 的设定
加上或取消注解 (#) ]定改成 AllowOverride All
然后储存及重新启动伺服器软件
--------------------------------------------------------------------------------
.htaccess 的用法
先把 .htaccess 放在要用的目录内 (Windows 系统内不可以直接把档案改名作 .htaccess ,需用间接的方法,如在指令模式下 ren filename.txt .htaccess 或用 FTP 软件修改该档名)
用法:
1. 密码保护
.htaccess 内写上:
AuthName "testing"
AuthType Basic
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
require valid-user
AuthName 是当你进入密码保护时,浏览器出现的说明
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd" 是密码档案的位置
密码档案 (.htpasswd) 内写上:
user1:password1
user2:password2
但密码不应直接写上,即是如果有一使用者是 test ,密码是 password ,先用 C:\Apache\bin 内的 htpasswd.exe 把密码加密,用法如下] require group group1 ,因此只有 group1 的使用者可通过,即 john , peter 及 david

2. 自设错误报告文件
.htaccess 内写上:
ErrorDocument 404 /404.html
如果找不到网页时,便输出 404.html 的内容,其他错误时做法亦然,如:
ErrorDocument 404 "<html><body>找不到网页</body></html>
ErrorDocument 500 /500.html

3. 自设首页档案
.htaccess 内写上:
DirectoryIndex a.html
首页档案便是 a.html 了

4. 禁止读取档案
.htaccess 内写上:
<Files secret.html>
order allow,deny
deny from all
</Files>
deny from all 即所有人皆不能读取 sercet.html

而 Apache 1.3 以后的版本,更可以用支援 regular expression 的 filesmatch
.htaccess 内写上:
<filesmatch "\.jpg">
order allow,deny
deny from all
</filesmatch>
即所有人皆不能读取副档名为 .jpg 的档案

.htaccess 内写上:
<Files secret.html>
order allow,deny
allow form all
deny form 202.202
</Files>
即 ip 是以 202.202 开头的不能读取副档名为 secret.html 的档案

.htaccess 内写上:
<Limit GET>
order deny,allow
deny from all
allow from 202.202 domain.com
</Limit>
即只允许 ip 是以 202.202 为开头或域名是 domain.com 的读取此目录内的任何内容

.htaccess 内写上:
AuthUserFile "C:/Apache/htdocs/testing/.htpasswd"
<files sercet.html>
require user peter
</files>
即只允许 .htpasswd 内列出的 peter 读取 sercet.html

5. 重新导向文件
.htaccess 内写上:
Redirect /old http://ur...ew
当用户要求 /old 时,伺服器便把其重新导向至 http://ur...ew ,或在 .htaccess 内写上:
Redirect permanent /old http://ur...ew
当用户要求 /old 时,伺服器便叫浏览器把其永远重新导向至 http://ur...ew

6. 防止列出目绿内的档案
.htaccess 内写上:
Option -Indexes
这便不能列出档案目录了
.htaccess 内写上:
IndexIgnore *.zip *.txt
这便不列出档案目录内的 .zip 与 .txt 档了

注: .htaccess 的大部分内容 (如:密码保护) 可以写回 httpd.conf 相应的目录设定内( <Directory> )


[ 此文章被andyz在2005-05-18 19:37重新编辑 ]



献花 x0 回到顶端 [楼 主] From:台湾数位联合 | Posted:2002-12-12 11:49 |
aa 手机 会员卡 葫芦墩家族
个人文章 个人相簿 个人日记 个人地图 个人商品
特殊贡献奖 社区建设奖
头衔:            
站长
级别: 站长 该用户目前不上站
推文 x1963 鲜花 x5260
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

首先在你要保护的目录下建立一个名为.htaccess的档案,内容大致如下:

# 随便取个名字吧,它会出现在浏览器的蹦出视窗中。
AuthName 阿达的秘密基地
# 编码方式,目前只支援Basic一种而已。
AuthType Basic
# 密码档的绝对路径,可用htpasswd指令建立。
AuthUserFile /home/.../filename
# 若下面有指定 require group,则需要group档的绝对路径。
AuthGroupFile /home/.../filename

<Limit GET POST>
require user user1 user2 ...
require group group1 group2 ...
</Limit>


它的密码认证方式有二种,一种是指定users,也就是只有指定的username才有资格进入这个受保护的目录。另一种是则指定group,凡属于指定的group的users才有资格进入这个受保护的目录,而group中到底有哪些users是记录在AuthGroupFile中所指定的档案,users的密码档则是指定在AuthUserFile中。且让我们来看看这两个档的格式:

group档:
group1:user1 user2 ...
group2:user3 user4 ...
...

passwd档:
user1:password
user2:password
...

密码档里的密码是编码过的,你可以使用htpasswd指令来建立密码档或新增users。而且由于密码是用crypt函数编码的,所以你也可以自己写个CGI程式来新增users。

另外还有个安全问题要提醒你:密码档不要放在www文件的树目录中,最好是放在www目录外面,以免密码档被不法之徒偷去猜密码。

OK!!就这么简单,现在你可以试试看成功了没...


献花 x0 回到顶端 [1 楼] From:未知地址 | Posted:2002-12-12 11:50 |
aa 手机 会员卡 葫芦墩家族
个人文章 个人相簿 个人日记 个人地图 个人商品
特殊贡献奖 社区建设奖
头衔:            
站长
级别: 站长 该用户目前不上站
推文 x1963 鲜花 x5260
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

一、建立.htaccess档案
首先login cyber.cs.ntou.edu.tw,在你要保护的目录下建立一个名为.htaccess 内容为

复制程式
  # 随便取个名字吧,它会出?#123;在浏览器的蹦出视窗中。
  AuthName 国立台湾海洋大学资讯系通讯录查询

  # 编码方式,目前只支援Basic一种而已。
  AuthType Basic

  # 密码档的绝对路径,可用htpasswd指令建立
  # (.htpasswd档可在ind.ntou.edu.tw上建好再传回cyber.cs.ntou.edu.tw)。
  AuthUserFile /home/under/b86204/.htpasswd
  #^^^^^
  #自己的帐户名

  # 若下面有指定 require group,则需要group档的绝对路径。
  # AuthGroupFile /home/under/b86204/filename

  <Limit GET POST>
  require user franklin
  require user beef
  require user david
  require user huangant
  #^^^^^^^^^
  #可?#91;看你的Project的username

  #require group my-users group2 ...
  </Limit>


AuthGroupFile 档案内容范例]franklin:Z4vr73LqdneRA
beef:RVN7zpfopMpiY
davidɈmUlggYxqB4KQ
huangant:DtAmaQ49hJuWk [/code]


^^^^^^^^^^^^^^^^^^^^
usernamegcrypt编码过的密码


当然最简单的方法,就是 telnet ind.ntou.edu.tw这台主机, 利用htpasswd程式建立.htpasswd,再传回cyber.cs.ntou.edu.tw。 方法如下所示] % telnet ind.ntou.edu.tw
(login的过?#123;略)

% htpasswd -c htpasswd.txt franklin
Adding password for franklin
New password:
Re-type new password:

% htpasswd htpasswd.txtbeef
Adding user beef
New password:
Re-type new password:

(其余的username 之密码建立,如同"htpasswd htpasswd.txt beef"一般,
以此类推。如果 htpasswd ?#123;式有任何疑问,不妨用"htpasswd -h"查查)

%ftp cyber.cs.ntou.edu.tw
(ftp过?#123;略)

ftp>mput htpasswd.txt
ftp>bye

%telnet cyber.cs.ntou.edu.tw
(login过?#123;略)

% mv htpasswd.txt .htpasswd[/code]


三、要如何在Homepage或CGI辨识那一个user在浏览此一加密的目录? 不外乎要使用环境变数,但是需要一些前置作业]% cp /usr/local/etc/httpd/cgi-bin/w3-msql ~/www/project
^^^^^^^^^^^^^
Project location
% mv ~/www/project/w3-msql ~/www/project/w3-msql.cgi[/code]




在你呼叫含有w3-msql语法的html档时,要用


复制程式
"http://cyber.cs.ntou.edu.tw/~b86204/project/w3-msql.cgi/~b86204/project/project.html"
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                  your cgi                        your project file





如此一来,就可以在w3-msql中,用$REMOTE_USER得知目前的user是谁了。

四、如何限制某些 domain 的存取

复制程式
AuthName Example 
AuthType Basic 

AuthUserFile /home/under/b86204/.htpasswd 

AuthGroupFile /home/under/b86204/filename 

<Limit GET> 
order deny, allow

deny from all

allow from .ntou.edu.tw

</Limit>


献花 x0 回到顶端 [2 楼] From:未知地址 | Posted:2002-12-12 11:53 |
tomlch
数位造型
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

大大你发表了这三篇是不是都是一样的!我看了有点昏了...可以说明一下你这三篇文章的差别在那吗?另外,我可以把你的文章张贴在我个人的论坛上吗?


献花 x0 回到顶端 [3 楼] From:未知地址 | Posted:2004-06-08 07:06 |
炽天使死神
个人头像
个人文章 个人相簿 个人日记 个人地图
小人物
级别: 小人物 该用户目前不上站
推文 x0 鲜花 x0
分享: 转寄此文章 Facebook Plurk Twitter 复制连结到剪贴簿 转换为繁体 转换为简体 载入图片

是一篇文章分成三部份来贴吧 囧rz


  A man can be an artist in anything.
  Anything if his
soul is true to it.
  It depends on how good he is at it.
献花 x0 回到顶端 [4 楼] From:未知地址 | Posted:2004-12-18 00:33 |

首页  发表文章 发表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.022962 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言