2016-03-30 27 views
1

Bir sorunum var (tabiki :)). Spring Security ve Spring MVC (Rest API ile) ile bir yay 4.2 uygulamasına sahibim ve bir REST yönteminde bulunan @Secured (ROLE_FOO) ek açıklamasının etkinliğini test etmek istiyorum. Bu yüzden bunun için spring-security-test kütüphanesi kurmam gerekiyor. TAMAM. Bi'şey tüm "uneccessary" kod kaldırmaya çalışırken (http://docs.spring.io/autorepo/docs/spring-security/4.1.0.RC1/reference/htmlsingle/ İşteYay Güvenliği Testi: @Secured (veya @PreAuthorize) ek açıklamasını sınama

benim test kodu İşte

@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")); 
    } 

istediğim yöntemi: Sonra resmi gibi bazı dersler (veya doc) takip. testine:

@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... 
    } 

sen, nEDEN benim test söyleyebilir

SecurityContextHolder.getContext().getAuthentication() 

null değerini döndürüyor? @WithMockUser otomatik (dolayısıyla anapara)

teşekkürler kullanıcının kimliğini doğrulamak gerekir

Edit1: (sadece güvenlik talimatı ilgili) testin kurulum kısmı:

@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: Sadece olmak

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

kullandığınız bahar-testi hangi sürümü:

Alternatif açıkça gibi sınıf WithSecurityContextTestExecutionListener ekleyerek çözebilirsiniz? Referanstan "Spring Security’in test desteği, spring-test-4.1.3.RELEASE veya üstü gerektirir." –

+0

Pom.xml'de belirtilen bir sürüm yok. yay çizme-marş-ebeveyn org.springframework.boot 1.3.1.RELEASE

+0

: bahar ile ilgili olarak, yalnızca belirtilen sürüm pom.xml üst kısmındadır Maalesef, maven bağımlılığımda (kavanoz dosyaları), o zaman kullanılan bir bahar testi-4.2.4.RELEASE var. –

cevap

1

sorun Bahar Güvenlik en WithSecurityContextTestExecutionListener be değil yürütülmekte olan şudur: sınıf tanımının net Neden @IntegrationTest, overriding the default TestExecutionListeners.

Büyük olasılıkla, MockMvc ile @IntegrationTest'a ihtiyacınız yoktur, bu yüzden onu tamamen kaldırabilmeniz ve sorununuzu çözebilmeniz gerekir.

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

sorun olabilir gibi @ EntegrasyonTest'i kaldırmayı deneyin, Teşekkürler ama yine de işe yaramaz. Aslında, hiç hatam yok, ancak kukla rolünü ayarlamak bir hataya işaret etmiyor http 401. Test her zaman geçer. Kurulum yapılandırmam: cf. Test kurulum kodu için EDIT1. –