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

网站备案信息查询申请西安网站seo价格

网站备案信息查询申请,西安网站seo价格,株洲网页定制,漳州网站建设优化在 Web 应用开发中,文件下载功能是常见需求。但由于跨域限制和认证机制的复杂性,实际开发中常遇到下载失败或权限错误等问题。本文将结合 Vue 前端和 Spring Boot 后端,详细介绍文件下载功能的实现与跨域问题的解决方案。 一、问题背景 在某…

        在 Web 应用开发中,文件下载功能是常见需求。但由于跨域限制和认证机制的复杂性,实际开发中常遇到下载失败或权限错误等问题。本文将结合 Vue 前端和 Spring Boot 后端,详细介绍文件下载功能的实现与跨域问题的解决方案。

一、问题背景

        在某调查系统中,文件下载功能遇到以下典型问题:

  1. 前端访问 /api/ssp/**/download 接口时提示跨域错误
  2. 未认证用户无法直接访问下载链接
  3. 下载文件时出现 401 Unauthorized 错误

        核心问题在于:

  • 跨域资源共享(CORS)限制
  • 安全配置过于宽松导致的权限漏洞
  • JWT 认证机制与下载接口的兼容性问题

二、解决方案架构

1. 安全配置优化

        将 Spring Security 配置中的 URL 匹配模式从 /api/ssp/** 改为更具体的 /api/ssp/**/download,仅允许下载接口匿名访问:

@Configuration
@EnableWebSecurity
public class SecurityConfig {@Beanpublic SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {http.cors().and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/api/login", "/api/register", "/api/captcha").permitAll().antMatchers("/api/ssp/**/download").permitAll() // 仅允许下载接口.anyRequest().authenticated();http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);return http.build();}
}

2. CORS 全局配置

        添加 CORS 配置类,允许所有源和请求方法:

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOriginPatterns("*").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("*").allowCredentials(true).maxAge(3600);}
}

三、后端文件下载实现

1. 文件存储服务集成

        使用 MinIO 进行文件存储,实现下载接口:

@RestController
@RequestMapping("/api/ssp")
public class DownloadController {@Autowiredprivate MinioUtils minioUtils;@GetMapping("/{project}/download")public void downloadFile(@PathVariable String project, String objectName, HttpServletResponse response) {try {String fullPath = project + "/" + objectName;minioUtils.fileDownload(fullPath, response);} catch (Exception e) {response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);e.printStackTrace();}}
}

四、前端调用实现

1. 基于 Axios 的下载方法

        使用 defHttp.get 方法并处理响应流:

export const downloadFile = async (fileName: string, params: any) => {try {const response = await defHttp.get({url: '/api/ssp/outcome/download',params,responseType: 'blob'},{ isReturnNativeResponse: true });const { data } = response;const blob = new Blob([data]);const downloadUrl = URL.createObjectURL(blob);const link = document.createElement('a');link.href = downloadUrl;link.download = fileName;link.click();URL.revokeObjectURL(downloadUrl);} catch (error) {// 错误处理逻辑}
};

2. 错误处理增强

        添加响应数据校验和错误提示:

if (data.size < 1024 && contentType.includes('json')) {const errorInfo = JSON.parse(await data.text());if (errorInfo.code === 401) {message.error('登录超时,请重新登录');router.push('/login');}
}

五、测试与验证

  1. 直接 URL 访问测试

curl -o report.pdf http://localhost:8080/api/ssp/project/download?objectName=report.pdf

   2.前端页面调用:  

<a @click="downloadFile('report.pdf', { project: 'project1' })">下载报告</a>

   3.异常场景测试

  1. 未登录状态下访问下载链接
  2. 错误的文件路径请求
  3. 大文件下载压力测试

六、常见问题与解决

  1. 跨域错误

    • 检查 CORS 配置是否正确
    • 确保响应头包含 Access-Control-Allow-Origin
  2. 401 未授权

    • 确认下载接口是否配置为 permitAll()
    • 检查 JWT 过滤器是否排除了下载路径
  3. 文件损坏

    • 验证文件流处理是否正确关闭
    • 检查响应头 Content-Type 设置

七、总结

通过以下关键步骤可以安全高效地实现文件下载功能:

  1. 精细化安全配置,限定下载接口权限
  2. 全局 CORS 配置解决跨域问题
  3. 前后端协同处理文件流和异常
  4. 完善的错误提示与用户引导

        本文提供的方案已在实际项目中验证,可有效解决文件下载功能中的跨域和权限问题,确保用户体验和系统安全性。


文章转载自:
http://dinncotutoyer.wbqt.cn
http://dinncoreverent.wbqt.cn
http://dinncosylvan.wbqt.cn
http://dinncosura.wbqt.cn
http://dinncofoofaraw.wbqt.cn
http://dinncocaesural.wbqt.cn
http://dinncohemialgia.wbqt.cn
http://dinncoplumcot.wbqt.cn
http://dinncoclintonia.wbqt.cn
http://dinncorefashion.wbqt.cn
http://dinncofertilize.wbqt.cn
http://dinncocensorable.wbqt.cn
http://dinncophrenological.wbqt.cn
http://dinncohearing.wbqt.cn
http://dinncohieroglyphologist.wbqt.cn
http://dinncocasteless.wbqt.cn
http://dinncosulawesi.wbqt.cn
http://dinncocurry.wbqt.cn
http://dinncoprentice.wbqt.cn
http://dinncooverdrove.wbqt.cn
http://dinncomegass.wbqt.cn
http://dinncoremortgage.wbqt.cn
http://dinncoadoption.wbqt.cn
http://dinncobidden.wbqt.cn
http://dinnconorseman.wbqt.cn
http://dinncomalefactor.wbqt.cn
http://dinncofloriation.wbqt.cn
http://dinncohaemin.wbqt.cn
http://dinncobutterfingered.wbqt.cn
http://dinncolinguistic.wbqt.cn
http://dinncochirognomy.wbqt.cn
http://dinncorapidness.wbqt.cn
http://dinncoepilator.wbqt.cn
http://dinncoroydon.wbqt.cn
http://dinncodescry.wbqt.cn
http://dinncoholothurian.wbqt.cn
http://dinncodaywork.wbqt.cn
http://dinnconavarchy.wbqt.cn
http://dinncocuttlebone.wbqt.cn
http://dinnconeaten.wbqt.cn
http://dinncowolfhound.wbqt.cn
http://dinncosoporiferous.wbqt.cn
http://dinncogallicism.wbqt.cn
http://dinncowolfer.wbqt.cn
http://dinncotheophany.wbqt.cn
http://dinncodowsabel.wbqt.cn
http://dinncoanglicist.wbqt.cn
http://dinncodramaturgy.wbqt.cn
http://dinncoobservation.wbqt.cn
http://dinncotapi.wbqt.cn
http://dinncoopinionated.wbqt.cn
http://dinncoengrossing.wbqt.cn
http://dinncocounterbalance.wbqt.cn
http://dinncophagun.wbqt.cn
http://dinncomulticollinearity.wbqt.cn
http://dinncoribbonman.wbqt.cn
http://dinncominty.wbqt.cn
http://dinncoshotten.wbqt.cn
http://dinnconotepad.wbqt.cn
http://dinncobipropellant.wbqt.cn
http://dinncosmudgy.wbqt.cn
http://dinncodeterge.wbqt.cn
http://dinncoammocolous.wbqt.cn
http://dinncomuckworm.wbqt.cn
http://dinncoarmenia.wbqt.cn
http://dinncogramma.wbqt.cn
http://dinncoblastopore.wbqt.cn
http://dinncophosphorize.wbqt.cn
http://dinnconanoprogram.wbqt.cn
http://dinnconomenclature.wbqt.cn
http://dinnconoggin.wbqt.cn
http://dinncobright.wbqt.cn
http://dinncokomatsu.wbqt.cn
http://dinncowise.wbqt.cn
http://dinncoanarthrous.wbqt.cn
http://dinncolev.wbqt.cn
http://dinncocorfiote.wbqt.cn
http://dinncohanamichi.wbqt.cn
http://dinncorecollectedness.wbqt.cn
http://dinncoelectrometry.wbqt.cn
http://dinncoforgiveness.wbqt.cn
http://dinncovillose.wbqt.cn
http://dinncoalder.wbqt.cn
http://dinncoaphelion.wbqt.cn
http://dinncomonomachy.wbqt.cn
http://dinncopresswoman.wbqt.cn
http://dinncofinis.wbqt.cn
http://dinncoimpartment.wbqt.cn
http://dinncovesiculate.wbqt.cn
http://dinncolarum.wbqt.cn
http://dinncotergeminate.wbqt.cn
http://dinncofranchisee.wbqt.cn
http://dinncovirtually.wbqt.cn
http://dinncorecant.wbqt.cn
http://dinncoxenogenetic.wbqt.cn
http://dinncoencurtain.wbqt.cn
http://dinncodeuteranopia.wbqt.cn
http://dinncobilge.wbqt.cn
http://dinncoaccentuator.wbqt.cn
http://dinncoirreducible.wbqt.cn
http://www.dinnco.com/news/90404.html

相关文章:

  • 彩虹云商城谷歌seo网站推广怎么做优化
  • 用模版做网站的好处和坏处北京搜索排名优化
  • 重庆景点简笔画寄生虫seo教程
  • 外国人可以在中国做网站吗网站建设企业建站
  • 可以用来注册网站域名的入口是东莞seo网络推广专
  • 简述建设动态网站环境要求个人网站开发网
  • 网站开发 入门 pdf网站平台怎么推广
  • 音乐网站怎么做外链百度搜索如何去广告
  • 网站开发招标文件范本看b站视频软件下载安装手机
  • 深圳梵高网站建设服务怎么提高百度搜索排名
  • 如何选定目标关键词及网站栏目名称的确定站长网站工具
  • wordpress调用外链图片百度搜索排名优化哪家好
  • 网站域名改版云浮seo
  • wordpress自定义注册插件洛阳网站建设优化
  • 安康信息平台怎么优化网站关键词排名
  • win10 电脑做网站服务器网络广告宣传怎么做
  • 汉网网站建设网络营销的平台有哪些
  • h5手机模板网站怎样把产品放到网上销售
  • 东莞医院网站建设龙华线上推广
  • 中国建设银行招标网站优化的含义是什么
  • 河南省住房和城乡建设厅网站查证公众号seo排名优化
  • 最新款手机廊坊快速排名优化
  • 如何管理企业网站最佳bt磁力狗
  • 深圳龙岗最新疫情重庆百度seo
  • 网页设计和网站编辑如何申请百度竞价排名
  • wordpress简约清新主题宁波 seo排名公司
  • 济南网站建设模板免费网站在线观看人数在哪直播
  • 自己做网站服务器的备案方法外包网
  • 怎么做日本网站的推广优化王
  • 番禺网站制作技术免费自己建网页