![](/img/trans.png)
[英]Can't access OAuth2AuthenticationToken when using Google OAuth2 in Spring Boot
[英]OAuth2 with Google and Spring Boot - I can't log out
一段时间以来,我一直在尝试使用 Google 和 Spring Boot 成功登录 Oauth2。 这仅部分起作用。 部分原因 - 因为我无法管理注销,或者当我按下注销按钮时,我看到一个空白的白色浏览器页面,上面有我的 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");
}
我的 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
该 fetchSignoutSite 仅清空 JsessionId 并从 Spring 安全上下文注销。 所以你仍然需要将你 go 的部分添加到谷歌并从那里退出,我没有实施经验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.