简体   繁体   English

Spring Webflux如何自定义Oauth2登录页面?

[英]How Can I Customize Login Page for Oauth2 in Spring Webflux?

I just want to override default oauth2 login url ( /login ).我只想覆盖默认的 oauth2 登录 url ( /login )。 How can I do that?我怎样才能做到这一点? The config I have tried without success:我尝试过但没有成功的配置:

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        http
                .authorizeExchange().pathMatchers(permittedUrls).permitAll()
                .anyExchange().authenticated()
                .and()
                .oauth2Login(Customizer.withDefaults()).formLogin().loginPage("/oauth2_login")
                .authenticationSuccessHandler(this::onAuthenticationSuccess)
                .and()
                .csrf().disable();
        return http.build();

I was hoping it will redirect to /oauth2_login url but it didn't work.我希望它会重定向到/oauth2_login url 但它没有用。 It still redirect to /login.它仍然重定向到/login。 But this time it returns 404 instead of showing default login page.但是这次它返回 404 而不是显示默认登录页面。

The code above is customizing the login page for formLogin which is typically username/password based log in from a form.上面的代码是为formLogin定制登录页面,这通常是基于用户名/密码的表单登录。 It's much easier to see what configuration you are impacting using the new lambda style approach, so I have updated the entire configuration to use it.使用新的 lambda 样式方法可以更容易地查看您正在影响的配置,因此我更新了整个配置以使用它。 If you want to customize the login page for oauth2Login, then you should change the login page on it.如果你想自定义oauth2Login的登录页面,那么你应该更改登录页面就可以了。 For example:例如:

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
    http
            .authorizeExchange(exchanges -> exchanges
                .pathMatchers(permittedUrls).permitAll()
                .anyExchange().authenticated()
            )
            .oauth2Login(oauth2 -> oauth2
                // you now must render a log in page for the URL /login
                .loginPage("/login") 
            );
            // remove formLogin that was for a username/password based log in
            // if you are doing oauth2 login I'm guessing you allow users to work within a browser, so you should not disable csrf
    return http.build();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM