使用https访问网站静态资源无法加载

最近发现公司m站使用https访问的时候,样式无法加载,F12查看发现js,css等样式还是以http开头的,并且提示
This request has been blocked; the content must be served over HTTPS,因为HTTPS 承载的页面上不允许出现 http 请求,所以遇到http的样式就无法加载了。后台是使用 request.getScheme()获取的请求头

在这里插入图片描述
按照网上搜索的解决方案是在nginx添加
proxy_set_header X-Forwarded-Proto $scheme;
在这里插入图片描述
然后在tomcat的server.xml的engine模块下添加

在这里插入图片描述
在这里插入图片描述
至此,本地使用https访问的时候静态资源也是https了。问题得以解决
但是在部署上线的时候发现线上加载的静态资源还是http的。本地验证通过,服务器没有生效,也就是说request.getScheme()获取的请求头还是http
后面改成
request.getHeader(“X-Forwarded-Proto”),部署到线上才正确识别到时https

在这里插入图片描述
总结下来就是:
request.getScheme()在localhost已经能正确识别到是http还是https,但是线上必须要用request.getHeader(“X-Forwarded-Proto”)才能识别,反正就是用request.getScheme()不行的话就考虑用request.getHeader(“X-Forwarded-Proto”)去获取正确的协议。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 静态资源默认是放在 `/static`,`/public`,`/resources`,`/META-INF/resources` 目录下的,这些资源在应用启动时会被加载到内存中供访问。如果需要动态加载静态资源,可以使用 `ResourceHttpRequestHandler`。 下面是一个简单示例: 1. 创建 `DynamicResourceController` 控制器类,该类负责动态加载静态资源: ```java @Controller public class DynamicResourceController { @Autowired private ResourceLoader resourceLoader; @RequestMapping("/dynamic/**") public ResponseEntity<Resource> getDynamicResource(HttpServletRequest request) throws IOException { String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String bestMatchingPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); AntPathMatcher apm = new AntPathMatcher(); String relativePath = apm.extractPathWithinPattern(bestMatchingPattern, path); Resource resource = resourceLoader.getResource("classpath:dynamic/" + relativePath); if (!resource.exists()) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } return ResponseEntity.ok(resource); } } ``` 2. 在 `application.properties` 中配置静态资源路径: ``` spring.mvc.static-path-pattern=/static/** ``` 3. 在 `WebMvcConfigurer` 中注册 `ResourceHttpRequestHandler`: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); registry.addResourceHandler("/dynamic/**") .addResourceLocations("classpath:/dynamic/") .resourceChain(false) .addResolver(new PathResourceResolver() { @Override protected Resource getResource(String resourcePath, Resource location) throws IOException { Resource requestedResource = location.createRelative(resourcePath); if (requestedResource.exists() && requestedResource.isReadable()) { return requestedResource; } else if (resourcePath.contains(".")) { String pathPrefix = resourcePath.substring(0, resourcePath.lastIndexOf(".")); String pathSuffix = resourcePath.substring(resourcePath.lastIndexOf(".")); Resource indexResource = location.createRelative(pathPrefix + ".html"); if (indexResource.exists() && indexResource.isReadable()) { return indexResource; } } return new ClassPathResource("/dynamic/index.html"); } }); } } ``` 4. 在 `/dynamic` 目录下创建静态资源文件,例如 `/dynamic/index.html`。 5. 启动应用,访问 `http://localhost:8080/dynamic/index.html` 即可动态加载静态资源。 以上就是使用 `ResourceHttpRequestHandler` 实现动态加载静态资源的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值