5

पर्यावरण अंदर: मैं एक वसंत बूट आधारित microservice वास्तुकला कई ढांचागत सेवाओं और संसाधन सेवाएं (व्यापार तर्क युक्त) से मिलकर आवेदन किया है। प्रमाणीकरण और प्रमाणीकरण को उपयोगकर्ता इकाइयों के प्रबंधन और ग्राहकों के लिए जेडब्ल्यूटी टोकन बनाने के लिए ओएथ 2-सेवा द्वारा नियंत्रित किया जाता है।उपयोग @WithMockUser (@SpringBootTest के साथ) एक OAuth2 संसाधन सर्वर आवेदन

अपनी संपूर्णता में एक भी microservice आवेदन का परीक्षण करने के मैं TestNG, spring.boot.test, org.springframework.security.test साथ परीक्षण का निर्माण करने की कोशिश की ...

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, properties = {"spring.cloud.discovery.enabled=false", "spring.cloud.config.enabled=false", "spring.profiles.active=test"}) 
@AutoConfigureMockMvc 
@Test 
public class ArtistControllerTest extends AbstractTestNGSpringContextTests { 

    @Autowired 
    private MockMvc mvc; 

    @BeforeClass 
    @Transactional 
    public void setUp() { 
    // nothing to do 
    } 

    @AfterClass 
    @Transactional 
    public void tearDown() { 
    // nothing to do here 
    } 

    @Test 
    @WithMockUser(authorities = {"READ", "WRITE"}) 
    public void getAllTest() throws Exception { 

    // EXPECT HTTP STATUS 200 
    // BUT GET 401 
    this.mvc.perform(get("/") 
      .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()) 
    } 
} 

जहां सुरक्षा (संसाधन सर्वर) कॉन्फ़िगरेशन निम्न

@Configuration 
@EnableResourceServer 
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    // get the configured token store 
    @Autowired 
    TokenStore tokenStore; 

    // get the configured token converter 
    @Autowired 
    JwtAccessTokenConverter tokenConverter; 

    /** 
    * !!! configuration of springs http security !!! 
    */ 
    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/**").authenticated(); 
    } 

    /** 
    * configuration of springs resource server security 
    */ 
    @Override 
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception { 
    // set the configured tokenStore to this resourceServer 
    resources.resourceId("artist").tokenStore(tokenStore); 
    } 

} 

और निम्न विधि आधारित सुरक्षा जांच एनोटेटेड इन्स है नियंत्रक वर्ग

@PreAuthorize("hasAuthority('READ')") 
@RequestMapping(value = "/", method = RequestMethod.GET) 
public List<Foo> getAll(Principal user) { 
    List<Foo> foos = fooRepository.findAll(); 
    return foos; 
} 

मैंने सोचा था कि काम करेगा आईडीई लेकिन जब परीक्षण चल रहा है मैं केवल एक अभिकथन त्रुटि

java.lang.AssertionError: Status 
Expected :200 
Actual :401 


प्रश्न मिलती है: वहाँ कुछ पूरी तरह से स्पष्ट है कि मैं कर रहा हूँ है गलत? या @WithMockUser @SpringBootTest और @AutoConfigureMockMvc के साथ oAuth2 वातावरण में काम नहीं करेगा? यदि यह मामला है ... इस तरह के एक (एकीकरण) परीक्षण के हिस्से के रूप में मार्ग और विधि आधारित सुरक्षा विन्यास परीक्षण के लिए सबसे अच्छा तरीका क्या होगा?


परिशिष्ट: मैं भी निम्नलिखित की तरह कुछ की तरह अलग अलग दृष्टिकोण की कोशिश की ... लेकिन यह एक ही परिणाम :(

this.mvc.perform(get("/") 
     .with(user("admin").roles("READ","WRITE").authorities(() -> "READ",() -> "WRITE")) 
     .accept(MediaType.APPLICATION_JSON)) 

देखने के लिए नेतृत्व:
spring security testing
spring boot 1.4 testing

उत्तर

4

@WithMockUserमें प्रमाणीकरण SecurityContext पैदा करता है। with(user("username")) के लिए भी लागू होता है।

डिफ़ॉल्ट रूप से OAuth2 प्रमाणीकरणप्रोसेसिंगफिल्टर सुरक्षा कॉन्टेक्स्ट का उपयोग नहीं करता है, लेकिन हमेशा टोकन ("स्टेटलेस") से प्रमाणीकरण का निर्माण करता है।

आप आसानी से इस व्यवहार को गलत पर संसाधन सर्वर सुरक्षा विन्यास में राज्यविहीन ध्वज स्थापित कर बदल सकते हैं: अपने परीक्षण संदर्भों में झूठी, केवल करने के लिए

@Configuration 
@EnableResourceServer 
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 

    @Override 
    public void configure(ResourceServerSecurityConfigurer security) throws Exception { 
     security.stateless(false); 
    } 
} 

बेशक, करने के लिए ध्वज सेट।

+0

इस कक्षा को मेरे (maven प्रोजेक्ट) में जोड़ना src/test/जावा मेरे लिए स्प्रिंग बूट 1.5.4 और 1.5.9 के साथ काम करता है। मैं अब उपयोगकर्ता @WithMockUser के रूप में उम्मीद कर सकता हूं। – DaShaun

+0

इस प्रोजेक्ट को मेरी प्रोजेक्ट में जोड़ना मुझे सुरक्षा को बाईपास करने की इजाजत देता है हालांकि यह उपयोगकर्ता के मामलों की तरह प्रतीत नहीं होता है। मैं अपने टेस्ट उपयोगकर्ताओं को जो दायरा देता हूं, उसके बावजूद यह हमेशा सुरक्षा को पास करता है। क्या यह इरादा है? – Rig

2

मेरे पास था मुझे मुद्दा है, और एक ही तरीका है मैंने पाया एक टोकन बनाने गया था और mockMvc में इसका उपयोग करने

mockMvc.perform(get("/resource") 
        .with(oAuthHelper.bearerToken("test")) 

और OAuthHelper करते हैं:

@Component 
@EnableAuthorizationServer 
public class OAuthHelper extends AuthorizationServerConfigurerAdapter { 

    @Autowired 
    AuthorizationServerTokenServices tokenservice; 

    @Autowired 
    ClientDetailsService clientDetailsService; 

    public RequestPostProcessor bearerToken(final String clientid) { 
     return mockRequest -> { 
      OAuth2AccessToken token = createAccessToken(clientid); 
      mockRequest.addHeader("Authorization", "Bearer " + token.getValue()); 
      return mockRequest; 
     }; 
    } 

    OAuth2AccessToken createAccessToken(final String clientId) { 
     ClientDetails client = clientDetailsService.loadClientByClientId(clientId); 
     Collection<GrantedAuthority> authorities = client.getAuthorities(); 
     Set<String> resourceIds = client.getResourceIds(); 
     Set<String> scopes = client.getScope(); 

     Map<String, String> requestParameters = Collections.emptyMap(); 
     boolean approved = true; 
     String redirectUrl = null; 
     Set<String> responseTypes = Collections.emptySet(); 
     Map<String, Serializable> extensionProperties = Collections.emptyMap(); 

     OAuth2Request oAuth2Request = new OAuth2Request(requestParameters, clientId, authorities, 
       approved, scopes, resourceIds, redirectUrl, responseTypes, extensionProperties); 

     User userPrincipal = new User("user", "", true, true, true, true, authorities); 
     UsernamePasswordAuthenticationToken authenticationToken = 
       new UsernamePasswordAuthenticationToken(userPrincipal, null, authorities); 
     OAuth2Authentication auth = new OAuth2Authentication(oAuth2Request, authenticationToken); 

     return tokenservice.createAccessToken(auth); 
    } 

    @Override 
    public void configure(final ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("test") 
       .authorities("READ"); 
    } 

} 
+1

समस्या यह है कि मेरी सुरक्षा सेवा जो उत्पादन में टोकन बनाती है वह एक अन्य माइक्रोस्कोस है जो प्राधिकरण और प्रमाणीकरण की जांच करने के लिए टोकन का उपयोग करती है ... और चूंकि यह एक जेडब्ल्यूटी टोकन बनाने के लिए एक निजी कुंजी फ़ाइल का उपयोग करता है टोकन और मैं अपने सभी संसाधन माइक्रो सेवाओं पर निजी टोकन फैलाना नहीं चाहता हूं। या मुझे कुछ गलत हो रहा है? – David

+0

हां, मेरे पास एक ही डिज़ाइन है, इसलिए संसाधनों का पर्दाफाश करने वाले माइक्रोस्कोस का परीक्षण करने के लिए, मैंने परीक्षण किया है –

+0

में मेरे पास ऑथ सेवा में एक निजी कुंजी है और अन्य माइक्रोस्कोस –

संबंधित मुद्दे