腾讯云
开发者社区
文档
建议反馈
控制台
登录/注册
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
搜索
搜索
关闭
发布
精选内容/技术社群/优惠产品,
尽在小程序
立即前往
首页
标签
templates
#
templates
关注
专栏文章
(68)
技术视频
(0)
互动问答
(3)
怎么让Spring Boot中 static 和 templates 下的文件不能被直接访问到
1
回答
spring
、
boot
、
static
、
templates
gavin1024
要阻止对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文件夹下的文件的直接访问,并通过控制器来控制访问权限。...
展开详请
赞
0
收藏
0
评论
0
分享
要阻止对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会报错啊?
0
回答
templates
对dokuwiki新增page时templates的设定?
0
回答
templates
、
txt
、
wiki
、
插件
、
服务器
热门
专栏
AutoML(自动机器学习)
393 文章
58 订阅
测试开发架构之路
241 文章
64 订阅
程序猿DD
1.9K 文章
85 订阅
Albert陈凯
530 文章
55 订阅
磐创AI技术团队的专栏
726 文章
125 订阅
领券