tomcat中禁止某类文件的访问
2014-03-27 10:46
417 查看
有时候为了网站的健壮性或者安全性,我们需要禁止直接对开发目录的某类文件的访问.或者对某个文件夹下面的文件进行访问.
如果是tomcat的话,通过设置安全认证可以实现.
web.xml中配置例子如下:
<!-- 权限管理 -->
<security-constraint>
<!-- 设置要保护的资源 -->
<web-resource-collection>
<web-resource-name>Members Only</web-resource-name>
<url-pattern>/membersonly/*</url-pattern>
<url-pattern>[URL类型2]</url-pattern>
<http-method>[HTTP方法1]</http-method>
<http-method>[HTTP方法2]</http-method>
</web-resource-collection>
<!-- 设置访问角色 -->
<auth-constraint>
<role-name>user</role-name>
<role-name>[角色2]</role-name>
</auth-constraint>
</security-constraint>
<!-- 认证方法 -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Members Only</realm-name>
</login-config>
<!-- 网站角色-->
<security-role>
<role-name>user</role-name>
</security-role>
<security-role>
<role-name>角色2</role-name>
</security-role>
如果想对某类文件禁止访问,我们这样设置<url-pattern>/'*.jsp</url-pattern>.
这样的话url里面只要有jsp就会被拒绝访问.对某些安全性要求较高的网站来说不失为一种好的办法.
===================
你如下设置就可以了
Tomcat_Homt\conf\tomcat-users.xml 文件内容如下,注意其中的employee和web.xml中的employee是对应的。
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="tomcat"/>
<role rolename="admin"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="lizongbo" password="lizongbopass" roles="admin,manager"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>
你的Webapp下WEB-INF\web.xml内容
<display-name>Tomcat Server Configuration Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>/images/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<!-- Login configuration uses form-based authentication -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Tomcat Server Configuration Form-Based Authentication Area</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description>
The role that is required to log in to the Administration Application
</description>
<role-name>admin</role-name>
</security-role>
如果是tomcat的话,通过设置安全认证可以实现.
web.xml中配置例子如下:
<!-- 权限管理 -->
<security-constraint>
<!-- 设置要保护的资源 -->
<web-resource-collection>
<web-resource-name>Members Only</web-resource-name>
<url-pattern>/membersonly/*</url-pattern>
<url-pattern>[URL类型2]</url-pattern>
<http-method>[HTTP方法1]</http-method>
<http-method>[HTTP方法2]</http-method>
</web-resource-collection>
<!-- 设置访问角色 -->
<auth-constraint>
<role-name>user</role-name>
<role-name>[角色2]</role-name>
</auth-constraint>
</security-constraint>
<!-- 认证方法 -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Members Only</realm-name>
</login-config>
<!-- 网站角色-->
<security-role>
<role-name>user</role-name>
</security-role>
<security-role>
<role-name>角色2</role-name>
</security-role>
如果想对某类文件禁止访问,我们这样设置<url-pattern>/'*.jsp</url-pattern>.
这样的话url里面只要有jsp就会被拒绝访问.对某些安全性要求较高的网站来说不失为一种好的办法.
===================
你如下设置就可以了
Tomcat_Homt\conf\tomcat-users.xml 文件内容如下,注意其中的employee和web.xml中的employee是对应的。
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="manager"/>
<role rolename="tomcat"/>
<role rolename="admin"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="lizongbo" password="lizongbopass" roles="admin,manager"/>
<user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>
你的Webapp下WEB-INF\web.xml内容
<display-name>Tomcat Server Configuration Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>/images/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<!-- Login configuration uses form-based authentication -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Tomcat Server Configuration Form-Based Authentication Area</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description>
The role that is required to log in to the Administration Application
</description>
<role-name>admin</role-name>
</security-role>
相关文章推荐
- Weblogic/Tomcat服务器(过滤)禁止访问指定后缀名文件
- Weblogic Tomcat服务器 (过滤)禁止访问指定后缀名文件
- tomcat中禁止某类文件的访问
- tomcat 下的虚拟目录 禁止通过url访问到文件列表
- tomcat 禁止某些文件(夹)的访问
- Apache通过配置.htaccess文件禁止访问.git、.svn等目录
- Android虚拟机访问tomcat服务器下的文件
- 如何使用HOSTS文件禁止用户访问特定的站点?
- 有关Tomcat 8.0版本文件上传后无权限访问的问题
- apache禁止访问文件或目录执行权限、禁止运行脚本PHP文件的设置方法
- tomcat 访问文件夹地址时显示目录下所有文件或文件夹
- tomcat部署FineReport成功,访问reportlets下的自带模板cpt文件提示数据集配置错误
- WEB项目tomcat容器下 java访问自定义的资源文件xml
- Tomcat中 的jsp文件访问原理?
- nginx 配置禁止访问目录或文件
- php禁止客户端访问目录文件
- Apache下禁止php文件被直接访问的解决方案
- Apache下文件和目录禁止访问安全设置
- 对于一个网站,如何禁止直接从浏览器Web browser中访问js文件
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法