2013-01-25 4 views
6

나는 공식적인 튜토리얼 Sparklr2/Tonr2을 기반으로 내 자신의 예제를 구현하려고한다. 모든 것이 좋아 보인다하지만 난 내 Tonr2 구현, 봄 보안 필터에 web.xml에서 제거 할 때 나는 예외가 있습니다스프링 보안 oauth 2 간단한 예제

아니오 URI가 현재 요청

나는 이해할 수 없다 설립되었습니다 리디렉션 어떤 URL을 사용해야합니까?

<!--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 및 귀하의 경우 같은 웹 애플리케이션에서 실행 보호 자원, 당신 2 XML. (물론 원하는 경우 분리 할 수 ​​있습니다).

클라이언트 (첫 번째 붙여 넣은 XML)는 다른 이야기입니다. 만약 당신이 올바르게 이해한다면, 당신은 클라이언트가 Spring의 도움없이 실행되기를 원할 것입니다. (스프링 - 보안 - oauth-client webapp가 아닌 일반적인 webapp가 될 것입니다.)

oAuth 작동 방식을 이해해야합니다. 클라이언트가 보호 된 리소스에 접근하려고합니다. 액세스 토큰이없는 경우 oAuth-provider (로그인 페이지를 표시하고 토큰을 제공함)로 리디렉션됩니다. By the standard, 액세스 토큰에 대한 요청은 "redirect-uri"매개 변수를 포함해야하며, 따라서 성공적인 로그인 후에 oAuth- 공급자는 클라이언트를 리디렉션 할 위치를 알고 있습니다. oAuth 클라이언트가 자동으로이 작업을 수행하며, web.xml에서 "oauth client"를 삭제하면 이제 직접 구현해야합니다.

답장을 보내 주셔서 감사합니다. 하지만 스프링 보안이 내 oAuth 클라이언트에 미치는 영향을 이해할 수 없습니다. 그리고 스프링 보안없이 클라이언트 측 spring-oauth (spring-mvc)에 사용할 수 있습니까? 당신이 당신의 XML에서이 줄을 쓸 때

는 :

< oauth:client id="oauth2ClientFilter" /> 

당신이 OAuth를 전용 패키지, 봄 보안을 기반으로 봄 - 보안의 OAuth를 사용하는 것을 의미한다. 파고들 경우, 클라이언트와 관련된 oAuth 항목을 처리하는 체인에 특수 필터 (OAuth2ClientContextFilter)를 넣습니다. 그 중 하나가 모든 매개 변수 ("redirect-uri"가 그 중 하나임)와 함께 요청을 보냅니다.

당신이 봄 보안의 OAuth를 사용하지 않으려면, 잘 - 데 도움이 당신이 희망 ... 혼자서이 논리를 구현하는

을 한 것이다!

+0

당신의 대답에 감사드립니다. 하지만 저는 아직도 oAuth 클라이언트에 스프링 보안이 어떻게 유입되는지 이해하지 못합니다. 그리고 스프링 보안없이 클라이언트 측 스프링 - 오트 (spring-mvc)를 사용할 수 있습니까? – chaldaean

+2

감사합니다 OhadR, 당신의 대답은 참으로 나를 이해하는 데 도움이. – chaldaean

+0

@chaldaean은 수정 한 후 전체 security.xml 파일을 시연 해 주시기 바랍니다. – PRASANTHMV