Spring security 如何在spring安全无状态(jwt令牌)中注销用户?

Spring security 如何在spring安全无状态(jwt令牌)中注销用户?,spring-security,jwt,token,logout,stateless,Spring Security,Jwt,Token,Logout,Stateless,我通过spring创建了一个微服务系统,在我的项目中,我有3个前端和多个微服务,这些前端使用(并存储到cookie中)jwt令牌从我的微服务中获取资源 我的用户登录场景: 当用户想要从前端登录时,我重定向到我的实时项目(另一个前端)用户登录到我的实时前端,重定向到前端,并在查询参数中传递令牌,因此前端可以使用此令牌获取资源。 当用户从前端2登录时重定向到live,当用户在之前登录时在存储的live token的cookie中未获取用户名和密码时,使用exist token重定向到前端2。 这是我

我通过spring创建了一个微服务系统,在我的项目中,我有3个前端和多个微服务,这些前端使用(并存储到cookie中)jwt令牌从我的微服务中获取资源

我的用户登录场景: 当用户想要从前端登录时,我重定向到我的实时项目(另一个前端)用户登录到我的实时前端,重定向到前端,并在查询参数中传递令牌,因此前端可以使用此令牌获取资源。 当用户从前端2登录时重定向到live,当用户在之前登录时在存储的live token的cookie中未获取用户名和密码时,使用exist token重定向到前端2。 这是我的sso:)

但我不能注销用户,因为当用户注销(从cookie中删除令牌)时,如何理解用户注销的其他前端

我想我必须创建会话来获取用户登录的状态。或者我需要集中的令牌状态检查

如何创建此会话

我的spring安全代码是spring的默认安全代码

最后我为我的英语道歉

我的实时项目代码(UAA)

其他资源

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends 
ResourceServerConfigurerAdapter {

@Override
public void configure(final HttpSecurity http) throws Exception {
    // @formatter:off
    http.csrf().disable() // csrf
            .antMatcher("/**").authorizeRequests() // /**
            .antMatchers("/actuator/**").permitAll() // Actuator
            .antMatchers("/ws/**").permitAll() // websocket
            .anyRequest().authenticated();
    // @formatter:on
}

@Override
public void configure(ResourceServerSecurityConfigurer config) {
    config.tokenServices(tokenServices());
}

@Bean
public TokenStore tokenStore() {
    return new JwtTokenStore(accessTokenConverter());
}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    Resource resource = new ClassPathResource("public-key.txt");
    String publicKey = null;
    try {
        publicKey = IOUtils.toString(resource.getInputStream());
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    converter.setVerifierKey(publicKey);
    return converter;
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
    DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
    defaultTokenServices.setTokenStore(tokenStore());
    return defaultTokenServices;
}
}

我们使用Redis缓存/服务器来存储令牌。有关Redis的更多信息,请访问

登录后,我们将用户上下文和此令牌作为密钥存储,并将用户信息作为后端的值存储。您还可以设置此令牌的过期时间

注销后,您需要从后端删除此令牌和相应的值

现在,每个经过身份验证的请求都会有这个令牌,它将验证它,并检查Redis中是否存在这个令牌

令牌将在2个案例中丢失

  • 用户已注销
  • 令牌已过期
  • 在每次有效请求之后,我们都会更新到期时间。因此,只有在系统空闲特定时间的情况下,令牌才会过期

    这也是有益的分布式后端环境

    @Configuration
    @EnableResourceServer
    public class OAuth2ResourceServerConfig extends 
    ResourceServerConfigurerAdapter {
    
    @Override
    public void configure(final HttpSecurity http) throws Exception {
        // @formatter:off
        http.csrf().disable() // csrf
                .antMatcher("/**").authorizeRequests() // /**
                .antMatchers("/actuator/**").permitAll() // Actuator
                .antMatchers("/ws/**").permitAll() // websocket
                .anyRequest().authenticated();
        // @formatter:on
    }
    
    @Override
    public void configure(ResourceServerSecurityConfigurer config) {
        config.tokenServices(tokenServices());
    }
    
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }
    
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource("public-key.txt");
        String publicKey = null;
        try {
            publicKey = IOUtils.toString(resource.getInputStream());
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        converter.setVerifierKey(publicKey);
        return converter;
    }
    
    @Bean
    @Primary
    public DefaultTokenServices tokenServices() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        return defaultTokenServices;
    }
    }