Spring Security 5 OAuth 2社交注销

问题描述

我已将其添加到我的Spring Boot MVC Web应用程序社交登录功能中。它允许用户使用GitHub,Facebook或Google帐户登录到我的应用程序。但是我正在努力使/ logout功能正常工作。即使调用/ logout并加载了logoutSuccessUrl,如果用户再次单击登录链接,也不会再次要求用户提供用户名或密码。看来用户仍在通过身份验证。

你们如何使用新的Spring Security 5 OAuth 2客户端支持实现/ logout?

我已经使用了新的OAuth 2客户端支持

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

spring.security.oauth2.client.registration.facebook.client-id =  
spring.security.oauth2.client.registration.facebook.client-secret = 

这是我的HTTPSecurity配置的样子:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/").permitAll()
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .and()
        .logout().logoutSuccessUrl("/");
}

我也尝试过这种方式:

@Override 受保护的void configure(HttpSecurity http)抛出异常{ http

        .authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers(HttpMethod.POST,"/logout").permitAll()
        .anyRequest().authenticated()
        .and()
        .oauth2Login()
        .and()
        .logout()
        .invalidateHttpSession(true)
        .clearauthentication(true)
        .deleteCookies("JSESSIONID")
        .logoutSuccessUrl("/").permitAll()
        .and()
            .csrf()
            .csrftokenRepository(CookieCsrftokenRepository.withHttpOnlyFalse());

}

你们如何使用新的Spring Security OAuth 2客户端支持注销使用其中一个Social OAuth 2登录提供程序进行身份验证的用户

解决方法

我当前在我的Chrome浏览器中登录google,并且可以查看我的gmail等,因此我正在与google进行有效会话。 如果我要访问您的spring应用程序,并使用google登录,您的spring应用程序会将我重定向到googles身份验证服务器,该服务器会检测到我已经登录google,从而知道我是谁,因此只需要问我即可同意您的应用程序所要求的范围,如果我同意向您的应用程序颁发访问令牌,则表示同意。 现在,如果我要注销您的应用程序,Spring Security将使您的应用程序上的会话无效,但它无法控制我与google打开的会话,实际上,我也不想退出google。 因此,如果要再次显示第三方的登录屏幕,则需要转到他们的页面并注销。