首页 文章

Spring安全客户端详细信息将作为DaoAuthenticationProvider中的用户详细信息

提问于
浏览
2

Spring Boot,安全OAuth2实现,默认令牌 endpoints (/ oauht / token)工作正常 . 但是,当我向/ oauth / http / token的新 endpoints 发送请求时,由于以下原因,它会抛出Bad Credentials:

FilterChainProxy 触发大约12个过滤器,其中一个是 BasicAuthenticationFilter . 它使用 UserDetailsServiceDaoAuthenticationProvider 类来获取用户数据 . 对于客户端身份验证,这应该是 ClientDetailsService 但是,由于某种原因,这始终是 UserDetailsService ,因为此客户端凭据转到 UserRepository 并失败 . 此类确实正确初始化,因为默认 /oauth/token 工作正常 .

我尝试在 BasicAuthenticationFilter 中注入现有的身份验证管理器,并将其添加为 ResourceServerConfigurerAdapter 中的过滤器,但这没有任何区别 . 它确实将身份验证管理器提供程序从 AnonymousAuthenticationProvider 更改为 DaoAuthenticationProvider ,但 UserDetailsService 仍然是 UserDetails .

请求 /oauth/http/token ,这不起作用 . 代码与 postAccessToken()org.springframework.security.oauth2.provider.endpoint.TokenEndpoint 几乎相同

在上面的屏幕截图中,我们可以看到userDetailsService是UserDetailsServiceImpl,并且由于此请求头中存在此客户端详细信息,因为 Basic dGVzdDpwYXNzd29yZA== 将转到用户存储库并在用户表中检查而不是转到客户端存储库并在客户端表中进行检查 .

请求 /oauth/token ,这是有效的

1 回答

  • 2

    FilterChainProxy 维护的不是单个过滤器链,而是 SecurityFilterChain -s.Each安全过滤器链的列表包含请求匹配器和过滤器列表 . 因此,您将在这些不同的链中拥有多个 BasicAuthenticationFilter 实例 .

    将触发哪个 filter chain 取决于传入的请求和请求匹配的决定 .

    /oauth/token 触发由spring oauth创建的链,并在末尾使用 ClientDetailsService . /oauth/http/token 触发由您的Web安全配置创建的另一个链,并使用用户详细信息服务 .

    所以......这就是原因 . 要查看在启动时如何创建链,您可以启用安全性调试,例如,在 application.yml

    logging:
      level:
        org.springframework.security: DEBUG
    

    然后你会看到oauth安全链的创建:

    Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/oauth/token'], Ant [pattern='/oauth/token_key'], Ant [pattern='/oauth/check_token']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@4ce4e309, org.springframework.security.web.context.SecurityContextPersistenceFilter@16331720, org.springframework.security.web.header.HeaderWriterFilter@60ef29a5, org.springframework.security.web.authentication.logout.LogoutFilter@4c9638cc, org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter@9eefda5, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@16d090e9, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@484a9950, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1c4fefe8, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@12082780, org.springframework.security.web.session.SessionManagementFilter@20a49b7b, org.springframework.security.web.access.ExceptionTranslationFilter@24313d10, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@47ce08d2]
    

    请注意请求匹配 .

    UPDATE: 如果要将 endpoints 'remap'到您自己的 endpoints ,您可以重新配置它 .

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints.pathMapping("/oauth/token", "/oauth/http/token");
    }
    

相关问题