पर्यावरण अंदर: मैं एक वसंत बूट आधारित 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
इस कक्षा को मेरे (maven प्रोजेक्ट) में जोड़ना src/test/जावा मेरे लिए स्प्रिंग बूट 1.5.4 और 1.5.9 के साथ काम करता है। मैं अब उपयोगकर्ता @WithMockUser के रूप में उम्मीद कर सकता हूं। – DaShaun
इस प्रोजेक्ट को मेरी प्रोजेक्ट में जोड़ना मुझे सुरक्षा को बाईपास करने की इजाजत देता है हालांकि यह उपयोगकर्ता के मामलों की तरह प्रतीत नहीं होता है। मैं अपने टेस्ट उपयोगकर्ताओं को जो दायरा देता हूं, उसके बावजूद यह हमेशा सुरक्षा को पास करता है। क्या यह इरादा है? – Rig