Warm tip: This article is reproduced from serverfault.com, please click

security-使用oauth2和Google / Azure身份验证服务器在Java应用程序中注销的问题

(security - Problem to logout in java app with oauth2 and Google / Azure auth server)

发布于 2020-09-22 16:33:46

我遇到问题,需要一些建议。情况如下,我有一个非常简单的具有弹簧安全性的Java模块和一个受保护的HTML页面。该模块唯一要做的就是针对Google / Azure进行身份验证,如果凭据正确无误,它将带我进入HTML页面。实现OAuth2和Spring安全5。身份验证工作没有问题,我获得了idTokens,访问令牌和刷新令牌(对于Azure)。尝试注销时出现问题(HTML调用“ app / logogut” URL的按钮)。我看到有关本地安全上下文的内容已删除,没有cookie,浏览器的存储中也没有任何内容,但是,外部服务器(Google / Azure)上的会话仍然处于活动状态,当我刷新页面时,仍处于登录状态。如果我使用Gmail打开一个新标签,我直接登录而不登录,如果我从Gmail注销,则现在刷新第一个标签时,它会询问用户并再次通过。我的查询是我是否缺少某些登出帐户以通过Spring安全性完全关闭会话?

另外,尝试对每个服务器的URL进行GET登出,这似乎可行,但是用这种方式-CORS问题:(-。

我的安全配置

在此处输入图片说明

文字内

 @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/", "/login**").permitAll()
            .anyRequest().authenticated()
            .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/app/logout"))
            .addLogoutHandler(new HeaderWriterLogoutHandler(
                    new ClearSiteDataHeaderWriter(
                            ClearSiteDataHeaderWriter.Directive.CACHE,
                            ClearSiteDataHeaderWriter.Directive.COOKIES,
                            ClearSiteDataHeaderWriter.Directive.STORAGE)))
            .and()
            .oauth2Login()
            .and().csrf().disable()
    ;
}

我的控制器

 @Autowired
@PreAuthorize("hasRole('ROLE_USER')")
@GetMapping("/restricted")
public String restricted() throws IOException, URISyntaxException {
    URL resource = getClass().getClassLoader().getResource("static\\Protected.html");
    if (resource == null) {
        throw new IllegalArgumentException("file not found!");
    } else {

        File html = new File(resource.toURI());
        List<String> strings = Files.readLines(html, StandardCharsets.UTF_8);
        StringBuffer sb = new StringBuffer();
        for (String s : strings) {
            sb.append(s);
        }
        return sb.toString();


    }

}

HTML页面

在此处输入图片说明

任何帮助,将不胜感激!

提前致谢

Questioner
MarceloM
Viewed
0
MarceloM 2020-12-03 13:10:27

终于,在所有这些时间之后,我找到了解决问题的方法。遵循Spring文档(https://docs.spring.io/spring-security/site/docs/5.2.x/reference/html/oauth2.html#oauth2login-advanced-oidc-logout),我发现了一些所谓的Single Sign Out如果在application.properties中指定了issuer-uri,则可以实施。此uri返回一个json,其中除其他数据外,是end_session_endpoint URL。

我的application.properties

现在,你可以像这样配置OidcClientInitiatedLogoutSuccessHandler

SecurityConfig.java

这允许从我的应用程序注销时,调用先前的处理程序,并使授权服务器中的会话无效。

非常感谢你的帮助