有时候为了网站的健壮性或者安全性,我们需要禁止直接对开发目录的某类文件的访问.或者对某个文件夹下面的文件进行访问.
如果是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>