2012-10-05 6 views
5

私はSpring 3.1とRESTEasyプロジェクト用にOAuth 2.0を実装したいと思います。このプロジェクトは、JSONベースのRESTサービスです。私は、Spring Security 3.1とspring-security-oauth2 version 1.0.0.RC2(最新のはずです)を使用しています。これまでのところ私は春のセキュリティ設定をデフォルト設定で行っています。 OAuth 2.0の基本設定(デフォルト)もあります。oauth2プロバイダーエンドポイントのハンドラーエラーのアダプターがありません

以前はRESTサービスを使用していましたが、完璧に動作します。春のセキュリティもうまくいくようです。 RESTサービスへのリンクを開くと、ログインページにリダイレクトされます。ログイン後、私は期待される結果を与えるREST呼び出しを行うことができます。

私はHetはOAuthのをテストするために、localhost:8080/tools-service/oauth/tokenまたはlocalhost:8080/tools-service/oauth/errorをURLを開くと、私は次のエラーが、私は/oauth/tokenにアクセスするときに表示されるエラー500 を取得します。 /oauth/errorのエラーはsimularです。私が正しいだ場合

HTTP Status 500 - No adapter for handler [public org.springframework.http.ResponseEntity org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.getAccessToken(java.security.Principal,java.lang.String,java.util.Map)]: Does your handler implement a supported interface like Controller?

これはTokenEndpoint.getAccessToken機能にエラーがあることを意味しますか?そのクラスはSpringフレームワークの一部であるため(実際には上手く見えるコードを参照しています)、問題は実際にそれらのクラスに関連しているとは思われません。それは私を無知にしてしまいます。

今、私はこれがなぜ起こり、どのようにこれを解決できるかを知りたいと思います。 私はおそらく、私はおそらく、ブラウザのURLを訪問することは許されていないと考えました。しかし、私はSparklr2(the Spring OAuth 2.0 sample application)で同じものを試してみると、XMLメッセージ(/ oauth2/token用)とエラー・ページ(/ oauth2/error用)を期待どおりに取得します。

どのようなヘルプやヒントも大歓迎です。


のweb.xmlから

セキュリティ関連の抜粋:

<filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

私のアプリケーションコンテキストは、私が作成した次のセキュリティ-config.xmlファイルをロードします。

<?xml version="1.0" encoding="UTF-8"?> 

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:sec="http://www.springframework.org/schema/security" 
    xmlns:oauth="http://www.springframework.org/schema/security/oauth2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.1.xsd 
         http://www.springframework.org/schema/security/oauth2 
         http://www.springframework.org/schema/security/spring-security-oauth2.xsd"> 

    <sec:http auto-config="true"> 
     <sec:intercept-url pattern="/**" access="ROLE_USER" /> 
    </sec:http> 

    <sec:authentication-manager> 
     <sec:authentication-provider> 
      <sec:user-service> 
       <sec:user name="user1" password="test123" authorities="ROLE_USER" /> 
       <sec:user name="user2" password="hello123" authorities="ROLE_USER" /> 
      </sec:user-service> 
     </sec:authentication-provider> 
    </sec:authentication-manager> 

    <sec:global-method-security pre-post-annotations="enabled" proxy-target-class="true"> 
     <sec:expression-handler ref="oauthExpressionHandler" /> 
    </sec:global-method-security> 


    <bean id="clientDetailsService" class="be.collectortools.rest.service.security.CollectorDetailsServiceImpl" /> 

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

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

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

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

</beans> 

CollectorClientDetails実装が唯一のダミーですコード:

@Service 
public class CollectorDetailsServiceImpl implements ClientDetailsService { 

    @Resource 
    private CollectorClientDetailsRepository collectorClientDetailsRepository; 

    @Override 
    public ClientDetails loadClientByClientId(final String clientId) throws OAuth2Exception { 
     CollectorClientDetails dummyClient = new CollectorClientDetails(); 
     dummyClient.setClientId(clientId); 

     return dummyClient; 
    } 

} 

答えて

11

この問題を数日間冷やすと、私は新しいGoogle検索を行いました。これは私をSpring Sourceフォーラムhttp://forum.springsource.org/showthread.php?130684-OAuth2-No-adapter-for-handler-exceptionに導いた。

ここで私はbanifouに同じ問題があることを発見しました。あなたは バニラsparklrから<mvc:annnotation-driven/>を取り除いよう

に見えます:デイブSyerは、このような質問に答えました。ハンドラアダプタに戻すと、 が定義されていると思います。

スプリングセキュリティは、リクエストとレスポンスを処理するためにSpring MVCフレームワークに依存しています。したがって、SpringセキュリティOAuthが動作するためには、MVCフレームワークを組み込み、適切に設定する必要があります。

解決策は、私は自分のアプリケーションのコンテキストに2個のタグを追加したことである:

  • <mvc:annotation-driven />

は、注釈を処理するためにMVCのframworkの準備ができてください。これは@FrameworkEndpointを適切に動作させます。エラーこのハンドラは、デフォルトのサーブレットへのすべての要求を転送します

  • <mvc:default-servlet-handler />
    • 500を与えた public class TokenEndpointに使用されて、後1、。したがって、他のすべてのURL HandlerMappingsの順に最後にとどまることが重要です。これは、 <mvc:annotation-driven>を使用する場合に当てはまります。 (春のドキュメントからの引用。)

      詳しい情報はここで見つけることができます: annotation-drivendefault-servlet-handlerを。

      <?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:context="http://www.springframework.org/schema/context" 
           xmlns:mvc="http://www.springframework.org/schema/mvc" 
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.1.xsd 
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 
      
          <context:component-scan base-package="be.collectortools.rest"/> 
          <context:annotation-config/> 
      
          <mvc:annotation-driven /> 
          <mvc:default-servlet-handler /> 
      
          <import resource="classpath:springmvc-resteasy.xml"/> 
          <import resource="mongo-config.xml"/> 
          <import resource="security-config.xml"/> 
      
      </beans> 
      
    +4

    私はこのような自分の疑問に答えることができてうれしいです。これは、あなたの問題がどれほど曖昧であっても、常に同じことを経験している他のユーザーが少なくとも1人いる場合の良い例です。 – Joe

    +0

    Vertongen、私は同じ問題を抱えていましたが、すべての関連するGoogleの結果が読み込まれていましたが、自分の問題のみが解決されました。が私のコードから抜けていました。あなた自身の質問に答えると私を助けてくれてありがとう! – szpetip

    +0

    ようこそ。私はこれがすべての時間の後にまだ有用であることを嬉しく思う。 – Vertongen

    関連する問題