简体   繁体   English

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

[英]OAuth2 with Google and Spring Boot - I can't log out

I've been trying to get a successful Oauth2 login with Google and Spring Boot for a while now.一段时间以来,我一直在尝试使用 Google 和 Spring Boot 成功登录 Oauth2。 This only works partially.这仅部分起作用。 Why partly - because I can't manage the logout or when I pressed the logout button I see an empty, white browser page with my URL (http://localhost:8181/ben/"). After a refresh of the page I get error from google, but if I open a new tab, enter my url, I'm still logged in to google, because I can see my user, which I'm outputting to my react application.部分原因 - 因为我无法管理注销,或者当我按下注销按钮时,我看到一个空白的白色浏览器页面,上面有我的 URL (http://localhost:8181/ben/")。刷新页面后,我从谷歌得到错误,但如果我打开一个新标签,输入我的 url,我仍然登录到谷歌,因为我可以看到我的用户,我正在输出到我的反应应用程序。

@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");
}

My application.yml file looks like this:我的 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

在此处输入图像描述

That fetchSignoutSite only emptying the JsessionId and logging out from Spring Security context.该 fetchSignoutSite 仅清空 JsessionId 并从 Spring 安全上下文注销。 So you would still need to add part where you go to google and sign out from there which I have no experience on implementation.所以你仍然需要将你 go 的部分添加到谷歌并从那里退出,我没有实施经验。

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

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