【问题标题】:How to add a header into `AuthenticationKeyGenerator`?如何在 `AuthenticationKeyGenerator` 中添加标头?
【发布时间】:2019-02-16 10:48:18
【问题描述】:

背景: 多个设备可以使用相同的客户端凭据,因此它们共享令牌,当 2 个或更多设备尝试刷新令牌时,这可能会导致竞争条件。设备标识可以在标头中找到,因此自定义 AuthenticationKeyGenerator 实现可以使用设备 ID 为每个设备生成一个令牌。设备id可以作为附加参数存储在OAuth2Request.getExtensions()中。

问题: 正如我所提到的,设备 ID 不是来自 GET/POST 参数,因此它不存在于 OAuth2Request.getRequestParameters() 中,但可以在标头中找到。但是OAuth2Request是在DefaultOAuth2RequestFactory中创建的(OAuth2RequestFactory的实现),它无法访问原始http请求从headers中复制信息并添加到OAuth2Request.getExtensions()

捕获标头并将其存储在 OAuth2Request.getExtensions() 中以供 AuthenticationKeyGenerator 以后用作密钥的一部分的最佳方法是什么?

【问题讨论】:

    标签: spring spring-security oauth-2.0 spring-security-oauth2 spring-oauth2


    【解决方案1】:

    我有一个类似的 AuthenticationKeyGenerator 实现,其中 设备/用户代理 用于使用 Redis 作为令牌存储为 auth_to_access 生成密钥。

    下面是我如何在自定义 AuthenticationKeyGenerator 实现中捕获用户代理的代码 sn-p

    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
    
    String userAgent = request.getHeader("User-Agent");
    

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 2015-08-30
      • 2020-03-09
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      相关资源
      最近更新 更多