Spring Security 5 OAuth 2社交注销社交、Spring、Security、OAuth

2023-09-03 10:43:28 作者:别拿发型挑战我

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

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

spring security oauth2

我已使用新的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("/");
}

我也尝试过这种方式:

@覆盖 受保护的空配置(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浏览器上登录到谷歌,并且可以查看我的Gmail等,因此我与谷歌的会话处于活动状态。 如果我访问您的Spring应用程序,并使用Google登录,您的Spring应用程序会将我重定向到Google身份验证服务器,该服务器会检测到我已经登录到Google,因此它只需要求我同意您的应用程序请求的范围,以及如果我同意向您的应用程序颁发访问令牌。 现在,如果我想注销你的应用程序,Spring Security将使你应用程序上的会话无效,但它无法控制我与谷歌打开的会话,事实上,我不想也从谷歌注销。 因此,如果您想再次访问第三方的登录屏幕,您需要转到他们的页面并注销。