Search code examples
spring-bootspring-securitythymeleafspring-webfluxspring-cloud-gateway

How to set up login page in Spring Boot 2 with WebFlux?


I have created Spring Boot 2 WebFlux application (based on Spring Cloud Gateway project) and now try to configure custom login page instead of standard:

@Bean
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
    return http.httpBasic().and()
            .authorizeExchange()
            .anyExchange().authenticated()
            .and()
            .formLogin().loginPage("/login")
            .and()
            .csrf().disable()
            .build();
}

I tried to use Thymeleaf to render this page by means of login html page creation and controller setup:

@Controller
public class LoginController {

    @RequestMapping(value = "/login")
    public Mono<String> getLoginPage() {
        return Mono.just("/templates/login.html");
    }
}

But it don't working. Can anybody explain how to do this and should I using Thymeleaf at all? Maybe this have already implemented and is on GitHub?


Solution

  • Try

    @Controller
    public class LoginController {
    
        @GetMapping("/login")
        public String getLoginPage() {
            // assuming that Thymeleaf is present
            // and a valid src/main/resources/templates/login.html template 
            return "login";
        }
    }