当前位置: 首页 > web >正文

若依Ruoyi中优先从本地文件加载静态资源

   1、Spring Boot方法说明       

在 Spring Boot 中,若要让addResourceHandlers优先加载 JAR 包内的资源,可通过合理设置资源位置的顺序来达成。因为 Spring 会按照资源位置配置的先后顺序进行搜索,所以把 JAR 包内的资源路径放在前面就能实现优先加载。

下面是一个配置示例:

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/",  // 优先加载JAR包内的资源"file:./static/"      // 再加载外部文件系统的资源).setCachePeriod(3600).setUseLastModified(true);}
}

配置说明
资源搜索顺序:Spring 会按照addResourceLocations方法中路径的顺序去搜索资源。在上述例子中,会先搜索 JAR 包内classpath:/static/路径下的资源,若找不到,才会去搜索外部文件系统file:./static/路径下的资源。
外部资源覆盖:要是你有覆盖 JAR 包内资源的需求,比如在测试或生产环境中,可以把外部资源路径添加到 JAR 包资源路径之后。
路径协议
       classpath:用于引用 JAR 包内的资源。
       file:用于引用文件系统中的资源。

2、若依中调整静态资源加载方式

调整ResourcesConfig的addResourceHandlers(ResourceHandlerRegistry registry)方法,模块位置在ruoyi-framework\src\main\java\com\ruoyi\framework\config\ResourcesConfig.java文件中。

 @Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry){//Spring Boot(static → public → resources → META-INF/resources)检查这些目录中的资源/** 本地文件上传路径 */registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/");/** swagger配置 */registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");/** logo和img文件加载优先级配置,文件访问权限,由ShiroConfig.java配置*/registry.addResourceHandler("/logo/**") //访问路径.addResourceLocations("file:./static/logo/", //优先加载外部文件系统的资源,路径必须以‘/’结尾"classpath:/static/logo/" //再加载JAR包内的资源);registry.addResourceHandler("/img/**").addResourceLocations("file:./static/img/","classpath:/static/img/");}

如果需要调整权限,比如logo文件在登录前允许访问,需要在ShiroConfig.java的

ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager)添加相应的过滤器,如:
 public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){CustomShiroFilterFactoryBean shiroFilterFactoryBean = new CustomShiroFilterFactoryBean();// Shiro的核心安全接口,这个属性是必须的shiroFilterFactoryBean.setSecurityManager(securityManager);// 身份认证失败,则跳转到登录页面的配置shiroFilterFactoryBean.setLoginUrl(loginUrl);// 权限认证失败,则跳转到指定页面shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);// Shiro连接约束配置,即过滤链的定义LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();// 对静态资源设置匿名访问filterChainDefinitionMap.put("/favicon.ico**", "anon");filterChainDefinitionMap.put("/ruoyi.png**", "anon");filterChainDefinitionMap.put("/irootech.png**", "anon");filterChainDefinitionMap.put("/static/**", "anon");filterChainDefinitionMap.put("/logo/**", "anon"); //logofilterChainDefinitionMap.put("/html/**", "anon");filterChainDefinitionMap.put("/css/**", "anon");filterChainDefinitionMap.put("/docs/**", "anon");filterChainDefinitionMap.put("/fonts/**", "anon");filterChainDefinitionMap.put("/img/**", "anon"); //imgfilterChainDefinitionMap.put("/ajax/**", "anon");filterChainDefinitionMap.put("/js/**", "anon");filterChainDefinitionMap.put("/ruoyi/**", "anon");filterChainDefinitionMap.put("/captcha/captchaImage**", "anon");filterChainDefinitionMap.put("/api/**", "anon");
...

3、文件位置

     在jar包运行目录下,创建如下两个目录,对应的文件就会覆盖jar包中的同名文件

/static/logo
/static/img

http://www.xdnf.cn/news/11960.html

相关文章:

  • 42、响应处理-【源码分析】-浏览器与PostMan内容协商完全适配
  • Shopify 主题开发:促销活动页面专属设计思路
  • 【计算机】计算机存储器的分类与特性
  • 300道GaussDB(WMS)题目及答案。
  • Cursor 工具项目构建指南:Java 21 环境下的 Spring Boot Prompt Rules 约束
  • AI 时代下语音与视频伪造的网络安全危机
  • 服务器中僵尸网络攻击是指什么?
  • 联通专线赋能,亿林网络裸金属服务器:中小企业 IT 架构升级优选方案
  • MySQL JSON 查询中的对象与数组技巧
  • 【网络安全】fastjson原生链分析
  • Python 中 kwargs.get() 方法详解
  • LabVIEW的MathScript Node 绘图功能
  • 【UE5 C++】通过文件对话框获取选择文件的路径
  • Unity与Excel表格交互热更方案
  • go语言学习 第4章:流程控制
  • 使用jstack排查CPU飙升的问题记录
  • CMap应用场景和例子
  • ABP-Book Store Application中文讲解 - Part 10: Book to Author Relation
  • 【Pandas】pandas DataFrame reset_index
  • HTTP常见的请求方法、响应状态码、接口规范介绍
  • c#开发AI模型对话
  • 回归任务和分类任务损失函数详解
  • 性能剖析:在 ABP 框架中集成 MiniProfiler 实现性能可视化诊断
  • 学习笔记(24): 机器学习之数据预处理Pandas和转换成张量格式[2]
  • 以人类演示视频为提示,学习可泛化的机器人策略
  • 鸿蒙仓颉语言开发实战教程:商城登录页
  • [网页五子棋][匹配模块]实现胜负判定,处理玩家掉线
  • 【C++】std::wstring` 和 C# 的 `String`(全称 `System.String`)
  • HRI-2025 | 大模型驱动的个性化可解释机器人人机交互研究
  • .Net Framework 4/C# 面向对象编程进阶