【问题标题】:Spring Security Oauth 2 custom token end point urlSpring Security Oauth2 自定义令牌端点 url
【发布时间】:2015-08-26 18:14:18
【问题描述】:

您好,我必须在我的项目中集成 spring security oauth2。所以我添加了配置相关的部分并且它工作正常。但问题是令牌的第一个请求转到 "/oauth/token" ,我想将其更改为 "api/v1/token" 。 我搜索并找到了一些解决方案,例如在oauth:authorization-server 中添加token-endpoint-url,还添加了将覆盖ClientCredentialsTokenEndpointFilter 的自定义过滤器类并将url 传递给构造函数。但这些都不起作用。 我收到以下错误请求“api/v1/token”-

在 SecurityContext 中找不到 Authentication 对象

我的配置主要是基于xml的。 Spring-security.xml 文件-

<http pattern="/api/v1/token" create-session="stateless"
      authentication-manager-ref="authenticationManager"
      xmlns="http://www.springframework.org/schema/security" > 
    <intercept-url pattern="/api/v1/token" access="IS_AUTHENTICATED_FULLY" />
    <anonymous enabled="false" />
    <http-basic entry-point-ref="clientAuthenticationEntryPoint"/>
    <custom-filter ref="customClientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" /> 
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<http pattern="/test/**" create-session="never"
      entry-point-ref="oauthAuthenticationEntryPoint"       
      xmlns="http://www.springframework.org/schema/security">
    <anonymous enabled="false" />
    <intercept-url pattern="/test/**" access="ROLE_USER" />
    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
</http>
<beans:bean id="oauthAuthenticationEntryPoint"
            class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
</beans:bean>
 <beans:bean id="oauthAccessDeniedHandler"
    class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler">
</beans:bean>
<beans:bean id="clientAuthenticationEntryPoint"
            class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <beans:property name="realmName" value="springsec/client" />
    <beans:property name="typeName" value="Basic" />
</beans:bean>


 <beans:bean id="customClientCredentialsTokenEndpointFilter"
            class="com.walletdoc.oauth.web.security.CustomClientCredentialsTokenEndpointFilter">
     <beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>

<authentication-manager alias="authenticationManager"
                        xmlns="http://www.springframework.org/schema/security">
    <authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager>
<beans:bean id="clientDetails" class="com.walletdoc.oauth.web.security.SpringSecurityClientService">
    <beans:property name="id" value="mysupplycompany" />
    <beans:property name="secretKey" value="mycompanykey" />
    <beans:property name="authorities" value="ROLE_CLIENT" />
</beans:bean>
<beans:bean id="clientDetailsUserService"
            class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
    <beans:constructor-arg ref="clientDetails"/>
</beans:bean>



<authentication-manager id="userAuthenticationManager" 
                        xmlns="http://www.springframework.org/schema/security">
    <authentication-provider  ref="customUserAuthenticationProvider">
    </authentication-provider>
</authentication-manager>

<beans:bean id="customUserAuthenticationProvider"
            class="com.walletdoc.oauth.web.security.ClientAuthenticationProvider">
</beans:bean>

<oauth:authorization-server
    client-details-service-ref="clientDetails" token-services-ref="tokenServices" token-endpoint-url="/api/v1/token">
    <oauth:authorization-code />
    <oauth:implicit/>
    <oauth:refresh-token/>
    <oauth:client-credentials />
    <oauth:password authentication-manager-ref="userAuthenticationManager" />
</oauth:authorization-server>

<oauth:resource-server id="resourceServerFilter"
                       resource-id="springsec" token-services-ref="tokenServices" />

<beans:bean id="tokenStore"
            class="org.springframework.security.oauth2.provider.token.InMemoryTokenStore" />

<beans:bean id="tokenServices"
            class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
    <beans:property name="tokenStore" ref="tokenStore" />
    <beans:property name="supportRefreshToken" value="true" />
    <beans:property name="accessTokenValiditySeconds" value="3600"></beans:property>
    <beans:property name="clientDetailsService" ref="clientDetails" />
</beans:bean>

从过去 2 天开始,我一直在尝试解决此问题,因此我们将不胜感激。

【问题讨论】:

    标签: java spring-security-oauth2


    【解决方案1】:

    终于搞定了。这个问题中缺少的一件事是 &lt;beans:property name="filterProcessesUrl" value="/api/v1/token" /&gt;ClientCredentialsTokenEndpointFilter Bean 定义中。

    custom-filter 标记中有ClientCredentialsTokenEndpointFilter Bean 的引用,并且在该Bean 中定义了一个属性ID filterProcessesUrl,并且该值作为所需的url 给出,在我的例子中它是/api/v1/token。这样就解决了问题。

    所以基本上为了改变token-endpoint-url,我们需要在4个地方改变-

    1. authorization-server 定义中添加token-endpoint-url

    2. 更改&lt;http pattern="changed URL"

    3. 更改&lt;intercept-url pattern="changed URL"

    4. 并且,在ClientCredentialsTokenEndpointFilter 中添加带有value="changed url"filterProcessesUrl 属性。

    【讨论】:

      【解决方案2】:

      路径“/oauth/token”不是一种事实上的标准吗?

      不幸的是,我没有关于 XML 配置的确切答案,但也许这个 Java 配置可以给你提示。

      当您扩展 AuthorizationServerConfigurerAdapter 时,以下方法可让您访问 Builder 以访问授权服务器配置:

      @Override
      public void configure(AuthorizationServerEndpointsConfigurer oauthServer) throws Exception {
          oauthServer
      
              // Here you can override the default endpoints mappings
              .pathMapping("/oauth/authorize", "/api/v1/authorize")
              .pathMapping("/oauth/token", "/api/v1/token")
      
              // .. rest of the authorization server customization
              .authenticationManager(authenticationManager)
              .tokenStore(tokenStore);
      }
      

      这有点奇怪,因为您通过 Map 覆盖映射,您必须知道要覆盖的每条路径,并且 AuthorizationServerEndpointsConfigurer 类中实际上没有任何提示或文档。

      最终它起作用了,就像授权服务器启动日志时一样:

      ...
      .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/api/v1/authorize],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto ...
      .s.o.p.e.FrameworkEndpointHandlerMapping : Mapped "{[/api/v1/token],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto ...
      

      ...

      我猜在 XML 中它应该转换为 &lt;oauth:authorization-server&gt; 中的某些元素,例如:

      <oauth:authorization-server>
        <!-- ... -->
        <pathMappings>
          <!-- key/value here -->
        </pathMappings>
      </oauth:authorization-server>
      

      编辑:在检查了XML schema 之后,您可能一开始就做对了,因为token-endpoint-url 元素应该已经根据 cmets 进行了数学运算:

              <xs:attribute name="token-endpoint-url" type="xs:string">
                  <xs:annotation>
                      <xs:documentation>
                          The URL at which a request for an access token
                          will be serviced.
                          Default value: "/oauth/token"
                      </xs:documentation>
                  </xs:annotation>
              </xs:attribute>
      

      也许您应该在spring-security-oauth issue tracker 中提出问题?

      【讨论】:

      • 太糟糕了,您应该将问题提交到 Spring 的问题跟踪器上。同时,也许您可​​以使用 Java @Configuration 而不是 XML,因为它可以工作。祝你好运:)
      • “路径“/oauth/token”难道不是一种事实上的标准吗?”根据spec,端点是不是 b> 以/oauth 为前缀。它们只是/authorize/token。因此,在 Spring 配置中更改这些可能不是一个坏主意。
      猜你喜欢
      • 2020-07-20
      • 2015-01-16
      • 2014-04-08
      • 2015-02-11
      • 2013-09-25
      • 2015-01-01
      • 2020-04-19
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多