Spring Boot OAuth2单点注销(注销)

时间:2017-03-28 13:48:57

标签: java spring spring-boot spring-security spring-security-oauth2

我正在考虑将OAuth2用于我的应用程序。我想要实现的架构如下:

  • 我将拥有自己的(仅限于此)授权服务器
  • 某些资源应用使用授权服务器验证对其资源的访问
  • 某些客户端应用程序(网络,移动设备)会将用户重定向到授权服务器进行身份验证,并且会成功使用资源应用程序上的api。

到目前为止,我已设法在3个基本应用程序(1个auth服务器,1个资源服务器和1个客户端)之间实现此交互。我没有工作的是注销功能。我已经阅读了Dave Syer在他的教程中描述的"notoriously tricky problem",但在这种情况下,我确实需要用户在注销后重新登录。我已经尝试给访问令牌和刷新令牌提供几秒钟,但是当到期时,我没有被提示再次登录,而是在客户端应用程序上获得NPE。我还尝试了此post中提出的解决方案,以从令牌存储中删除令牌,但它不起作用。单点注销对我来说是这种实现的理想行为。如何使用Spring Boot Oauth2实现此目的。如果由于某种原因不可能,我可以使用哪些替代方法来使用Spring Boot实现集中式安全性?

提前致谢。

1 个答案:

答案 0 :(得分:16)

经过大量测试后,我意识到只需重定向到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,其中包含此实施的完整示例。

相关问题