2013-01-25 79 views
6

我尝试基于官方教程Sparklr2/Tonr2实现我自己的示例。一切看起来都很好,但是当我在Tonr2落实,春季安全过滤器从web.xml删除我有例外:弹簧安全oauth 2简单示例

没有重定向URI已经建立当前请求

我无法理解我应该使用哪个网址。这里是我的代码,为客户实现:

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

<!--define an oauth 2 resource for sparklr --> 
<oauth:resource id="provider" type="authorization_code" client-id="client" client-secret="secret" 
    access-token-uri="http://localhost:8080/provider/oauth/token" user-authorization-uri="http://localhost:8080/provider/oauth/authorize" scope="read,write" /> 

<beans:bean id="clientController" class="com.aouth.client.ClientController"> 
    <beans:property name="trustedClientRestTemplate"> 
     <oauth:rest-template resource="provider" /> 
    </beans:property> 
</beans:bean> 

而对于供应商:

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

<authentication-manager id="clientAuthenticationManager" xmlns="http://www.springframework.org/schema/security"> 
    <authentication-provider user-service-ref="clientDetailsUserService" /> 
</authentication-manager> 

<bean id="clientDetailsUserService" class="org.springframework.security.oauth2.provider.client.ClientDetailsUserDetailsService"> 
    <constructor-arg ref="clientDetails" /> 
</bean> 

<!-- The OAuth2 protected resources are separated out into their own block so we can deal with authorization and error handling 
    separately. This isn't mandatory, but it makes it easier to control the behaviour. --> 
<http pattern="/secured" create-session="never" access-decision-manager-ref="accessDecisionManager" xmlns="http://www.springframework.org/schema/security"> 
    <anonymous enabled="false" /> 
    <intercept-url pattern="/secured" access="ROLE_USER,SCOPE_READ" /> 
    <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> 
    <http-basic /> 
</http> 

<bean id="accessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased" xmlns="http://www.springframework.org/schema/beans"> 
    <constructor-arg> 
     <list> 
      <bean class="org.springframework.security.oauth2.provider.vote.ScopeVoter" /> 
      <bean class="org.springframework.security.access.vote.RoleVoter" /> 
      <bean class="org.springframework.security.access.vote.AuthenticatedVoter" /> 
     </list> 
    </constructor-arg> 
</bean> 

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

<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="clientDetails"/> 
</bean> 

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

<http auto-config="true" xmlns="http://www.springframework.org/schema/security"> 
    <intercept-url pattern="/test" access="ROLE_USER" /> 
    <intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY" /> 
</http> 

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

<oauth:authorization-server client-details-service-ref="clientDetails" token-services-ref="tokenServices" > 
    <oauth:authorization-code /> 
    <oauth:implicit /> 
    <oauth:refresh-token /> 
    <oauth:client-credentials /> 
    <oauth:password /> 
</oauth:authorization-server> 

<oauth:client-details-service id="clientDetails"> 
    <oauth:client client-id="client" resource-ids="resource" authorized-grant-types="authorization_code, implicit" 
     authorities="ROLE_CLIENT" scope="read,write" secret="secret" /> 
</oauth:client-details-service> 

我只是希望我的客户不带弹簧的安全工作。而当我需要我的受保护资源时,我只想在供应商一方登录。

+0

你能完成它吗?你有代码来证明你做了什么吗?我正在尝试学习相同的东西 – daydreamer

回答

10

您在此处粘贴是春天的为OAuth的提供商 XML和保护资源,而你的情况在同一个Web应用程序运行你第二XML。 (当然,如果你愿意,你可以将它们分开)。

客户端(第一个粘贴XML)是一个不同的故事。如果我正确地理解了你,你希望你的客户在没有Spring的帮助下运行(成为一个普通的web应用程序,而不是spring-security-oauth-client webapp)。

您必须了解oAuth的工作方式:客户端尝试访问受保护的资源;如果它没有访问令牌,它将被重定向到oAuth提供程序(显示登录页面并提供令牌)。 By the standard,对访问令牌的请求必须包含一个“redirect-uri”参数,所以在成功登录后,oAuth提供者知道将客户端重定向到哪里。 oAuth客户端为你做,如果你从web.xml中删除“oauth客户端”,你现在必须自己实现这个。

感谢您的回答。但我仍然不明白弹簧如何影响我的oAuth客户端。并且我可以用于客户端 spring-oauth(spring-mvc)没有弹簧安全性吗?

当你写这条线在你的XML:

< oauth:client id="oauth2ClientFilter" /> 

这意味着你使用弹簧安全的OAuth,这是一种专门用于OAuth的包,建立在春天的安全性。如果您深入了解,它会在处理oAuth内容的链中放置一个特殊的过滤器(OAuth2ClientContextFilter),这些过滤器与客户端相关。其中之一是发送请求的所有参数(“redirect-uri”就是其中之一)。

如果你决定不使用弹簧安全的OAuth,以及 - 你必须自己实现这个逻辑...

希望帮助!

+0

感谢您的回答。但我仍然不明白弹簧安全如何影响我的oAuth客户端。并且我可以使用客户端spring-oauth(spring-mvc)而没有弹簧安全性吗? – chaldaean

+2

谢谢OhadR,你的回答的确有助于我理解。 – chaldaean

+0

@chaldaean您可以在纠正错误后演示完整的security.xml文件 – PRASANTHMV