我已添加到我的 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("/");
}
我也尝试过这种方法:
@覆盖 protected 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 客户端支持注销使用社交 OAuth 2 登录提供程序之一进行身份验证的用户?
最佳答案
我目前已在 chrome 浏览器上登录 google,并且可以查看我的 gmail 等,因此我与 google 之间有一个事件 session 。 如果我要访问您的 spring 应用程序并使用 google 登录,您的 spring 应用程序会将我重定向到 google 身份验证服务器,该服务器检测到我已经登录到 google,因此它知道我是谁,因此它只需要询问我同意您的应用程序请求的范围,并且如果我同意向您的应用程序颁发访问 token 。 现在,如果我想注销你的应用程序,Spring Security 将使你的应用程序上的 session 无效,但它无法控制我用谷歌打开的 session ,事实上我不想也从谷歌注销。 因此,如果您想再次看到第三方的登录屏幕,您需要转到他们的页面并注销。
关于spring-security - Spring Security 5 OAuth 2 社交注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64176103/