Spring 启动 OAuth2 单点注销(注销)

Spring Boot OAuth2 Single Sign Off (Logout)

我正在考虑为我的应用程序使用 OAuth2。我尝试实现的架构如下:

到目前为止,我已经设法在 3 个基本应用程序(1 个身份验证服务器、1 个资源服务器和 1 个客户端)之间实现了这种交互。我无法正常工作的是注销功能。我读过 "notoriously tricky problem" that Dave Syer describes in his tutorial, but in this case I really need the user to re-login after loging out. I have tried giving few seconds to the access token and the refresh token, but instead of being prompted to login again when the expiration arrives, I'm getting a NPE on the client app. I have also tried the solutions proposed in this post 从令牌存储中删除令牌,但它不起作用。单点关闭对我来说是此实现的理想行为。如何使用 Spring Boot Oauth2 实现此目的。如果由于某种原因不可能,我可以使用哪些替代方案来使用 Spring Boot 实施集中式安全性?

提前致谢。

经过大量测试后,我意识到这可以通过重定向到 AuthServer 并像这样以编程方式注销来解决:

  • 在客户端应用程序 (WebSecurityConfigurerAdapter) 中:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .logout()
                .logoutSuccessUrl("http://your-auth-server/exit");
    }
    
  • 在授权服务器中:

    @Controller
    public class LogoutController {
    
        @RequestMapping("/exit")
        public void exit(HttpServletRequest request, HttpServletResponse response) {
            // token can be revoked here if needed
            new SecurityContextLogoutHandler().logout(request, null, null);
            try {
                //sending back to client app
                response.sendRedirect(request.getHeader("referer"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

我已经发布了 sample app on github,其中包含此实现的完整示例。