Customizing login page for OAUTH2 login with Spring Webflux

Viewed 571

I am looking to customize my oauth2 login page. I am using Spring cloud Gateway so this is Spring Security Webflux. This should be fairly straightforward but I cannot figure out a way to do it with Webflux security. Stuff I have tried :

  • I have tried using loginPage() as suggested in How Can I Customize Login Page for Oauth2 in Spring Webflux? but as the O.P. posted there, this does not compile in the webflux world.
  • I have tried putting in a controller at /login and have it direct to my custom page but the controller is not hit. My SecurityConfig :
    @Bean
    public SecurityWebFilterChain configure(ServerHttpSecurity http) throws Exception {
        return http
                .csrf().disable().authorizeExchange()
                .pathMatchers(HttpMethod.GET,"/oauth2login",
                        "*/*.js", "/*.json", "/*.ico", "/login").permitAll()
                .pathMatchers("/api/","/api/**").authenticated()
                .and().oauth2ResourceServer(ServerHttpSecurity.OAuth2ResourceServerSpec::jwt)
                .authorizeExchange().anyExchange().authenticated()
               .and().securityContextRepository(new WebSessionServerSecurityContextRepository())
                   .oauth2Login()
                .and().build();
    }

What am I doing wrong here?

1 Answers

It's a bit confusing because there are multiple different ways to customize the login page depending on what type of authentication you are configuring in servlet-based applications.

In reactive applications, it has been centralized in one place (exceptionHandling() in the DSL), so you can simply set the authentication entry point as in the following example:

.exceptionHandling(exceptions -> exceptions
        .authenticationEntryPoint(new RedirectServerAuthenticationEntryPoint("/login"))
)

This disables the default login page, and you just need to provide your own which should match the configured URL.

Related