2016-11-18 7 views
0

私の春のRESTエンドポイントの認証(春の認証を使用)をテストする助けをいくつか使うことができます。 以下のアノテーション@PreAuthorize( "isAuthenticated()")を使用して、認証が必要なエンドポイントを定義しました。 これは動作しているようですが、Webサーバーを起動してURLに移動すると認証が求められ、その後適切な結果が得られるからです。RESTエンドポイントでの認証のテスト

私のテストでは問題が発生します。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = TestContext.class) 
@WebAppConfiguration 
public class ControllerTest { 

    @Autowired 
    private WebApplicationContext context; 

    private MockMvc mvc; 

    @Before 
    public void setup() { 
     mvc = MockMvcBuilders 
       .webAppContextSetup(context) 
       .apply(springSecurity()) 
       .build(); 
    } 

    @Test 
    public void testGetMethod() throws Exception { 
     mvc 
      .perform(get("/a-valid-url") 
         .accept(MediaType.APPLICATION_JSON)) 
      .andDo(print()) 
      .andExpect(status().isUnauthorized()); 
    } 

} 

TestContextクラスは、このような嘲笑されているコントローラ内のいくつかのautowiredクラスが含まれています java.lang.IllegalStateException:私は次のエラーを取得する次のように実行すると

@Configuration 
public class TestContext { 

    @Bean 
    public ClassToMock autowiredClass() { 
     return Mockito.mock(ClassToMock.class); 
    } 

} 

springSecurityFilterChainすることはできませんnullになります。 SpringSecurityFilterChainという名前のBeanがFilterを実装していることを確認するか、使用するFilterを挿入します。ときに私にも、しかし、私は常に取得

@Bean 
public Filter springSecurityFilterChain() { 
    return Mockito.mock(Filter.class); 
} 

をステータス= 200:

私はすべてを動作させるには、これまでに発見した唯一の解決策は、このように、TestContextクラスでspringSecurityFilterChain Beanを追加することですコントローラーで定義されていないURLを使用してください。

誰かが正しい方向に私を指すことができたら、それは非常に感謝しています!

編集:プロジェクトの設定の一部。

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> 

プロジェクトはTokenContextとAuthenticationStatelessContext構成XMLファイルが含まれています依存関係として別のプロジェクトを持っています。 DefaultMethodSecurityExpressionHandler、PreAuthenticatedAuthenticationProviderとUserDetailsByNameServiceWrapper:

TokenContext.xml

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

    <context:component-scan base-package="com.example.auth.service" resource-pattern="TokenServiceImpl.class"/> 
    <context:component-scan base-package="com.example.auth.service" resource-pattern="ActorRolServiceImpl.class"/> 
</beans> 

AuthenticationStatelessContext.xmlは、次のスプリング豆を定義します。

更新:春がでクラスとxmlファイルの読み込みを許可していないため、

@Configuration 
@ImportResource({ 
    "classpath:**/AuthenticationStatelessContext.xml", 
    "classpath:**/TokenContext.xml"}) 
public class TestContext2 { 

} 

とテスト

@ContextConfiguration(classes = {TestContext.class, TestContext2.class}) 

これにそのクラスを追加しました:TestContext2という名前のクラスを作成し @ContextConfigurationアノテーションで同じ時間。

テストを実行しようとすると、次のエラーが表示されます。 java.lang.IllegalStateException:springSecurityFilterChainをnullにすることはできません。

PS:使用されているspringframeworkバージョンは4.1.3で、スプリングセキュリティバージョンは4.0.4です。

答えて

0

springSecurityFilterChain Beanを挿入するには、Filterインターフェイスをモックするのは間違いです。

少なくとも1つの認証フィルタを使用してフィルタチェーンを挿入します。 UsernamePasswordAuthenticationFilter CasAuthenticationFilter BasicAuthenticationFilter。 thisあなたの目的のために注入するフィルターを理解するための春のセキュリティフィルターチェーンガイドをお読みください。

今のところ、mockitoはspringSecurityFilterChainのフィルタを設定しています。このフィルタはすべてのURLを許可し、意図したフィルタリングの動作を提供しません。

修正は、TestクラスのTestContextと共に適切なアプリケーションコンテキストへの参照を含めるのと同じくらい簡単です。アプリケーションの残りのコンテキストをどのように定義したかによって異なります。フィルタチェーンBeanが自動配線にも使用できる可能性があります。

更新:

がここTokenContextとAuthenticationStatelessContextをロードするためのバネを教える:TestContext

@ContextConfiguration(classes = TestContext.class) 

は、セキュリティフィルタチェーンのBeanを定義するための任意のコードを削除し

+0

はに時間を割いていただきありがとうございます応答する。 私はフィルタを嘲笑することが間違っていたという印象を受けましたが、それはテストを実行した唯一の方法でした。 DelegatingFilterProxyをspringSecurityFilterChain Beanとして定義しようとしましたが、StackOverflowErrorが返されました。 あなたが与えたリンクに基づいていくつかのフィルターをTestContextクラスに追加しようとしましたが、私はあきらめている間にさらにクラスを追加して別のものを設定しなければなりませんでした。 私は後で認証のテストを残しており、現在はプロジェクトの他の部分に焦点を当てています。 –

+0

@Peter Webアプリケーションとして実行するとすべてがうまく動作すると言われているので、filterChain beanを苦労せずに設定することができます。あなたのテストコードからも簡単に注入できる、あなたのWebアプリの春の文脈には何かがあるはずです。あなたのWebアプリケーションのコンテキストを見てみると少し助けになるかもしれません。 –

+0

あなたは私が何を探していなければならないのかを示すことができますか?いくつかの設定ファイルで質問を更新しました。 –

関連する問題