Spring Boot + OAuth2 + Google登录 - 如何实施注销

时间:2015-04-19 17:54:07

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

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

有什么好办法吗?我目前在Spring Boot Auth服务器配置中没有任何注销配置。

由于

1 个答案:

答案 0 :(得分: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());
                      }
                  });

    }
}