指定敏感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
- starter-web
- starter-eureka
- 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)
就可完美的上传啦!