如何更改spring security oauth2默认令牌端点?

时间:2014-03-06 11:21:36

标签: spring-security-oauth2

我们有基于spring security oauth2的应用程序。一切都很好。但我无法将默认令牌端点从“/ oauth / token”更改为“/ external / oauth / token”。

我的spring-servlet.xml

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

<oauth:authorization-server client-details-service-ref="clientDetails" 
        token-services-ref="tokenServices" 
        user-approval-handler-ref="userApprovalHandler" token-endpoint-url="/external/oauth/token">
        <oauth:authorization-code />
        <oauth:implicit />
        <oauth:refresh-token />
        <oauth:client-credentials />
        <oauth:password />
</oauth:authorization-server>

但是当我访问此端点时的结果是

{
    error: "unauthorized"
    error_description: "An Authentication object was not found in the SecurityContext"
}
我错过了什么吗?请建议。

4 个答案:

答案 0 :(得分:31)

使用版本2.0.5.RELEASE或更高版本的spring-security-oauth2

在基于java的配置的一行中,测试并且工作正常,它以某种方式覆盖了TokenEndpoint类的RequestMapping值。

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {      

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints
                .pathMapping("/oauth/token", "<your custom endpoint>")
        }
}

答案 1 :(得分:6)

几天来一直在努力,但现在让它在最新的Spring Oauth2 1.0.5.RELEASE上工作。我并不是100%确定我的解决方案是最合适的(特别是第4步),但它有效并且我能够继续前进。

就我而言,我想从网址中删除/oauth前缀,最后只有/token/authorize。我的解决方案主要是xml配置,有两个黑客可以覆盖端点请求映射。

1 - 在应用上下文xml中,将authorization-endpoint-urltoken-endpoint-url attribs添加到您的<oauth:authorization-server>元素。

矿:

<oauth:authorization-server client-details-service-ref="clientDetailsService" token-services-ref="tokenServices" user-approval-handler-ref="userApprovalHandler" authorization-endpoint-url="/authorize" token-endpoint-url="/token">

2 - 在应用程序上下文xml中,相应地调整安全端点。应该有两个,分别管理令牌和身份验证URL的安全性。需要更新<http><intercept-url>代码上的模式支柱。

矿:

<http pattern="/token/**" create-session="stateless" authentication-manager-ref="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/token/**" access="IS_AUTHENTICATED_FULLY" />

...

<http pattern="/authorize/**" access-denied-page="/login.jsp?authorization_error=true" disable-url-rewriting="true" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/authorize/**" access="IS_AUTHENTICATED_FULLY" />

3 - (如果您选择使用可选的clientCreds过滤器。)在应用程序上下文xml中,您应该已经将clientCredentialsTokenEndpointFilter bean作为<custom-filter> within your element. So, within the filter's bean, add a filterProcessesUrl连接到了属性。

矿:

<bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <property name="authenticationManager" ref="clientAuthenticationManager" />
    <property name="filterProcessesUrl" value="/token" />
</bean>

4 - 最后一步是覆盖实际内部端点控制器的请求映射URL。 spring oauth2 lib有两个类:AuthorizationEndpointTokenEndpoint。每个使用@RequestMapping类型的注释来绑定url(就像我们为项目的app控制器所做的那样)。对我来说,试图以任何方式覆盖请求映射的值,而不是(遗憾地)重新创建我的src文件夹中的spring类包,将AuthorizationEndpoint和TokenEndpoint类逐字复制到所述文件夹中是一种令人头发的努力。并编辑内联@RequestMapping注释值。

无论如何,这就是诀窍。希望听到更优雅的方式来覆盖端点控制器请求映射值。

感谢。

最终,正在运行的应用环境:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:sec="http://www.springframework.org/schema/security" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"

  xsi:schemaLocation="
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
    http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
  "
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>

  <!-- Declare OAuth2 services white-list. (This is the top of the config.) -->
  <oauth:authorization-server client-details-service-ref="clientDetailsService" token-services-ref="tokenServices" user-approval-handler-ref="userApprovalHandler" authorization-endpoint-url="/authorize" token-endpoint-url="/token">
    <oauth:authorization-code />
    <oauth:implicit />
    <oauth:refresh-token />
    <oauth:client-credentials />
    <!-- <oauth:password /> -->
  </oauth:authorization-server>
  <bean id="userApprovalHandler" class="org.springframework.security.oauth2.provider.approval.TokenServicesUserApprovalHandler">
    <!-- This bean bridges client auth service and user tokens... kind of an out of place requirement. -->
    <property name="tokenServices" ref="tokenServices" />
  </bean>

  <!-- This starts the far back-end config for client token management. -->
  <sec:authentication-manager id="clientAuthenticationManager">
    <sec:authentication-provider user-service-ref="clientDetailsUserService" />
  </sec:authentication-manager>
  <bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService">
    <constructor-arg ref="clientDetailsService" />
  </bean>
  <bean id="clientDetailsService" class="com.mycompany.oauth.spring.security.oauth2.IntegratedOauth2ClientDetailsService">
    <!-- This bean is what wires OAuth2 into the persistence stack for client details stored in the oauth_client table. -->
  </bean>


  <!-- OAuth is layered on to spring security which is centered around users which requires a user auth manager.  -->
  <authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security">
    <authentication-provider ref="daoAuthenticationProvider" />
  </authentication-manager>
  <bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService" />
  </bean>

  <bean id="tokenServices" class="org.springframework.security.oauth2.provider.token.DefaultTokenServices">
    <property name="tokenStore" ref="tokenStore" />
    <property name="supportRefreshToken" value="true" />
    <property name="clientDetailsService" ref="clientDetailsService" />
  </bean>
  <bean id="tokenStore" class="com.mycompany.oauth.spring.security.oauth2.IntegratedOAuth2TokenStore">
    <!-- This bean is what wires OAuth2 tokens into my company's application stack. -->
    <constructor-arg ref="dataSource" />
  </bean>

  <!-- **************************************************************************************** -->
  <!-- Finally, sew OAuth into spring security with some http tags... -->
  <!-- **************************************************************************************** -->

  <!-- The OAuth2 endpoint for direct token requests (i.e. for client_credentials flow). -->
  <http pattern="/token/**" create-session="stateless" authentication-manager-ref="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/token/**" access="IS_AUTHENTICATED_FULLY" />
    <anonymous enabled="false" />
    <http-basic entry-point-ref="clientAuthenticationEntryPoint" />
    <custom-filter ref="clientCredentialsTokenEndpointFilter" before="BASIC_AUTH_FILTER" />
    <access-denied-handler ref="oauthAccessDeniedHandler" />
  </http>
  <bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <property name="authenticationManager" ref="clientAuthenticationManager" />
    <property name="filterProcessesUrl" value="/token" />
  </bean>
  <bean id="clientAuthenticationEntryPoint" class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint">
    <property name="realmName" value="myrealm" />
  </bean>
  <bean id="oauthAccessDeniedHandler" class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />

  <!-- The OAuth2 endpoint for user-approved authorization (i.e. for "authorization" flow involving user login/approve). -->
  <http pattern="/authorize/**" access-denied-page="/login.jsp?authorization_error=true" disable-url-rewriting="true" xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/authorize/**" access="IS_AUTHENTICATED_FULLY" />
    <form-login authentication-failure-url="/login.jsp?authentication_error=true" default-target-url="http://www.mycompany.com/" login-page="/login.jsp" login-processing-url="/login.do" />
    <http-basic />
    <anonymous />
  </http>

</beans>

答案 2 :(得分:4)

要自定义令牌结束点URL,请执行以下步骤。

1)编写自己的类,扩展ClientCredentialsTokenEndpointFilter类&amp;使用&#34; / external / oauth / token&#34;调用ClientCredentialsTokenEndpointFilter类构造函数值。

super("/external/oauth/token");

2)在安全配置中插入新的自定义过滤器。

替换

<custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" />

<custom-filter ref="your customize filter" after="BASIC_AUTH_FILTER" />

3)为新映射创建自己的类(/ external / oauth / token)&amp;延伸至tokenendpoint。

4)改变http&amp; intercept-url元素的模式属性值为&#34; / external / oauth / token&#34;

答案 3 :(得分:4)

你正在变得比现在更难,它实际上非常简单! (请注意,我使用&#34; oauth2:&#34;而不是&#34; oauth:&#34;作为XML标记)

  1. 转到security-context.xml

  2. 查找&#34; oauth2:authorization-server&#34;在上面的文件中。

    <oauth2:authorization-server
                    client-details-service-ref="someService"
                    request-validator-ref="someScopeRequestValidator"
                    token-services-ref="someTokenServices" >
    
  3. 只需添加token-endpoint-url="/oauth/whatever_you_like"

    即可
    <oauth2:authorization-server
                    client-details-service-ref="someService"
                    request-validator-ref="someScopeRequestValidator"
                    token-services-ref="someTokenServices" 
    **token-endpoint-url="/oauth/whatever_you_like"** >