Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringMVC:如何防止客户端直接访问JSP视图?_Spring_Jsp_Spring Mvc - Fatal编程技术网

SpringMVC:如何防止客户端直接访问JSP视图?

SpringMVC:如何防止客户端直接访问JSP视图?,spring,jsp,spring-mvc,Spring,Jsp,Spring Mvc,我正在使用SpringMVC制作一个简单的web应用程序 我的控制器请求映射如下所示: @RequestMapping(value = "index.html") public String index(Model model) { model.addAttribute("type", "index"); return "index"; } 当我使用时,请访问以下URL: http://localhost:8012/MyCloud/index.html 一切正常,我可以看到正

我正在使用SpringMVC制作一个简单的web应用程序

我的控制器请求映射如下所示:

@RequestMapping(value = "index.html")
public String index(Model model) {
    model.addAttribute("type", "index");
    return "index";
}
当我使用时,请访问以下URL:

http://localhost:8012/MyCloud/index.html
一切正常,我可以看到正确渲染的
/views/index.jsp

但是,如果我直接使用下面的URL访问
视图/index.jsp
文件,那么该URL确实是
可访问的
。还有一个难看的500服务器错误,因为显然没有设置名为
“type”
的属性,所以会抛出
NullReferenceException

http://localhost:8012/MyCloud/views/index.jsp
通过将请求映射到
*.html
URL,我想诱使我的客户相信他们正在访问纯html页面。但是,如果他们以某种方式设法知道我的JSP视图位置并直接访问它们,他们将看到丑陋的错误

我能阻止吗


我应该使用错误页面吗?

Servlet容器不会提供WEB-INF中的任何内容。通过将JSP放在那里,您可以通过在浏览器中按名称导航到JSP来阻止任何人直接访问JSP


因此,将所有JSP移动到WEB-INF文件夹,用户将无法直接访问URL,而控制器代码将能够使用它们正确呈现UI。

Servlet容器不会提供WEB-INF中的任何内容。将JSP放在那里,通过在浏览器中按名称导航到JSP,可以防止任何人直接访问JSP


因此,将所有JSP移动到WEB-INF文件夹,用户将无法直接访问URL,而控制器代码将能够使用它们正确呈现UI。

您可以在WEB.xml中配置错误页面

<error-page>
    <location>/general-error.html</location>
</error-page>

/general-error.html

您可以在web.xml中配置错误页面

<error-page>
    <location>/general-error.html</location>
</error-page>

/general-error.html

将JSP放在web inf下,它们不能被直接访问将JSP放在web inf下,它们不能被直接访问