org.springframework.web.server.WebFilter Java Examples

The following examples show how to use org.springframework.web.server.WebFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GatewayApplication.java    From MyShopPlus with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (!CorsUtils.isCorsRequest(request)) {
            return chain.filter(ctx);
        }
        HttpHeaders requestHeaders = request.getHeaders();
        ServerHttpResponse response = ctx.getResponse();
        HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
        HttpHeaders headers = response.getHeaders();
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
        headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
        if (requestMethod != null) {
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
        }
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
        headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
        headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
        if (request.getMethod() == HttpMethod.OPTIONS) {
            response.setStatusCode(HttpStatus.OK);
            return Mono.empty();
        }
        return chain.filter(ctx);
    };
}
 
Example #2
Source File: Bucket4JAutoConfigurationWebfluxFilter.java    From bucket4j-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void initFilters() {
	AtomicInteger filterCount = new AtomicInteger(0);
	properties
		.getFilters()
		.stream()
		.filter(filter -> !StringUtils.isEmpty(filter.getUrl()) && filter.getFilterMethod().equals(FilterMethod.WEBFLUX))
		.map(filter -> {
			filterCount.incrementAndGet();
			FilterConfiguration<ServerHttpRequest> filterConfig = buildFilterConfig(filter, cacheResolver.resolve(
					filter.getCacheName()), 
					webFilterExpressionParser(), 
					beanFactory);
			
			servletConfigurationHolder().addFilterConfiguration(filter);
			
			WebFilter webFilter = new WebfluxWebFilter(filterConfig);
	        
	        log.info("create-webflux-filter;{};{};{}", filterCount, filter.getCacheName(), filter.getUrl());
	        return webFilter;
		}).forEach(webFilter -> {
			context.registerBean("bucket4JWebfluxFilter" + filterCount, WebFilter.class, () -> webFilter);
		});
	
}
 
Example #3
Source File: GatewayConfiguration.java    From microservice-integration with MIT License 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (CorsUtils.isCorsRequest(request)) {
            ServerHttpResponse response = ctx.getResponse();
            HttpHeaders headers = response.getHeaders();
            headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
            headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
            headers.add("Access-Control-Max-Age", MAX_AGE);
            headers.add("Access-Control-Allow-Headers",ALLOWED_HEADERS);
            if (request.getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
        }
        return chain.filter(ctx);
    };
}
 
Example #4
Source File: WebConfiguration.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 6 votes vote down vote up
@Bean
public WebFilter indexHtmlFilter() {
   return (exchange, chain) -> {
      if (exchange.getRequest().getURI().getPath().equals("/")) {
         return chain.filter(exchange
            .mutate()
            .request(exchange
               .getRequest()
               .mutate()
               .path("/index.html")
               .build())
            .build());
      }
      return chain.filter(exchange);
   };
}
 
Example #5
Source File: CorsConfig.java    From microservice-recruit with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (CorsUtils.isCorsRequest(request)) {
            HttpHeaders requestHeaders = request.getHeaders();
            ServerHttpResponse response = ctx.getResponse();
            HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
            HttpHeaders headers = response.getHeaders();
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
            headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders
                    .getAccessControlRequestHeaders());
            if(requestMethod != null){
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
            }
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
            headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
            headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
            if (request.getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
        }
        return chain.filter(ctx);
    };
}
 
Example #6
Source File: WebFilterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testWebFilter() throws Exception {

	WebFilter filter = (exchange, chain) -> {
		DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
		buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
		return exchange.getResponse().writeWith(Mono.just(buffer));
	};

	WebTestClient client = WebTestClient.bindToWebHandler(exchange -> Mono.empty())
			.webFilter(filter)
			.build();

	client.get().uri("/")
			.exchange()
			.expectStatus().isOk()
			.expectBody(String.class).isEqualTo("It works!");
}
 
Example #7
Source File: WebHttpHandlerBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void updateFilters() {

		if (this.filters.isEmpty()) {
			return;
		}

		List<WebFilter> filtersToUse = this.filters.stream()
				.peek(filter -> {
					if (filter instanceof ForwardedHeaderTransformer && this.forwardedHeaderTransformer == null) {
						this.forwardedHeaderTransformer = (ForwardedHeaderTransformer) filter;
					}
				})
				.filter(filter -> !(filter instanceof ForwardedHeaderTransformer))
				.collect(Collectors.toList());

		this.filters.clear();
		this.filters.addAll(filtersToUse);
	}
 
Example #8
Source File: WebHttpHandlerBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void updateFilters() {

		if (this.filters.isEmpty()) {
			return;
		}

		List<WebFilter> filtersToUse = this.filters.stream()
				.peek(filter -> {
					if (filter instanceof ForwardedHeaderTransformer && this.forwardedHeaderTransformer == null) {
						this.forwardedHeaderTransformer = (ForwardedHeaderTransformer) filter;
					}
				})
				.filter(filter -> !(filter instanceof ForwardedHeaderTransformer))
				.collect(Collectors.toList());

		this.filters.clear();
		this.filters.addAll(filtersToUse);
	}
 
Example #9
Source File: CorsConfig.java    From microservice-recruit with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (CorsUtils.isCorsRequest(request)) {
            HttpHeaders requestHeaders = request.getHeaders();
            ServerHttpResponse response = ctx.getResponse();
            HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
            HttpHeaders headers = response.getHeaders();
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
            headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders
                    .getAccessControlRequestHeaders());
            if(requestMethod != null){
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
            }
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
            headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
            headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
            if (request.getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
        }
        return chain.filter(ctx);
    };
}
 
Example #10
Source File: WebFilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testWebFilter() throws Exception {

	WebFilter filter = (exchange, chain) -> {
		DataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
		buffer.write("It works!".getBytes(StandardCharsets.UTF_8));
		return exchange.getResponse().writeWith(Mono.just(buffer));
	};

	WebTestClient client = WebTestClient.bindToWebHandler(exchange -> Mono.empty())
			.webFilter(filter)
			.build();

	client.get().uri("/")
			.exchange()
			.expectStatus().isOk()
			.expectBody(String.class).isEqualTo("It works!");
}
 
Example #11
Source File: CorsConfig.java    From open-capacity-platform with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
	return (ServerWebExchange ctx, WebFilterChain chain) -> {
		ServerHttpRequest request = ctx.getRequest();
		if (!CorsUtils.isCorsRequest(request)) {
			return chain.filter(ctx);
		}
		HttpHeaders requestHeaders = request.getHeaders();
		ServerHttpResponse response = ctx.getResponse();
		HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
		HttpHeaders headers = response.getHeaders();
		headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
		headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
		if (requestMethod != null) {
			headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
		}
		headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
		headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
		headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
		if (request.getMethod() == HttpMethod.OPTIONS) {
			response.setStatusCode(HttpStatus.OK);
			return Mono.empty();
		}
		return chain.filter(ctx);
	};
}
 
Example #12
Source File: CorsConfig.java    From spring-microservice-exam with MIT License 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (!CorsUtils.isCorsRequest(request))
            return chain.filter(ctx);
        HttpHeaders requestHeaders = request.getHeaders();
        ServerHttpResponse response = ctx.getResponse();
        HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
        HttpHeaders headers = response.getHeaders();
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
        headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
        if (requestMethod != null)
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
        headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
        headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
        if (request.getMethod() == HttpMethod.OPTIONS) {
            response.setStatusCode(HttpStatus.OK);
            return Mono.empty();
        }
        return chain.filter(ctx);
    };
}
 
Example #13
Source File: CorsConfig.java    From simple-microservice with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
  return (ServerWebExchange ctx, WebFilterChain chain) -> {
    ServerHttpRequest request = ctx.getRequest();
    if (CorsUtils.isCorsRequest(request)) {
      HttpHeaders requestHeaders = request.getHeaders();
      ServerHttpResponse response = ctx.getResponse();
      HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
      HttpHeaders headers = response.getHeaders();
      headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
      headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
      if (requestMethod != null) {
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
      }
      headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
      headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
      headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
      if (request.getMethod() == HttpMethod.OPTIONS) {
        response.setStatusCode(HttpStatus.OK);
        return Mono.empty();
      }

    }
    return chain.filter(ctx);
  };
}
 
Example #14
Source File: ResourceServerConfiguration.java    From open-cloud with MIT License 6 votes vote down vote up
/**
 * 跨域配置
 *
 * @return
 */
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (CorsUtils.isCorsRequest(request)) {
            HttpHeaders requestHeaders = request.getHeaders();
            ServerHttpResponse response = ctx.getResponse();
            HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
            HttpHeaders headers = response.getHeaders();
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
            headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
            if (requestMethod != null) {
                headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
            }
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
            headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
            headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
            if (request.getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
        }
        return chain.filter(ctx);
    };
}
 
Example #15
Source File: CorsConfig.java    From spring-cloud-sofastack-samples with Apache License 2.0 6 votes vote down vote up
@Bean
public WebFilter corsFilter() {
    return (ServerWebExchange ctx, WebFilterChain chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (CorsUtils.isCorsRequest(request)) {
            ServerHttpResponse response = ctx.getResponse();
            HttpHeaders headers = response.getHeaders();
            headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN);
            headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS);
            headers.add("Access-Control-Allow-Headers", ALLOWED_HEADERS);
            headers.add("Access-Control-Expose-Headers", ALLOWED_EXPOSE);
            headers.add("Access-Control-Allow-Credentials", "true");
            if (request.getMethod() == HttpMethod.OPTIONS) {
                response.setStatusCode(HttpStatus.OK);
                return Mono.empty();
            }
        }
        return chain.filter(ctx);
    };
}
 
Example #16
Source File: DefaultWebFilterChain.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Public constructor with the list of filters and the target handler to use.
 * @param handler the target handler
 * @param filters the filters ahead of the handler
 * @since 5.1
 */
public DefaultWebFilterChain(WebHandler handler, List<WebFilter> filters) {
	Assert.notNull(handler, "WebHandler is required");
	this.allFilters = Collections.unmodifiableList(filters);
	this.handler = handler;
	DefaultWebFilterChain chain = initChain(filters, handler);
	this.currentFilter = chain.currentFilter;
	this.next = chain.next;
}
 
Example #17
Source File: DefaultWebFilterChain.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Private constructor to represent one link in the chain.
 */
private DefaultWebFilterChain(List<WebFilter> allFilters, WebHandler handler,
		@Nullable WebFilter currentFilter, @Nullable DefaultWebFilterChain next) {

	this.allFilters = allFilters;
	this.currentFilter = currentFilter;
	this.handler = handler;
	this.next = next;
}
 
Example #18
Source File: WebHttpHandlerBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Add the given filter(s).
 * @param filters the filter(s) to add that's
 */
public WebHttpHandlerBuilder filter(WebFilter... filters) {
	if (!ObjectUtils.isEmpty(filters)) {
		this.filters.addAll(Arrays.asList(filters));
		updateFilters();
	}
	return this;
}
 
Example #19
Source File: WebHttpHandlerBuilderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private WebFilter createFilter(String name) {
	return (exchange, chain) -> {
		String value = exchange.getAttribute(ATTRIBUTE);
		value = (value != null ? value + "::" + name : name);
		exchange.getAttributes().put(ATTRIBUTE, value);
		return chain.filter(exchange);
	};
}
 
Example #20
Source File: AbstractMockServerSpec.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T extends B> T webFilter(WebFilter... filters) {
	if (filters.length > 0) {
		this.filters = (this.filters != null ? this.filters : new ArrayList<>(4));
		this.filters.addAll(Arrays.asList(filters));
	}
	return self();
}
 
Example #21
Source File: SoulConfiguration.java    From soul with Apache License 2.0 5 votes vote down vote up
/**
 * init time web filter.
 *
 * @param soulConfig the soul config
 * @return {@linkplain TimeWebFilter}
 */
@Bean
@Order(30)
@ConditionalOnProperty(name = "soul.filterTimeEnable")
public WebFilter timeWebFilter(final SoulConfig soulConfig) {
    return new TimeWebFilter(soulConfig);
}
 
Example #22
Source File: CorsConfig.java    From black-shop with Apache License 2.0 5 votes vote down vote up
@Bean
public WebFilter corsFilter() {
	return (ServerWebExchange ctx, WebFilterChain chain) -> {
		ServerHttpRequest request = ctx.getRequest();
		if (CorsUtils.isCorsRequest(request)) {
			HttpHeaders requestHeaders = request.getHeaders();
			ServerHttpResponse response = ctx.getResponse();
			HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
			HttpHeaders headers = response.getHeaders();
			headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
			headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS,
					requestHeaders.getAccessControlRequestHeaders());
			if (requestMethod != null) {
				headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
			}
			headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
			headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "*");
			headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
			if (request.getMethod() == HttpMethod.OPTIONS) {
				response.setStatusCode(HttpStatus.OK);
				return Mono.empty();
			}

		}
		return chain.filter(ctx);
	};
}
 
Example #23
Source File: CorsConfiguration.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * attention:简单跨域就是GET,HEAD和POST请求,但是POST请求的"Content-Type"只能是application/x-www-form-urlencoded, multipart/form-data 或 text/plain
 * 反之,就是非简单跨域,此跨域有一个预检机制,说直白点,就是会发两次请求,一次OPTIONS请求,一次真正的请求
 */
@Bean
public WebFilter corsFilter() {
    return (ctx, chain) -> {
        ServerHttpRequest request = ctx.getRequest();
        if (!CorsUtils.isCorsRequest(request)) {
            return chain.filter(ctx);
        }
        HttpHeaders requestHeaders = request.getHeaders();
        ServerHttpResponse response = ctx.getResponse();
        HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod();
        HttpHeaders headers = response.getHeaders();
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin());
        headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders());
        if (requestMethod != null) {
            headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name());
        }
        headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
        headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL);
        headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE);
        if (request.getMethod() == HttpMethod.OPTIONS) {
            response.setStatusCode(HttpStatus.OK);
            return Mono.empty();
        }
        return chain.filter(ctx);
    };
}
 
Example #24
Source File: RouterFunctionsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void toHttpHandlerWebFilter() {
	AtomicBoolean filterInvoked = new AtomicBoolean();

	WebFilter webFilter = new WebFilter() {
		@Override
		public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
			filterInvoked.set(true);
			return chain.filter(exchange);
		}
	};

	HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.accepted().build();
	RouterFunction<ServerResponse> routerFunction =
			RouterFunctions.route(RequestPredicates.all(), handlerFunction);

	HandlerStrategies handlerStrategies = HandlerStrategies.builder()
			.webFilter(webFilter).build();

	HttpHandler result = RouterFunctions.toHttpHandler(routerFunction, handlerStrategies);
	assertNotNull(result);

	MockServerHttpRequest httpRequest = MockServerHttpRequest.get("http://localhost").build();
	MockServerHttpResponse httpResponse = new MockServerHttpResponse();
	result.handle(httpRequest, httpResponse).block();
	assertEquals(HttpStatus.ACCEPTED, httpResponse.getStatusCode());

	assertTrue(filterInvoked.get());
}
 
Example #25
Source File: DefaultHandlerStrategiesBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
public DefaultHandlerStrategies(
		List<HttpMessageReader<?>> messageReaders,
		List<HttpMessageWriter<?>> messageWriters,
		List<ViewResolver> viewResolvers,
		List<WebFilter> webFilters,
		List<WebExceptionHandler> exceptionHandlers,
		LocaleContextResolver localeContextResolver) {

	this.messageReaders = unmodifiableCopy(messageReaders);
	this.messageWriters = unmodifiableCopy(messageWriters);
	this.viewResolvers = unmodifiableCopy(viewResolvers);
	this.webFilters = unmodifiableCopy(webFilters);
	this.exceptionHandlers = unmodifiableCopy(exceptionHandlers);
	this.localeContextResolver = localeContextResolver;
}
 
Example #26
Source File: HttpSecurity.java    From spring-security-reactive with Apache License 2.0 5 votes vote down vote up
public WebFilter build() {
	List<WebFilter> filters = new ArrayList<>();
	if(headers != null) {
		filters.add(headers.build());
	}
	filters.add(securityContextRepositoryWebFilter());
	if(httpBasic != null) {
		httpBasic.authenticationManager(authenticationManager);
		filters.add(httpBasic.build());
	}
	if(authorizeRequest != null) {
		filters.add(authorizeRequest.build());
	}
	return new WebFilterChainFilter(filters);
}
 
Example #27
Source File: WebTestClientBuilder.java    From spring-security-reactive with Apache License 2.0 5 votes vote down vote up
private static ApplicationContext applicationContext(WebFilter... webFilters) {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	for(WebFilter filter : webFilters) {
		context.registerBean(WebFilter.class, () -> filter);
	}
	context.registerBean("webHandler", DispatcherHandler.class, () -> new DispatcherHandler());
	context.registerBean(HandlerMapping.class, () -> RouterFunctions.toHandlerMapping(request -> Mono.just(r -> ServerResponse.ok().build())));
	context.registerBean(HandlerAdapter.class, () -> new HandlerFunctionAdapter());
	context.registerBean(HandlerResultHandler.class, () -> new ServerResponseResultHandler());
	context.refresh();

	return context;
}
 
Example #28
Source File: Application.java    From spring-security-reactive with Apache License 2.0 5 votes vote down vote up
@Bean
WebFilter springSecurityFilterChain(ReactiveAuthenticationManager manager) throws Exception {
	HttpSecurity http = http();
	// FIXME use BeanPostProcessor to set the manager
	http.authenticationManager(manager);
	http.httpBasic();

	AuthorizeRequestBuilder authorize = http.authorizeRequests();
	authorize.antMatchers("/admin/**").hasRole("ADMIN");
	authorize.anyExchange().authenticated();
	return http.build();
}
 
Example #29
Source File: WingtipsSpringWebfluxComponentTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Bean
public WebFilter wingtipsWebFilter() {
    return WingtipsSpringWebfluxWebFilter
        .newBuilder()
        .withUserIdHeaderKeys(singletonList(USER_ID_HEADER_KEY))
        .build();
}
 
Example #30
Source File: DefaultWebFilterChain.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) {
	DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null);
	ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size());
	while (iterator.hasPrevious()) {
		chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain);
	}
	return chain;
}