2016-07-11 8 views
0

セキュリティ保護された接続のために春のセキュリティoauth2を使用するプロジェクトがあります。下は私の春の構成ファイルです。認証オブジェクトがoauth2のSecurityContextに見つかりませんでした

春-のsecurity.xml:

 <?xml version="1.0" encoding="UTF-8" ?> 
    <beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
     xmlns:sec="http://www.springframework.org/schema/security" xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-2.0.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd 
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd "> 

    <!-- @author Nagesh.Chauhan([email protected]) --> 
     <!-- This is default url to get a token from OAuth --> 
     <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 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> 

     <!-- This is where we tells spring security what URL should be protected 
      and what roles have access to them --> 
     <http pattern="/protected/**" create-session="never" 
      entry-point-ref="oauthAuthenticationEntryPoint" 
      access-decision-manager-ref="accessDecisionManager" 
      xmlns="http://www.springframework.org/schema/security"> 
      <anonymous enabled="false" /> 
      <intercept-url pattern="/protected/**" access="ROLE_APP" /> 
      <custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" /> 
      <access-denied-handler ref="oauthAccessDeniedHandler" /> 
     </http> 


     <bean id="oauthAuthenticationEntryPoint" 
      class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> 
      <property name="realmName" value="test" /> 
     </bean> 

     <bean id="clientAuthenticationEntryPoint" 
      class="org.springframework.security.oauth2.provider.error.OAuth2AuthenticationEntryPoint"> 
      <property name="realmName" value="test/client" /> 
      <property name="typeName" value="Basic" /> 
     </bean> 

     <bean id="oauthAccessDeniedHandler" 
      class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> 

     <bean id="clientCredentialsTokenEndpointFilter" 
      class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"> 
      <property name="authenticationManager" ref="clientAuthenticationManager" /> 
     </bean> 

     <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> 

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


     <!-- This is simple authentication manager, with a hardcoded user/password 
      combination. We can replace this with a user defined service to get few users 
      credentials from DB --> 
     <authentication-manager alias="authenticationManager" 
      xmlns="http://www.springframework.org/schema/security"> 
      <authentication-provider> 
       <user-service> 
        <user name="user1" password="user1" authorities="ROLE_APP" /> 
       </user-service> 
      </authentication-provider> 
     </authentication-manager> 

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


     <!-- This defined token store, we have used inmemory tokenstore for now 
      but this can be changed to a user defined one --> 
     <bean id="tokenStore" 
      class="org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore" /> 

     <!-- This is where we defined token based configurations, token validity 
      and other things --> 
     <bean id="tokenServices" 
      class="org.springframework.security.oauth2.provider.token.DefaultTokenServices"> 
      <property name="tokenStore" ref="tokenStore" /> 
      <property name="supportRefreshToken" value="true" /> 
      <property name="accessTokenValiditySeconds" value="3600" /> 
      <property name="refreshTokenValiditySeconds" value="5270400"></property> 
      <property name="clientDetailsService" ref="clientDetails" /> 
     </bean> 

     <bean id="oAuth2RequestFactory" 
       class="org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory"> 
      <constructor-arg ref="clientDetails"/> 
     </bean> 
     <bean id="userApprovalHandler" 
      class="org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler"> 
      <property name="tokenStore" ref="tokenStore" /> 
      <property name="requestFactory" ref="oAuth2RequestFactory"/> 
     </bean> 
     <oauth:authorization-server 
      client-details-service-ref="clientDetails" token-services-ref="tokenServices" 
      user-approval-handler-ref="userApprovalHandler" authorization-endpoint-url="/protected" token-endpoint-url="/oauth/token"> 
      <oauth:authorization-code /> 
      <oauth:implicit /> 
      <oauth:refresh-token /> 
      <oauth:client-credentials /> 
      <oauth:password /> 
     </oauth:authorization-server> 

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

     <oauth:client-details-service id="clientDetails"> 
      <!-- client --> 
         <oauth:client client-id="client1" 
       authorized-grant-types="authorization_code,client_credentials" 
       authorities="ROLE_APP" scope="read,write,trust" secret="secret" />   

     <oauth:client client-id="client1" 
       authorized-grant-types="password,authorization_code,refresh_token,implicit" 
       secret="client1" authorities="ROLE_APP" /> 

     </oauth:client-details-service> 

     <sec:global-method-security 
      pre-post-annotations="enabled" proxy-target-class="true"> 
      <!--you could also wire in the expression handler up at the layer of the 
       http filters. See https://jira.springsource.org/browse/SEC-1452 --> 
      <sec:expression-handler ref="oauthExpressionHandler" /> 
     </sec:global-method-security> 

     <oauth:expression-handler id="oauthExpressionHandler" /> 
     <oauth:web-expression-handler id="oauthWebExpressionHandler" /> 
    </beans> 

私はアクセスを取得しています以下の要求を使用してOAuthアクセストークンを要求し、トークンとして以下のリフレッシュ。

リクエストが

curl -X POST http://localhost:8080/SaveItMoneyOauth/oauth/token -H “Accept: application/json” -d "grant_type=password&client_id=client1&client_secret=client1&username=user1&password=user1&scope=read,write,trust" 

応答は次のとおりです。IIは、要求の下に使用して保護されたリソースのために要求されたとき

{"value":"4796a04a-2266-4184-a1be-e4248cea7ba8","expiration":"Jul 11, 2016 12:15:34 PM","tokenType":"bearer","refreshToken":{"expiration":"Sep 10, 2016 11:15:34 AM","value":"87509989-0ea9-4372-87aa-22290ae0c98e"},"scope":["read,write,trust"],"additionalInformation":{}}curl: (6) Could not resolve host: application 

は、私はエラーとして「認証オブジェクトがSecurityContextがで見つかりませんでした」取得しています。

要求は次のとおりです。

curl -H "access_token=4796a04a-2266-4184-a1be-e4248cea7ba8" "http://localhost:8080/SaveItMoneyOauth/protected/users/api" 

私はのOAuth2ライブラリとして "2.0.7.RELEASE" を使用しています。 このエラーを解決するにはどうすればいいですか。

答えて

1

あなたのCURLコマンドが間違っているため、認証ヘッダーにアクセストークンを提供する必要があります。試してみてください:

curl -H "Authorization: Bearer 4796a04a-2266-4184-a1be-e4248cea7ba8" "http://localhost:8080/SaveItMoneyOauth/protected/users/api" 
関連する問題