OAuth2 与 Google 和 Spring Boot - 我无法注销

问题描述 投票:0回答:2

我一直在尝试使用 Google 和 Spring Boot 成功登录 Oauth2 一段时间。这只能部分起作用。部分原因是 - 因为我无法管理注销,或者当我按下注销按钮时,我看到一个空的白色浏览器页面,其中包含我的 URL (http://localhost:8181/ben/")。刷新页面后,我从谷歌收到错误,但如果我打开一个新选项卡,输入我的网址,我仍然登录到谷歌,因为我可以看到我的用户,我将其输出到我的反应应用程序。

@SpringBootApplication
@EnableOAuth2Sso
@RestController
@CrossOrigin
public class SocialApplication extends WebSecurityConfigurerAdapter {

public static void main(String[] args) {
    SpringApplication.run(SocialApplication.class, args);
}

@RequestMapping("/user")
public Principal user(Principal principal) {
    return principal;
}

@RequestMapping("/logout")
public String fetchSignoutSite(HttpServletRequest request, HttpServletResponse response) {
    Cookie rememberMeCookie = new Cookie("JSESSIONID", "");
    rememberMeCookie.setMaxAge(0);
    response.addCookie(rememberMeCookie);

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        new SecurityContextLogoutHandler().logout(request, response, auth);
    }

    auth.getPrincipal();
    return "redirect:/ben/login";
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.antMatcher("/**").authorizeRequests().antMatchers("/ben/*").permitAll().anyRequest().authenticated().and()
            .logout().logoutSuccessUrl("http://localhost:8181/ben/login").invalidateHttpSession(true)
            .clearAuthentication(true).deleteCookies("JSESSIONID");
}

我的 application.yml 文件如下所示:

# Spring Boot configuration
spring:
  profiles:
active: google
# Spring Security configuration
security:
 oauth2:
   client:
     clientId: 415772070383-3sapp4flauo6iqsq8eag7knpcii50v9k.apps.googleusercontent.com
  clientSecret: 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
  accessTokenUri: https://www.googleapis.com/oauth2/v4/token
  userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth
  clientAuthenticationScheme: form
  scope:
    - email
    - profile
resource:
  userInfoUri: https://www.googleapis.com/oauth2/v3/userinfo
  preferTokenInfo: true
# Server configuration
server:
port: 8181
 servlet:
  context-path: /ben

spring spring-boot oauth-2.0 google-oauth spring-security-oauth2
2个回答
0
投票

fetchSignoutSite 仅清空 JsessionId 并从 Spring Security 上下文中注销。所以你仍然需要添加你去谷歌并从那里注销的部分,我没有实施经验。


0
投票

有几种方法,但恐怕它们都建议依赖浏览器操作。原始文章在这里:https://groups.google.com/g/google-api-javascript-client/c/PCs8xXV4wxk?pli=1

TL;博士

  1. 公开链接 https://accounts.google.com/logout 作为用户界面中某处注销操作的一部分。
  2. 添加隐藏的 iframe,其中 src 指向文档中的同一 URL 几秒钟,然后将其删除,希望当时没有互联网连接中断。

我选择了第一个,显示了带有按钮链接的注销屏幕。

© www.soinside.com 2019 - 2024. All rights reserved.