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

建设网站是什么样的太原整站优化排名外包

建设网站是什么样的,太原整站优化排名外包,中国建设网站齐齐哈尔市,属于网页制作平台安全文件下载系统设计与实现 系统概述 本文介绍了一种基于Spring Boot的安全文件下载系统,该系统通过动态生成的令牌来保护文件下载,避免了静态URL带来的安全隐患。系统主要特点包括: 动态令牌机制:每个下载链接都有时效性 安全…

安全文件下载系统设计与实现

  1. 系统概述
    本文介绍了一种基于Spring Boot的安全文件下载系统,该系统通过动态生成的令牌来保护文件下载,避免了静态URL带来的安全隐患。系统主要特点包括:

动态令牌机制:每个下载链接都有时效性

安全验证:检查令牌有效性、用户权限和文件路径

灵活存储:支持内存存储和外部存储(如Redis)

多种文件来源:支持本地文件、内存文件和流式文件

  1. 核心组件设计
    2.1 DownloadToken类
public class DownloadToken {private final String token;private final String fileName;private final Instant expiresAt;private final String username;public DownloadToken(String fileName, Duration validFor, String username) {this.token = UUID.randomUUID().toString();this.fileName = fileName;this.expiresAt = Instant.now().plus(validFor);this.username = username;}public boolean isExpired() {return Instant.now().isAfter(expiresAt);}// Getterspublic String getToken() { return token; }public String getFileName() { return fileName; }public Instant getExpiresAt() { return expiresAt; }public String getUsername() { return username; }
}

2.2 TokenStore接口及实现

public interface TokenStore {void store(String token, DownloadToken downloadToken);DownloadToken getToken(String token);void removeToken(String token);
}// 内存实现
public class MemoryTokenStore implements TokenStore {private final static Map<String, DownloadToken> tokens = new ConcurrentHashMap<>();@Overridepublic void store(String token, DownloadToken downloadToken) {tokens.putIfAbsent(token, downloadToken);}@Overridepublic DownloadToken getToken(String token) {return tokens.get(token);}@Overridepublic void removeToken(String token) {tokens.remove(token);}
}
  1. 控制器实现
    3.1 生成下载链接
@RestController
@RequestMapping("/download")
public class DownloadController {private final TokenStore tokenStore;public DownloadController(TokenStore tokenStore) {this.tokenStore = tokenStore;}@GetMapping("/request-download")public ResponseEntity<String> createDownloadLink(@RequestParam String fileName) {// 验证文件路径安全Path baseDir = Paths.get("d:/images").toAbsolutePath().normalize();Path requestedPath = baseDir.resolve(fileName).normalize();if (!requestedPath.startsWith(baseDir)) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();}DownloadToken token = new DownloadToken(fileName, Duration.ofMinutes(5), "pack");tokenStore.store(token.getToken(), token);String link = "/download/" + token.getToken();return ResponseEntity.ok(link);}
}

3.2 处理文件下载

@GetMapping("/{token}")
public ResponseEntity<Resource> download(@PathVariable String token) {DownloadToken stored = tokenStore.getToken(token);// 验证令牌if (stored == null || stored.isExpired()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}// 验证文件路径Path baseDir = Paths.get("d:/images").toAbsolutePath().normalize();Path filePath = baseDir.resolve(stored.getFileName()).normalize();if (!filePath.startsWith(baseDir)) {return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();}Resource file = new FileSystemResource(filePath);if (!file.exists()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}// 一次性令牌可在此处移除// tokenStore.removeToken(token);return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"").contentType(MediaType.APPLICATION_OCTET_STREAM).body(file);
}
  1. 高级功能实现
    4.1 内存文件下载
@GetMapping("/download-mem/{token}")
public ResponseEntity<byte[]> downloadFromMemory(@PathVariable String token) {DownloadToken downloadToken = tokenStore.getToken(token);if (downloadToken == null || downloadToken.isExpired()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}byte[] content = createFileStream(downloadToken.getFileName());return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + downloadToken.getFileName() + "\"").contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(content.length).body(content);
}

4.2 流式文件下载

@GetMapping("/download-stream/{token}")
public ResponseEntity<Resource> streamDownload(@PathVariable String token) throws IOException {DownloadToken downloadToken = tokenStore.getToken(token);if (downloadToken == null || downloadToken.isExpired()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}InputStream stream = fetchFromCloudStorage(downloadToken.getFileName());Resource resource = new InputStreamResource(stream);return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + downloadToken.getFileName() + "\"").contentType(MediaType.APPLICATION_OCTET_STREAM).body(resource);
}

4.3 安全认证下载

@GetMapping("/download-secure/{token}")
public ResponseEntity<Resource> downloadWithUserCheck(@PathVariable String token,@AuthenticationPrincipal UserDetails currentUser) {DownloadToken downloadToken = tokenStore.get(token);if (downloadToken == null || downloadToken.isExpired()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}if (!downloadToken.getUsername().equals(currentUser.getUsername())) {return ResponseEntity.status(HttpStatus.FORBIDDEN).build();}Path filePath = Paths.get("files", downloadToken.getFileName()).normalize();Resource file = new FileSystemResource(filePath);if (!file.exists()) {return ResponseEntity.status(HttpStatus.NOT_FOUND).build();}return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
  1. 安全注意事项
    路径遍历防护:始终验证最终文件路径是否在允许的目录内
Path baseDir = Paths.get("d:/images").toAbsolutePath().normalize();
Path requestedPath = baseDir.resolve(fileName).normalize();
if (!requestedPath.startsWith(baseDir)) {// 拒绝请求
}
令牌有效期:为每个令牌设置合理的有效期
new DownloadToken(fileName, Duration.ofMinutes(5), "pack");

用户绑定:将下载令牌与特定用户绑定,防止令牌共享
一次性令牌:对于高敏感文件,可使用后立即删除令牌
6. 扩展建议
存储优化:对于生产环境,建议使用Redis等外部存储代替内存存储
下载统计:记录下载次数、用户等信息用于审计
速率限制:防止用户频繁生成下载链接
内容加密:对于高敏感文件,可考虑在传输前加密

http://www.dtcms.com/wzjs/417744.html

相关文章:

  • 哪个网站做职客比较好 比较正规北京已感染上千万人
  • 龙岗营销网站建设嵌入式培训机构哪家好
  • 公司做网站需要提供什么资料搜狗首页排名优化
  • 哪家app软件开发公司好成都关键词seo推广电话
  • 给一个网站做需求分析网络营销的具体形式种类
  • 中国建设银行网站企业登陆推广方案是什么
  • 网站开发前期方案seo到底是做什么的
  • 游戏类网站欣赏杭州关键词排名系统
  • 电子商务网站建设与维护的主要内容摘抄一小段新闻
  • 漯河网页设计seo门户
  • 使用什么工具什么步骤完成网站的设计与建设百度有哪些产品
  • 廊坊网站建设价格营销渠道分为三种模式
  • 安宁网站建设百度搜索引擎竞价排名
  • 网站建设 业务员提成最近一周的时政热点新闻
  • 企业网站建设费用的预算优化网络推广外包
  • 阜新网站设计竞价广告是怎么推广的
  • 呼市品牌网站建设那家好淘宝指数官网
  • 哪个网站可以做行程表深圳关键词推广整站优化
  • 学做网站要编程关键词排名方案
  • 网站建设及发布的流程图软文标题和内容
  • 人脉推广app什么是seo关键词优化
  • 商汇通网站百度怎么做广告推广
  • 云南网站定制网页设计的流程
  • 做网站一定要有营业执照吗短视频询盘获客系统
  • 馨端网站建设软文发布平台
  • 做网站需要的大图百度一下官网页
  • 八上电脑课做网站需要什么软件广告联盟平台自动赚钱
  • 做公司网站写什么信息平台优化是指什么
  • 求推荐在哪个网站做德语翻译员知乎关键词排名优化工具
  • 海口市住房和城乡建设局 网站网络推广员怎么做