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

Sri*_*nth 29 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>
Run Code Online (Sandbox Code Playgroud)

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

{
    error: "unauthorized"
    error_description: "An Authentication object was not found in the SecurityContext"
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?请建议.

Emi*_*and 36

使用版本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>")
        }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*lla 6

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

在我的情况下,我想/oauth从网址中删除前缀,最后只是/token/authorize.我的解决方案主要是xml配置,有两个hacks来覆盖端点请求映射.

1 - 在应用程序上下文xml中,添加authorization-endpoint-urltoken-endpoint-url归因于您的<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">
Run Code Online (Sandbox Code Playgroud)

2 - 在应用程序上下文xml中,相应地调整安全端点.应该有两个,分别管理令牌和身份验证URL的安全性.需要更新模式prop <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" />
Run Code Online (Sandbox Code Playgroud)

3 - (如果您选择使用可选的clientCreds过滤器.)在app context xml中,您应该已经将clientCredentialsTokenEndpointFilterbean连接为<custom-filter> within yourelement. So, within the filter's bean, add afilterProcessesUrl`属性.

矿:

<bean id="clientCredentialsTokenEndpointFilter" class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
    <property name="authenticationManager" ref="clientAuthenticationManager" />
    <property name="filterProcessesUrl" value="/token" />
</bean>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

1) 编写您自己的类来扩展 ClientCredentialsTokenEndpointFilter 类并使用“/external/oauth/token”值调用 ClientCredentialsTokenEndpointFilter 类构造函数。

super("/external/oauth/token");
Run Code Online (Sandbox Code Playgroud)

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

代替

<custom-filter ref="clientCredentialsTokenEndpointFilter" after="BASIC_AUTH_FILTER" />
Run Code Online (Sandbox Code Playgroud)

<custom-filter ref="your customize filter" after="BASIC_AUTH_FILTER" />
Run Code Online (Sandbox Code Playgroud)

3) 为新映射 (/external/oauth/token) 创建您自己的类并扩展 tokenendpoint。

4) 将http &intercept-url 元素的pattern 属性值改为"/external/oauth/token"