2016-03-30 27 views
1

私は問題があります(もちろん:))。私はSpring SecurityとSpring MVC(Rest APIを使用)のSpring 4.2アプリケーションがあり、RESTメソッドに存在する@Secured(ROLE_FOO)アノテーションの有効性をテストしたいと考えています。 このために私は春のセキュリティテストのライブラリをインストールする必要があります。 OK。 その後、私は公式のもののよういくつかのチュートリアル(またはドキュメントを)フォローアップ:http://docs.spring.io/autorepo/docs/spring-security/4.1.0.RC1/reference/htmlsingle/春のセキュリティテスト:@Secured(または@PreAuthorize)の注釈のテスト

ここで、すべての「uneccessary」コードを削除しようとしている私のテストコード(I'am

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@IntegrationTest 
public class UserResourceIntTest { 

    private MockMvc restUserMockMvc2; 

    @Autowired 
    private WebApplicationContext context; 

....//Injection, Mocks declarations here 

@Before 
    public void setup() { 




     this.restUserMockMvc2 = MockMvcBuilders.webAppContextSetup(context).apply(SecurityMockMvcConfigurers.springSecurity()).build(); 



    } 


    @Test 
    @WithMockUser(roles="ROLE_VIEWER") 
    public void testGetUserListe() throws Exception { 
     //here getAuthentication() returns null !!! why ??? 
     SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 
//  restUserMockMvc2.perform(get("/api/users/liste") 
//    .principal(SecurityContextHolder.getContext().getAuthentication())) 
//    .accept(MediaType.APPLICATION_JSON)) 
//    .andExpect(status().isForbidden()); 
//    .andExpect(content().contentType("application/json")); 
    } 

ここで私が望む方法で。テストへ:

@RestController 
@RequestMapping("/api") 
public class UserResource { 

    @RequestMapping(value = "https://stackoverflow.com/users/liste", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
    @Timed 
    @Transactional(readOnly = true) 
    @Secured({ AuthoritiesConstants.TC_ADMIN }) 
    public ResponseEntity<List<ManagedUserDTO>> getUserListe(Pageable pageable, Principal principal) throws URISyntaxException { 

     //doSomething... 
    } 

あなたは、なぜ私のテストでは教えてもらえます

SecurityContextHolder.getContext().getAuthentication() 

はnullを返しますか? @WithMockUserは自動的に(したがって主)

おかげでユーザを認証する必要があり

EDIT1:(唯一のセキュリティ命令に関する)試験のセットアップの一部:

@Inject 
private FilterChainProxy springSecurityFilterChain; 

@Inject 
private PageableHandlerMethodArgumentResolver pageableArgumentResolver; 

@Before 
public void setup() { 

.... 

this.restUserMockMvc2 = MockMvcBuilders 
       .standaloneSetup(userResource2) 
       .alwaysDo(print())    .apply(SecurityMockMvcConfigurers.springSecurity(springSecurityFilterChain)) 
       .setCustomArgumentResolvers(pageableArgumentResolver) 
       .build(); 

... 

} 

EDIT2:わずかであるために

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes = Application.class) 
@WebAppConfiguration 
@TestExecutionListeners(listeners={ServletTestExecutionListener.class, 
     DependencyInjectionTestExecutionListener.class, 
     DirtiesContextTestExecutionListener.class, 
     TransactionalTestExecutionListener.class, 
     WithSecurityContextTestExecutionListener.class}) 
public class UserResourceIntTest { 
} 
+0

あなたは春・テストのどのバージョンを使用していますか? 「Spring Securityのテストのサポートには、spring-test-4.1.3.RELEASE以上が必要です。 –

+0

pom.xmlにバージョンが指定されていません。スプリングに関して、唯一の指定されたバージョンは、のpom.xmlの最上部にある: ばねブートスタータ親 org.springframework.boot 1.3.1.RELEASE

+0

申し訳ありませんが、私のMavenの依存関係(jarファイル)では、私はspring-test-4.2.4.RELEASEを使用しています。 –

答えて

1

問題は、Spring SecurityのWithSecurityContextTestExecutionListenerが実行されていないことです。原因@IntegrationTestoverriding the default TestExecutionListenersです。

@IntegrationTestMockMvcを使用する必要はほとんどない可能性が高いので、完全に削除して問題を解決できるはずです。

または明示的のようなあなたのクラスにWithSecurityContextTestExecutionListenerを追加することによってこの問題を解決することができます

@TestExecutionListeners(listeners = { WithSecurityContextTestExecutionListener.class, IntegrationTestPropertiesListener.class, 
     DirtiesContextBeforeModesTestExecutionListener.class, 
     DependencyInjectionTestExecutionListener.class, 
     DirtiesContextTestExecutionListener.class, 
     TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class }) 
@IntegrationTest 
public class UserResourceIntTest { 
+0

ありがとうございますが、まだ動作しません。実際、私はエラーはありませんが、ダミーの役割を設定しても、エラーhttp 401を意味するわけではありません。テストは常にパスします。私のセットアップ設定:cf.テストセットアップコードのEDIT1。 –

関連する問題