Spring security oauth2客户端

时间:2012-11-01 17:37:06

标签: java spring-security

我已经设置了具有spring安全性的OAuth2服务器。我想编写客户端应用程序以使用具有spring安全性的此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>

所以我只想/产品网址应该访问oauth2服务器。其余的URL映射应该没有这个。 并且用户应该对客户端匿名(无需在客户端显示登录信息)。

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

1 个答案:

答案 0 :(得分:12)

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提供程序授权页面之前,不会在客户端请求授权。

相关问题