apache http的<directory></directory>语句,允许被访问

本文详细介绍了如何通过Apache配置文件实现对目录的访问控制,包括默认不允许访问、无限制目录访问、有限制目录访问等场景。通过配置<Directory>标签来允许或拒绝特定目录的访问,并使用.htaccess文件实现更细粒度的权限控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原帖:http://blog.sina.com.cn/s/blog_6151984a0100f1tj.html

   如何访问根目录下的目录http://192.168.1.12/test/

  • 第一.缺省apache不允许访问http目录(没有<Directory >定义,就没有访问权限)
访问目录http://192.168.1.12/test/
会显示:
Forbidden
You don't have permission to access /test/ on this server.
  • 第二.无限制目录访问
在httpd.conf中增加<Directory >定义,即可打开无限制的目录访问权限
<Directory /home/macg/www/test>
    Options All
    AllowOverride all
</Directory>
再访问会显示如下:
Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80

Index of /test
 Name                    Last modified      Size  Description
 --------------------------------------------------------------------------------
 Parent Directory                             -   
 bg0073.jpg              29-Nov-2006 21:02   36K  
 bg0135.jpg              29-Nov-2006 21:03   41K  
 bg0137.jpg              29-Nov-2006 21:03   47K  
 slade1.html             29-Nov-2006 22:02  1.2K  
 slade2.html             29-Nov-2006 22:02  1.1K  
 slade3.html             29-Nov-2006 22:02  1.4K  
 slade4.html             29-Nov-2006 22:02  1.8K  
 slade5.html             29-Nov-2006 22:02  2.3K  
--------------------------------------------------------------------------------
Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80

实际AllowOverride all是enable  .htaccess目录限制功能。
但test目录下并没有.htaccess文件
等于开放访问,无限制
  • 第三.有限制目录访问
将其他目录中的.htaccess拷贝入要限制访问的目录
[root@localhost test]# ls -a
 ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  slade1.html  slade2.html  slade3.html  slade4.html  slade5.html
[root@localhost test]# cp ../test1/.htaccess .
[root@localhost test]# ls -a
 ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  .htaccess  slade1.html  slade2.html  slade3.html  slade4.html slade5.html
[root@localhost test]# more .htaccess
authType Basic
AuthName "Restricted Files"
AuthUserFile /etc/httpd/passwords
Require valid-user
再访问http://192.168.1.12/test/
会跳出身份认证窗口,输入用户名密码,即可访问目录


   .htaccess 目录限制的配置
  •     要使用.htaccess文件,先在将httpd.conf中建立<Directory ></Directory>
<Directory "/home/macg/www/test">   
        Options All            
允许对目录的操作,ALL---所有操作

    AllowOverride all                      
AllowOverride all----允许.htaccess所有指令,缺省是all
AllowOverride None ----完全忽略.htaccess文件

</Directory>
  •     用/usr/bin/htpasswd创建一个用于认证的密码文件。
并且这个文件不应该置于DocumentRoot目录下,以避免被下载。
建议创建在/etc/httpd/目录下:
[root@localhost httpd]# /usr/bin/htpasswd -c /etc/httpd/passwords macg
                                           -c建立文件
New password:
Re-type new password:
Adding password for user macg
[root@localhost httpd]# /usr/bin/htpasswd /etc/httpd/passwords gary  
                            没有-c就是单纯的adduser 追加用户
New password:
Re-type new password:
Adding password for user gary
[root@localhost httpd]# more /etc/httpd/passwords
macg:U8jCwSsZyAB2g
gary:06yCDyg7AijlM
  •   在受限制目录下建立.htaccess文件
[root@localhost test]# ls -a
 ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  .htaccess  slade1.html  slade2.html  slade3.html  slade4.html slade5.html
[root@localhost test]# more .htaccess
authType Basic
authType--------认证类型
     由mod_auth_basic提供的Basic
Basic认证方法并不加密来自用户浏览器的密码(明文传输)
     更安全的认证方法"AuthType Digest",即由mod_auth_digest供的摘要认证
最新的浏览器版本才支持MD5认证
(认证,服务器响应速度会受一些影响,一般有几百个用户就会对响应速度有非常明显的影响)


AuthName "Restricted Files"
AuthName "会员区"
此句是显示给用户看的

AuthUserFile /etc/httpd/passwords
此目录接受passwords内定义用户的认证请求
or
Require macg
此目录只接受单一用户macg(unix用户)认证请求



    <Directory ></Directory> 中指令的含义
<Directory "/home/macg/www/test">   
        Options All            
        AllowOverride all                      
</Directory>
  Options指令-------目录的访问特性
option  none    禁止对目录的所有操作
option all      允许对目录的所有操作,ALL---所有操作
option ExecCGI    对该目录,可以执行cgi脚本
option Indexes    允许访问该目录(而该目录没有index.html)时,返回目录下的文件列表                        
option FollowSymLinks       只允许对目录的FollowSymLinks操作
   AllowOverride指令
None    不读取.htaccess
all    all----允许.htaccess所有指令,缺省是all
Limit    .htaccess函盖具体限定的主机(allow,deny)
AuthConfig    .htaccess函盖跟认证有关指令(AuthType,AuthName) 



   <Directory ></Directory> 对下面的所有子目录也生效

   所以httpd.conf中先对根目录/进行配置,等于是设置缺省配置
httpd.conf中先对根目录/进行配置,等于是设置缺省配置

<Directory />                                  
    Options FollowSymLinks  禁止对目录的访问(option只允许对目录的FollowSymLinks操作)
    AllowOverride None      不读取.htaccess
    Order deny,allow        deny all
    Deny from all               

</Directory>


<Directory "/usr/local/apache2/htdocs">

    Options Indexes FollowSymLinks   只允许访问index和连接   
    AllowOverride None
    Order allow,deny       承接父目录(/)的deny all,这里也是deny all
    Allow from all           

</Directory>


    order allow deny -------------httpd.conf中封IP之类的操作
Apache模块 mod_authz_host   
<Directory /docroot>
Order Deny,Allow
Deny from ...
Allow from ...

</Directory> 
  注意顺序:
除了后面allow的,其他都deny
典型的封IP
Order Allow,Deny
Allow from all
Deny from 205.252.46.165            
注意顺序:
除了后面deny的,其他都allow

Deny from 192.168.2       典型的封网段



运行:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"/> <typeAliases> <package name="com.sqsf.javabean"/> </typeAliases> <environments default="a"> <environment id="a"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="mapper"/> </mappers> </configuration>为什么会出现:org.apache.ibatis.binding.BindingException: Type interface com.sqsf.mapper.UserMapper is not known to the MapperRegistry. at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47) at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:823) at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:291) at com.sqsf.util.UserTest.a(UserTest.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33) at org.junit.internal.runners.statements
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值