Пример #1
0
 @Bean
 public CorsFilter corsFilter() {
   UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
   CorsConfiguration config = props.getCors();
   if (config.getAllowedOrigins() != null && !config.getAllowedOrigins().isEmpty()) {
     source.registerCorsConfiguration("/api/**", config);
     source.registerCorsConfiguration("/v2/api-docs", config);
     source.registerCorsConfiguration("/oauth/**", config);
   }
   return new CorsFilter(source);
 }
 @Bean
 public CorsFilter corsFilter() {
   UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
   CorsConfiguration config = new CorsConfiguration();
   config.setAllowCredentials(true);
   config.addAllowedOrigin("*");
   config.addAllowedHeader("*");
   config.addAllowedMethod("GET");
   config.addAllowedMethod("PUT");
   source.registerCorsConfiguration("/**", config);
   return new CorsFilter(source);
 }
  @Override
  public Mono<Object> getHandler(ServerWebExchange exchange) {
    return getHandlerInternal(exchange)
        .map(
            handler -> {
              if (CorsUtils.isCorsRequest(exchange.getRequest())) {
                CorsConfiguration configA =
                    this.globalCorsConfigSource.getCorsConfiguration(exchange);
                CorsConfiguration configB = getCorsConfiguration(handler, exchange);
                CorsConfiguration config = (configA != null ? configA.combine(configB) : configB);

                if (!getCorsProcessor().processRequest(config, exchange)
                    || CorsUtils.isPreFlightRequest(exchange.getRequest())) {
                  return REQUEST_HANDLED_HANDLER;
                }
              }
              return handler;
            });
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Before
  public void before() throws Exception {
    myFhirCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
    myFhirCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);

    if (ourServer == null) {
      ourPort = RandomServerPortProvider.findFreePort();

      ourRestServer = new RestfulServer(myFhirCtx);

      ourServerBase = "http://localhost:" + ourPort + "/fhir/context";

      ourRestServer.setResourceProviders((List) myResourceProviders);

      ourRestServer
          .getFhirContext()
          .setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());

      myTerminologyUploaderProvider = myAppCtx.getBean(TerminologyUploaderProviderDstu3.class);

      ourRestServer.setPlainProviders(mySystemProvider, myTerminologyUploaderProvider);

      JpaConformanceProviderDstu3 confProvider =
          new JpaConformanceProviderDstu3(ourRestServer, mySystemDao, myDaoConfig);
      confProvider.setImplementationDescription("THIS IS THE DESC");
      ourRestServer.setServerConformanceProvider(confProvider);

      ourRestServer.setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class));

      Server server = new Server(ourPort);

      ServletContextHandler proxyHandler = new ServletContextHandler();
      proxyHandler.setContextPath("/");

      ServletHolder servletHolder = new ServletHolder();
      servletHolder.setServlet(ourRestServer);
      proxyHandler.addServlet(servletHolder, "/fhir/context/*");

      ourWebApplicationContext = new GenericWebApplicationContext();
      ourWebApplicationContext.setParent(myAppCtx);
      ourWebApplicationContext.refresh();
      //			ContextLoaderListener loaderListener = new ContextLoaderListener(webApplicationContext);
      //			loaderListener.initWebApplicationContext(mock(ServletContext.class));
      //
      proxyHandler
          .getServletContext()
          .setAttribute(
              WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
              ourWebApplicationContext);

      DispatcherServlet dispatcherServlet = new DispatcherServlet();
      //			dispatcherServlet.setApplicationContext(webApplicationContext);
      dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
      ServletHolder subsServletHolder = new ServletHolder();
      subsServletHolder.setServlet(dispatcherServlet);
      subsServletHolder.setInitParameter(
          ContextLoader.CONFIG_LOCATION_PARAM, WebsocketDstu3Config.class.getName());
      proxyHandler.addServlet(subsServletHolder, "/*");

      // Register a CORS filter
      CorsConfiguration config = new CorsConfiguration();
      CorsInterceptor corsInterceptor = new CorsInterceptor(config);
      config.addAllowedHeader("x-fhir-starter");
      config.addAllowedHeader("Origin");
      config.addAllowedHeader("Accept");
      config.addAllowedHeader("X-Requested-With");
      config.addAllowedHeader("Content-Type");
      config.addAllowedHeader("Access-Control-Request-Method");
      config.addAllowedHeader("Access-Control-Request-Headers");
      config.addAllowedOrigin("*");
      config.addExposedHeader("Location");
      config.addExposedHeader("Content-Location");
      config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
      ourRestServer.registerInterceptor(corsInterceptor);

      server.setHandler(proxyHandler);
      server.start();

      WebApplicationContext wac =
          WebApplicationContextUtils.getWebApplicationContext(
              subsServletHolder.getServlet().getServletConfig().getServletContext());
      myValidationSupport = wac.getBean(JpaValidationSupportChainDstu3.class);

      ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase);
      ourClient.registerInterceptor(new LoggingInterceptor(true));

      PoolingHttpClientConnectionManager connectionManager =
          new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
      HttpClientBuilder builder = HttpClientBuilder.create();
      builder.setConnectionManager(connectionManager);
      ourHttpClient = builder.build();

      ourServer = server;
    }
  }