Spring-webmvc源码解析之PathResourceResolver

本文详细解读了资源路径与位置匹配的关键代码逻辑,包括路径判断与异常处理,阐述了资源获取过程中的核心判断机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于4.1.7.RELEASE

本类是resolver链的最后一个,所以不会对resolverchain做任何操作。

private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException {
   if (!resource.getClass().equals(location.getClass())) {
      return false;
   }
   String resourcePath;
   String locationPath;
   /*  此处对上面两个path进行判断赋值,为了简洁代码已删除 */
   if (!resourcePath.startsWith(locationPath)) {
      return false;
   }
   if (resourcePath.contains("%")) {
      // Use URLDecoder (vs UriUtils) to preserve potentially decoded UTF-8 chars...
      if (URLDecoder.decode(resourcePath, "UTF-8").contains("../")) {
         if (logger.isTraceEnabled()) {
            logger.trace("Resolved resource path contains \"../\" after decoding: " + resourcePath);
         }
         return false;
      }
   }
   return true;
}

关键代码是

if (!resourcePath.startsWith(locationPath)) {
      return false;
   }

这里判断资源路径是否与所给定的location匹配,如果不匹配则返回false导致checkResource方法返回false,从而使getResource方法返回null

protected Resource getResource(String resourcePath, Resource location) throws IOException {
   Resource resource = location.createRelative(resourcePath);
   if (resource.exists() && resource.isReadable()) {
      if (checkResource(resource, location)) {
         return resource;
      }
      else if (logger.isTraceEnabled()) {
         logger.trace(“somelog");
      }
   }
   return null;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值