首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
首页标签templates

#templates

怎么让Spring Boot中 static 和 templates 下的文件不能被直接访问到

要阻止对Spring Boot中static和templates文件夹下的文件的直接访问,您可以通过以下方法实现: 1. 在`application.properties`或`application.yml`文件中,添加以下配置: ``` spring.resources.add-mappings=false ``` 这将禁用静态资源的自动映射,从而阻止对static和templates文件夹下的文件的直接访问。 2. 创建一个自定义的WebMvcConfigurer,以便在其中配置静态资源的访问权限。在您的Spring Boot项目中,创建一个新的Java类,如下所示: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CustomWebMvcConfigurer implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .resourceChain(true); } } ``` 这将配置静态资源的访问权限,使得只有通过控制器暴露的资源才能被访问。 3. 在您的控制器中,使用`@RequestMapping`或`@GetMapping`注解创建一个新的映射,以便允许访问static和templates文件夹下的文件。例如: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/files") public class FileController { @GetMapping("/{filename}") public String getFile(@PathVariable String filename) { return "forward:/" + filename; } } ``` 这将允许通过`/files/{filename}`访问static和templates文件夹下的文件。 通过这些步骤,您可以阻止对Spring Boot中static和templates文件夹下的文件的直接访问,并通过控制器来控制访问权限。... 展开详请
要阻止对Spring Boot中static和templates文件夹下的文件的直接访问,您可以通过以下方法实现: 1. 在`application.properties`或`application.yml`文件中,添加以下配置: ``` spring.resources.add-mappings=false ``` 这将禁用静态资源的自动映射,从而阻止对static和templates文件夹下的文件的直接访问。 2. 创建一个自定义的WebMvcConfigurer,以便在其中配置静态资源的访问权限。在您的Spring Boot项目中,创建一个新的Java类,如下所示: ```java import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class CustomWebMvcConfigurer implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .resourceChain(true); } } ``` 这将配置静态资源的访问权限,使得只有通过控制器暴露的资源才能被访问。 3. 在您的控制器中,使用`@RequestMapping`或`@GetMapping`注解创建一个新的映射,以便允许访问static和templates文件夹下的文件。例如: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/files") public class FileController { @GetMapping("/{filename}") public String getFile(@PathVariable String filename) { return "forward:/" + filename; } } ``` 这将允许通过`/files/{filename}`访问static和templates文件夹下的文件。 通过这些步骤,您可以阻止对Spring Boot中static和templates文件夹下的文件的直接访问,并通过控制器来控制访问权限。

大哥,为什么templates会报错啊?

对dokuwiki新增page时templates的设定?

领券