RouterFunctionConfiguration.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.gateway.config;
  18. import lombok.AllArgsConstructor;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springblade.gateway.props.AuthProperties;
  21. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  22. import org.springframework.context.annotation.Bean;
  23. import org.springframework.context.annotation.Configuration;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.http.HttpMethod;
  26. import org.springframework.http.HttpStatus;
  27. import org.springframework.http.server.reactive.ServerHttpRequest;
  28. import org.springframework.http.server.reactive.ServerHttpResponse;
  29. import org.springframework.web.cors.reactive.CorsUtils;
  30. import org.springframework.web.server.ServerWebExchange;
  31. import org.springframework.web.server.WebFilter;
  32. import org.springframework.web.server.WebFilterChain;
  33. import reactor.core.publisher.Mono;
  34. /**
  35. * 路由配置信息
  36. *
  37. * @author Chill
  38. */
  39. @Slf4j
  40. @Configuration
  41. @AllArgsConstructor
  42. @EnableConfigurationProperties({AuthProperties.class})
  43. public class RouterFunctionConfiguration {
  44. /**
  45. * 这里为支持的请求头,如果有自定义的header字段请自己添加
  46. */
  47. private static final String ALLOWED_HEADERS = "X-Requested-With, Tenant-Id, Blade-Auth, Content-Type, Authorization, credential, X-XSRF-TOKEN, token, username, client, knfie4j-gateway-request, knife4j-gateway-code, request-origion";
  48. private static final String ALLOWED_METHODS = "GET,POST,PUT,DELETE,OPTIONS,HEAD";
  49. private static final String ALLOWED_ORIGIN = "*";
  50. private static final String ALLOWED_EXPOSE = "*";
  51. private static final String MAX_AGE = "18000L";
  52. /**
  53. * 跨域配置
  54. */
  55. @Bean
  56. public WebFilter corsFilter() {
  57. return (ServerWebExchange ctx, WebFilterChain chain) -> {
  58. ServerHttpRequest request = ctx.getRequest();
  59. if (CorsUtils.isCorsRequest(request)) {
  60. ServerHttpResponse response = ctx.getResponse();
  61. HttpHeaders headers = response.getHeaders();
  62. headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
  63. headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
  64. headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
  65. headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
  66. headers.add("Access-Control-Max-Age", MAX_AGE);
  67. headers.add("Access-Control-Allow-Credentials", "true");
  68. if (request.getMethod() == HttpMethod.OPTIONS) {
  69. response.setStatusCode(HttpStatus.OK);
  70. return Mono.empty();
  71. }
  72. }
  73. return chain.filter(ctx);
  74. };
  75. }
  76. }