11

在注销控制器中,我尝试编写很多代码组合。现在我有这个:

final Authentication auth = SecurityContextHolder.getContext().getAuthentication();

if (auth != null) {
    new SecurityContextLogoutHandler().logout(request, response, auth);
}

SecurityContextHolder.getContext().setAuthentication(null);
auth.setAuthenticated(false);

但是在提供代码执行令牌后仍然有效。

我错了什么?如何最终撤销令牌?

4

5 回答 5

9

您要查找的类是 DefaultServicesmethod revokeToken(String tokenValue)

这里是撤销令牌的控制器的示例,这里DefaultServices是带有bean的 oauth2 配置。

于 2014-05-14T22:45:21.260 回答
9

如果您需要撤销当前用户以外的其他用户的令牌(例如,管理员想要禁用用户帐户),您可以使用以下命令:

Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientIdAndUserName(
                                                           "my_oauth_client_id", 
                                                           user.getUsername());
for (OAuth2AccessToken token : tokens) {
  consumerTokenServices.revokeToken(token.getValue());
}

tokenStore成为一个和org.springframework.security.oauth2.provider.token.TokenStore成为consumerTokenServices一个org.springframework.security.oauth2.provider.token.ConsumerTokenServices

于 2017-03-21T17:00:20.490 回答
0

该线程有点旧,但对于 JWTToken 用户来说,这不起作用,因为没有存储令牌。所以另一种选择是使用过滤器。1 为管理员创建一种方法来锁定/解锁数据库上的用户。2 使用过滤器,如果方法需要身份验证,检查用户是否处于活动状态

例子:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if(authentication != null
            &&  authentication.getName() != null
            && !authentication.getName().equalsIgnoreCase("anonymousUser")) {
        UserModel user = userService.getUser(authentication.getName());
        if(user != null && !user.isActivated())
            throw new SecurityException("SECURITY_USER_DISABLED");
    }
    chain.doFilter(request, response);
}

在客户端,只需拦截此错误并断开用户连接,希望这对某人有所帮助。

于 2018-01-13T12:46:33.993 回答
0

使用DefaultTokenServices为当前授权用户撤销令牌的简单示例:

  1. 默认令牌存储需要 Bean

    @Bean 
    public DefaultTokenServices tokenServices() {
         DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
         defaultTokenServices.setTokenStore(tokenStore());
         defaultTokenServices.setSupportRefreshToken(true);
         return defaultTokenServices;
    }
    
  2. 然后你可以编写自己的控制器

    @RestController
    @RequestMapping("/user")
    public class UserApi {
    
    @Autowired
    private DefaultTokenServices tokenServices;
    
    @Autowired
    private TokenStore tokenStore;
    
    @DeleteMapping("/logout")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void revokeToken() {
        final OAuth2Authentication auth = (OAuth2Authentication) SecurityContextHolder
                .getContext().getAuthentication();
        final String token = tokenStore.getAccessToken(auth).getValue();
        tokenServices.revokeToken(token);
    }
    }
    
于 2018-06-16T20:24:02.140 回答
-2

自动装配 DefaultTokenServices 然后使用以下代码:

String authHeader = request.getHeader("Authorization");
String tokenValue = authHeader.replace("bearer", "").trim();
tokenService.revokeToken(tokenValue);
tokenService.setAccessTokenValiditySeconds(1);
tokenService.setRefreshTokenValiditySeconds(1);

只需尝试撤销访问令牌的代码即可。

于 2016-11-24T12:13:07.230 回答