4

我有一个使用 Spring Boot + OAuth2 + Login through Google 的 Auth Server 实现。以及用于我的后端数据服务的资源服务器。我使用了 JDBC 令牌存储。一切都很好。但是我很难理解注销的实现。目前,每当用户单击注销时,我只是从浏览器本地存储中删除令牌,但会话在 Auth 服务器中保持活动状态,因此我不需要再次登录。我想要的是每当使用注销时点击我想使会话无效并强制他再次登录。

有什么好方法可以做到这一点吗?我的 Spring Boot Auth 服务器配置中目前没有任何注销配置。

谢谢

4

1 回答 1

0

尝试注册一个 LogoutSuccessHandler 来做到这一点。类似于以下内容:

@Configuration
@EnableWebSecurity
@EnableResourceServer
public class SecurityConfig extends ResourceServerConfigurerAdapter {

    @Bean
    public DefaultTokenServices tokenServices() {
        return new DefaultTokenServices();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.resourceId("myResourceId");
        resources.tokenServices(tokenServices());
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
       // configure http security here...

        http.logout().logoutSuccessHandler(new SimpleUrlLogoutSuccessHandler() {
                      @Override
                      public void onLogoutSuccess(HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  Authentication authentication) {
                          OAuth2AccessToken token = tokenServices().getAccessToken((OAuth2Authentication) authentication);
                          tokenServices().revokeToken(token.getValue());
                      }
                  });

    }
}
于 2015-06-23T21:17:37.460 回答