简体   繁体   English

Spring MVC资源映射到/ **

[英]spring mvc resources map to /**

I have the classic situation in a Spring web-app in which the webapp folder contains the resources folder with JS or CSS files for example. 我在Spring Web应用程序中遇到的经典情况是,其中webapp文件夹包含带有JS或CSS文件的资源文件夹。

Usually, I've seen that to map the access to these files we have to put this tag 通常,我已经看到要映射对这些文件的访问,我们必须将这个标签

<mvc:resources mapping="/resources/**" location="/resources/" />

in the spring web-app configuration xml. 在春季网络应用程序配置xml中。 I would like to know if there is some "versus" or risks that make me avoid to map them with the following : 我想知道是否存在一些“相对”或风险,使我避免使用以下内容来映射它们:

<mvc:resources mapping="/**" location="/resources/" />

such that every resource inside "resources" is mapped starting by "/" 这样,“ resources”中的每个资源都将以“ /”开头进行映射

As a consequence of this, for example I would import js files with 因此,例如,我将使用

<script type="text/javascript" src="/js/myfile.js">

rather than with 而不是与

  <script type="text/javascript" src="/resources/js/myfile.js">

At this moment, the only consequence I see is that other resources outside the "resources" folder are not being mapped. 目前,我看到的唯一结果是未映射“ resources”文件夹之外的其他资源。 But I ask me if there is also a security reason. 但是我问我是否还有安全原因。

This will map every path to the resource handler. 这会将每个路径映射到资源处理程序。 I'm not sure there are any security risks because every path will still just look in the resources folder. 我不确定是否存在任何安全风险,因为每个路径仍只会在resources文件夹中查找。 Presumably the resources folder content is not sensitive information. 大概资源文件夹的内容不是敏感信息。 But it's not flexible as you pointed out. 但正如您指出的那样,它并不灵活。

I believe @RequestMapping will take precedence over resource mappings, so you should be okay there. 我相信@RequestMapping将优先于资源映射,因此您应该可以。

But I'd advise against this because you can get the exact same behavior by mapping this way that won't cause problems with static content in the future: 但是我建议您不要这样做,因为通过这种方式映射,您可以获得完全相同的行为,这种行为将来不会导致静态内容出现问题:

<mvc:resources mapping="/js/**" location="/resources/js/" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM