如何让浏览器无法直接访问某些页面?

时间:2016-10-04 15:10:05

标签: html jsp tomcat webpage

例如:我的网络应用程序中有两个文件:1] index.html 2] some.html(或jsp)

从浏览器中只能访问index.html,

所以如果我调用localhost:8080 / index.html,它应该返回实际页面,并且在加载时如果我指向(重定向)到some.html那么some.html页面应该显示,

如果我直接调用localhost:8080 / some.html,它应该抛出一个页面无法直接访问的错误,如果我在tomcat服务器中托管webapp,有没有办法实现这个目的?

2 个答案:

答案 0 :(得分:3)

一个常见的解决方案是将它们移到WEB-INF目录下。从这里开始,它们不可公开访问,但您可以将Servlet或其他控制器转发给他们

https://docs.oracle.com/cd/E21764_01/web.1111/e13712/configurewebapp.htm#WBAPP158

  

WEB-INF目录不是公共文档树的一部分   应用。不能提供WEB-INF目录中包含的文件   由容器直接发送给客户端。 然而,内容   WEB-INF目录使用getResource对servlet代码可见   和ServletContext上的getResourceAsStream()方法调用   包含/转发使用RequestDispatcher

另一种方法是将它们放在WEB-INF之外,并在web.xml中配置安全约束。对于eaxmple,如果你在{webapp-root} / pages:

中有它们
<security-constraint>
    <web-resource-collection>
        <web-resource-name>JSP Files</web-resource-name>
        <description>No direct access to JSP files</description>
        <url-pattern>/pages/*</url-pattern>
        <http-method>POST</http-method>
        <http-method>GET</http-method>
    </web-resource-collection>
    <auth-constraint>
        <description>No direct browser access to JSP files</description>
        <role-name>NobodyHasThisRole</role-name>
    </auth-constraint>
</security-constraint> 

答案 1 :(得分:1)

使用Filters并拒绝访问jsp's

public class FilterMyJsp implements Filter{
    public void  doFilter(ServletRequest request, ServletReponse response,                
       FilterChain chain) {
      HttpServletRequest req= (HttpServletRequest) request;
      req.getRequestDispather("HandleError.jsp").forward(request,response);
}
}

的Web.xml <filter> <filter-name>FilterMyJsp</filter-name> <filter-class>my.FilterMyJsp</filter-class> </filter> <filter-mapping> <filter-name>FilterMyJsp</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping>

网址格式 *会将此过滤器应用于每个jsp&#39。您可以使用相应的错误消息设计HandleError.jsp,当用户尝试访问其他页面时,将显示该错误消息。

相关问题