|
| 1 | +package com.baeldung.functional; |
| 2 | + |
| 3 | +import org.springframework.boot.SpringApplication; |
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 5 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| 6 | +import org.springframework.context.annotation.Bean; |
| 7 | +import org.springframework.context.annotation.ComponentScan; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Profile; |
| 10 | +import org.springframework.core.io.ClassPathResource; |
| 11 | +import org.springframework.http.server.reactive.HttpHandler; |
| 12 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 13 | +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
| 14 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 15 | +import org.springframework.web.reactive.function.server.RouterFunction; |
| 16 | +import org.springframework.web.reactive.function.server.RouterFunctions; |
| 17 | +import org.springframework.web.reactive.function.server.ServerResponse; |
| 18 | +import org.springframework.web.server.adapter.WebHttpHandlerBuilder; |
| 19 | +import reactor.core.publisher.Flux; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 24 | + |
| 25 | +import static org.springframework.web.reactive.function.BodyInserters.fromObject; |
| 26 | +import static org.springframework.web.reactive.function.server.RequestPredicates.*; |
| 27 | +import static org.springframework.web.reactive.function.server.RouterFunctions.route; |
| 28 | +import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; |
| 29 | +import static org.springframework.web.reactive.function.server.ServerResponse.ok; |
| 30 | + |
| 31 | +@SpringBootApplication |
| 32 | +@ComponentScan(basePackages = { "com.baeldung.functional" }) |
| 33 | +public class FunctionalSpringBootApplication { |
| 34 | + |
| 35 | + private static final Actor BRAD_PITT = new Actor("Brad", "Pitt"); |
| 36 | + private static final Actor TOM_HANKS = new Actor("Tom", "Hanks"); |
| 37 | + private static final List<Actor> actors = new CopyOnWriteArrayList<>(Arrays.asList(BRAD_PITT, TOM_HANKS)); |
| 38 | + |
| 39 | + private RouterFunction<ServerResponse> routingFunction() { |
| 40 | + FormHandler formHandler = new FormHandler(); |
| 41 | + |
| 42 | + RouterFunction<ServerResponse> restfulRouter = route(GET("/"), serverRequest -> ok().body(Flux.fromIterable(actors), Actor.class)).andRoute(POST("/"), serverRequest -> serverRequest |
| 43 | + .bodyToMono(Actor.class) |
| 44 | + .doOnNext(actors::add) |
| 45 | + .then(ok().build())); |
| 46 | + |
| 47 | + return route(GET("/test"), serverRequest -> ok().body(fromObject("helloworld"))) |
| 48 | + .andRoute(POST("/login"), formHandler::handleLogin) |
| 49 | + .andRoute(POST("/upload"), formHandler::handleUpload) |
| 50 | + .and(RouterFunctions.resources("/files/**", new ClassPathResource("files/"))) |
| 51 | + .andNest(path("/actor"), restfulRouter) |
| 52 | + .filter((request, next) -> { |
| 53 | + System.out.println("Before handler invocation: " + request.path()); |
| 54 | + return next.handle(request); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public ServletRegistrationBean servletRegistrationBean() throws Exception { |
| 60 | + HttpHandler httpHandler = WebHttpHandlerBuilder |
| 61 | + .webHandler(toHttpHandler(routingFunction())) |
| 62 | + .prependFilter(new IndexRewriteFilter()) |
| 63 | + .build(); |
| 64 | + ServletRegistrationBean registrationBean = new ServletRegistrationBean<>(new RootServlet(httpHandler), "/"); |
| 65 | + registrationBean.setLoadOnStartup(1); |
| 66 | + registrationBean.setAsyncSupported(true); |
| 67 | + return registrationBean; |
| 68 | + } |
| 69 | + |
| 70 | + @Configuration |
| 71 | + @EnableWebSecurity |
| 72 | + @Profile("!https") |
| 73 | + static class SecurityConfig extends WebSecurityConfigurerAdapter { |
| 74 | + @Override |
| 75 | + protected void configure(final HttpSecurity http) throws Exception { |
| 76 | + http |
| 77 | + .authorizeRequests() |
| 78 | + .anyRequest() |
| 79 | + .permitAll(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static void main(String[] args) { |
| 84 | + SpringApplication.run(FunctionalSpringBootApplication.class, args); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments