Spring 没有外部客户端的 ClientId 和 ClientSecret 的 OAuth2 令牌

Spring OAuth2 token without ClientId and ClientSecret for External Clients

我们使用接受 Username/Password/ClientId/Secret 的 Spring 生成 OAuth 令牌,效果非常好。对于外部客户端,我们只需要输入用户名和密码并生成 OAuth 令牌。

<security:http pattern="/oauth/token" create-session="stateless"
        authentication-manager-ref="clientAuthenticationManager"
        xmlns="http://www.springframework.org/schema/security">
        <security:intercept-url pattern="/oauth/token" access="IS_AUTHENTICATED_FULLY" />
        <security:anonymous enabled="false" />
    <security:http-basic entry-point-ref="clientAuthenticationEntryPoint" />
        <!-- include this only if you need to authenticate clients via request parameters -->
        <security:custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" />
    <security:access-denied-handler ref="oauthAccessDeniedHandler" />
  </security:http>

下面是我们需要添加的新代码,但它要求在浏览器中输入用户名和密码。

<security:http pattern="/**external**/oauth/token" create-session="stateless"
        authentication-manager-ref="clientAuthenticationManager"
        xmlns="http://www.springframework.org/schema/security">
        <security:intercept-url pattern="/external/oauth/token" access="IS_AUTHENTICATED_FULLY" />
        <security:anonymous enabled="false" />
    <security:http-basic entry-point-ref="clientAuthenticationEntryPoint" />
        <security:custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" />
    <security:access-denied-handler ref="oauthAccessDeniedHandler" />
  </security:http>

请指导我们是否可以在没有clientId的情况下生成OAuth并在内部传递clientId来生成OAuth。

没有 clientId,您永远无法生成 OAuth 令牌! Oauth2 有 3 种创建令牌的方式,隐式、代码和 user/pass。最后一个应该避免,因为它意味着 Oauth 客户端将获得对用户凭据的访问权限,而 OAuth 正是为了防止这种情况而构建的。仅使用用户的凭据授予隐式令牌(通常仅涉及浏览器)。在 Code-mode 中,OAuth 客户端收到一个代码(不应在浏览器中),然后将其交换为令牌。令牌交换的代码要求 Oauth 客户端使用它的 clientId 和密码进行身份验证,这通常使用基本身份验证来完成。

我认为您需要的是 https://www.rfc-editor.org/rfc/rfc6749#section-1.3.3

中解释的资源所有者密码授予类型

资源所有者密码授予类型只能用于受信任的客户端。因此,如果您所说的外部客户端是受信任的客户端(如同一家公司开发的本机移动应用程序。例如 Facebook 移动应用程序),则可以使用。

流程在https://www.rfc-editor.org/rfc/rfc6749#section-4.3.1

中解释

资源所有者授权类型最重要的方面是客户端不应存储用户名和密码。