8.6 zuul的安全与header 和 8.7 使用zuul上传文件

指定敏感header

同一个服务系统中共享header,为路由指定敏感header,防止外泄。

zuul:
  routes:
    microservice-provider-user:
        path: /users/**
        sensitive-headers: Cookie,Set-Cookie,Authorization
        url: https://downstream

全局指定敏感 header

zuul:
    sensitive-headers: Cookie,Set-Cookie,Authorization

忽略 丢弃Header

zuul:
ignored-headers: header1,header2
丢弃的不会传到 其他微服务中
zuul:
  ignoreSecurityHeaders: false//忽略header设置为false,在security项目可以这样

使用zuul上传文件

1MB 以内的无需做任何处理

大于10MB的,需要为上传路径 添加/zuul前缀。或 zuul.servlet-path自定义前缀。

假如配置了

zuul:
  routes:
    microservice-file-upload: /microsevice-file-upload/**

则使用 /zuul/microservice-file-upload/upload 上传文件

对于超大文件(例如500MB),需要提升超时设置

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000

编写上传文件的微服务

pom
  1. starter-web
  2. starter-eureka
  3. starter-actuator
开启eureka
@SpringBootApplication
@EnableEurekaClient
编写controller
@Controller
public class FileUploadController {
  /**
   * 上传文件
   * 测试方法:
   * 有界面的测试:http://localhost:8050/index.html
   * 使用命令:curl -F "file=@文件全名" localhost:8050/upload
   * ps.该示例比较简单,没有做IO异常、文件大小、文件非空等处理
   * @param file 待上传的文件
   * @return 文件在服务器上的绝对路径
   * @throws IOException IO异常
   */
  @RequestMapping(value = "/upload", method = RequestMethod.POST)
  public @ResponseBody String handleFileUpload(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
    byte[] bytes = file.getBytes();
    File fileToSave = new File(file.getOriginalFilename());
    FileCopyUtils.copy(bytes, fileToSave);
    return fileToSave.getAbsolutePath();
  }
}
yml
server:
  port: 8050
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
spring:
  application:
    name: microservice-file-upload
  http:
    multipart:
      max-file-size: 2000Mb      # Max file size,默认1M
      max-request-size: 2500Mb   # Max request size,默认10M
index.html
	<form method="POST" enctype="multipart/form-data" action="/upload">
		File to upload: 
		<input type="file" name="file">
		<input type="submit" value="Upload">
	</form>
请求访问:postman上传文件

http://localhost:8040/microservice-file-upload/upload //大于1M,不能上传要加zuul

http://localhost:8040/microservice-file-upload/upload 配置好后都是可以的

http://localhost:8040/zuul/microservice-file-upload/upload 配置好后都是可以的

http://localhost:8050/upload

网关项目的更改

更改hystrix的超时时间
# 上传大文件得将超时时间设置长一些,否则会报超时异常。以下几行超时设置来自http://cloud.spring.io/spring-cloud-static/Camden.SR3/#_uploading_files_through_zuul
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
配置一下网关的最大上传数量(加了/zuul/的请求,无需配置也行)
spring:
  application:
    name: microservice-gateway-zuul
  http:
    multipart:
      max-file-size: 2000Mb      # Max file size,默认1M
      max-request-size: 2500Mb   # Max request size,默认10M

如果不加/zuul/还不配置这些,网关报错:exceeds the configured maximum (10485760)

就可完美的上传啦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值