【问题标题】:Spring security oauth2 clientSpring Security oauth2 客户端
【发布时间】:2012-10-22 08:42:57
【问题描述】:

我已经设置了一个带有 Spring Security 的 OAuth2 服务器。我想编写客户端应用程序以在不保护任何资源的情况下使用这个具有弹簧安全性的 oauth 服务器。意味着我只想使用 spring security 3.1 从客户端运行 oauth2。我已经编写了以下配置,但它在重定向到 oauth2 服务器授权页面之前要求提供凭据。但我想在从客户端询问任何凭据之前将用户重定向到 oauth2 服务器授权页面。我正在使用以下配置

<http auto-config='true' xmlns="http://www.springframework.org/schema/security">
    <intercept-url pattern="/product/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
    <custom-filter ref="oauth2ClientFilter" after="EXCEPTION_TRANSLATION_FILTER" />
</http>

<authentication-manager xmlns="http://www.springframework.org/schema/security">
    <authentication-provider>
        <user-service>
            <user name="jimi" password="jimi" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

<!--apply the oauth client context -->
<oauth:client id="oauth2ClientFilter" />


<oauth:resource id="fooClient" type="authorization_code"
    client-id="foo" client-secret="secret" access-token-uri="${accessTokenUri}"
    user-authorization-uri="${userAuthorizationUri}" scope="read" />


 <bean id="dService" class="com.abc.service.DServiceImpl">
    <property name="dURL" value="${dURL}"></property>
    <property name="dRestTemplate">
        <oauth:rest-template resource="fooClient" />
    </property>

 </bean>

所以我只想 /product url 应该访问 oauth2 服务器。没有这个,其余的 URL 映射应该可以工作。 并且用户应该是客户端的匿名用户(无需在客户端显示登录)。

但是当我运行我的应用程序“http://localhost/client-sample/product/1”时,它会显示“http://localhost/client-sample/spring_security_login”。但我希望用户应该重定向到 oaut2 服务器页面。

【问题讨论】:

    标签: java spring-security


    【解决方案1】:

    Spring 安全性阻止匿名用户获取访问令牌。但是,如果您仍然希望在您的应用程序中使用此功能,那么您将必须扩展 org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails 类并覆盖 isClientOnly() 方法。

    import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
    
    public class ExtendedBaseOAuth2ProtectedResourceDetails extends
        AuthorizationCodeResourceDetails {
    
    public boolean isClientOnly() {
        return true;
    }
    }
    

    默认情况下,此方法返回 false。所以你必须重写这个方法才能返回true。 然后在你的 root-context.xml 文件中你必须像这样定义 oaut2 资源。

    <bean id="fooClient" class="com.abc.service.ExtendedBaseOAuth2ProtectedResourceDetails">
      <property name="clientId" value="foo"></property>
      <property name="clientSecret" value="secret"></property>
      <property name="accessTokenUri" value="${accessTokenUri}"></property>
      <property name="userAuthorizationUri" value="${userAuthorizationUri}"></property>
      <property name="scope" value="#{{'read','write'}}">   </property>
    </bean>
    
    <bean id="dService" class="com.abc.service.DServiceImpl">
      <property name="dURL" value="${dURL}"></property>
      <property name="dRestTemplate">
          <oauth:rest-template resource="fooClient" />
      </property>
    </bean>
    

    在将用户重定向到 oauth2 提供者授权页面之前,这不会在客户端请求授权。

    【讨论】:

      猜你喜欢
      • 2013-06-20
      • 2019-04-06
      • 2017-09-17
      • 2020-02-11
      • 2021-03-07
      • 2022-11-08
      • 1970-01-01
      • 2019-07-20
      • 2015-11-05
      相关资源
      最近更新 更多