栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 2.6.+中文资源名称无法访问,英文正常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 2.6.+中文资源名称无法访问,英文正常

原因:

从2.6.0开始Spring MVC 处理程序映射匹配请求路径的默认策略已从 AntPathMatcher 更改为PathPatternParser。
基本可以确定是这个更改导致的,不知道是不是bug,更改之后具体的不知道改动了哪些,能力有限,暂时未知

一点分析:

从addResourceHandlers追踪,最终发现会到ResourceHttpRequestHandler,通过ResourceHttpRequestHandler调试发现,在使用PathPatternParser 后,现在传进来的url是原始的未decode过的url,但是UrlPathHelper 默认设置是decodeURL的,这就导致重复进行了一次encode

----------ResourceHttpRequestHandler.java-------------
    @Nullable
    protected Resource getResource(HttpServletRequest request) throws IOException {
        //这里获取的path是原始的encode的URL,而小于2.6版本会根据UrlPathHelper 里设置的decodeurl会有不同的值
        String path = (String)request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        if (path == null) {
            throw new IllegalStateException("Required request attribute '" + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE + "' is not set");
        } else {
            path = this.processPath(path);
            if (StringUtils.hasText(path) && !this.isInvalidPath(path)) {
                if (this.isInvalidEncodedPath(path)) {
                    return null;
                } else {
                    Assert.notNull(this.resolverChain, "ResourceResolverChain not initialized.");
                    Assert.notNull(this.transformerChain, "ResourceTransformerChain not initialized.");
                    //这里调用到PathResourceResolver
                    Resource resource = this.resolverChain.resolveResource(request, path, this.getLocations());
                    if (resource != null) {
                        resource = this.transformerChain.transform(request, resource);
                    }

                    return resource;
                }
            } else {
                return null;
            }
        }
    }
-----------PathResourceResolver.java-----------------

private String encodeOrDecodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) {
        if (this.shouldDecodeRelativePath(location, request)) {
            return UriUtils.decode(path, StandardCharsets.UTF_8);
        } else if (this.shouldEncodeRelativePath(location) && request != null) {//默认时decode
            Charset charset = (Charset)this.locationCharsets.getOrDefault(location, StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            StringTokenizer tokenizer = new StringTokenizer(path, "/");

            while(tokenizer.hasMoreTokens()) {
                String value = UriUtils.encode(tokenizer.nextToken(), charset);
                sb.append(value);
                sb.append('/');
            }

            if (!path.endsWith("/")) {
                sb.setLength(sb.length() - 1);
            }

            return sb.toString();
        } else {
            return path;
        }
    }
private boolean shouldEncodeRelativePath(Resource location) {
        return location instanceof UrlResource && this.urlPathHelper != null && this.urlPathHelper.isUrlDecode();
    }

这就导致在AbstractFileResolvingResource.java getFile的时候获取不到文件了

public File getFile() throws IOException {
        URL url = this.getURL();
        return url.getProtocol().startsWith("vfs") ? AbstractFileResolvingResource.VfsResourceDelegate.getResource(url).getFile() : ResourceUtils.getFile(url, this.getDescription());
    }

因为这里ResourceUtils去获取文件时解码出来的是我们请求的原始的encodeurl,还需要在decode一次才是真正的文件名,所以我们可以关闭decode,但是这样会影响到哪些地方 未知

解决办法: 1.UrlPathHelper 设置不decodeurl
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper=new UrlPathHelper();
        urlPathHelper.setUrlDecode(false);
        urlPathHelper.setDefaultEncoding(StandardCharsets.UTF_8.name());
        configurer.setUrlPathHelper(urlPathHelper);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  registry.addResourceHandler("/files/**").addResourceLocations("file:E:/FileUpload/HmiInterface/");
    }
}
2.使用原来的AntPathMatcher
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
能力有限,往上不是很好去调试了,不知道这算不算是bug吧,等待后续观察。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/703912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号