我正在嘗試測試其中一項的訪問,該訪問@RestController由定制的Spring Security配置保護。我的用例如下:HTTP GETto/someEndpoint通過身份驗證得到保護,但是POST對同一終結(jié)點的HTTP請求卻不被保護。當我啟動應用程序并用我的前端或郵遞員對其進行測試時,它工作正?!,F(xiàn)在,我正在嘗試MockMvc使用安全性配置編寫測試。我已經(jīng)在StackOverflow上通過很多答案獲得了成功,但是沒有任何幫助。我的測試設置如下所示:@RunWith(SpringRunner.class)@WebMvcTest(controllers = MyController.class)@WebAppConfiguration@ContextConfigurationpublic class AssessmentControllerTest { private MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @Before public void init() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .alwaysDo(print()) .apply(SecurityMockMvcConfigurers.springSecurity()) .build(); } // some test methods}通過此設置,我所有的端點都得到了保護,甚至HTTPPOST都返回401而不是201。我還啟用了調(diào)試日志以提高安全性,并且在調(diào)試日志中指出測試使用,default configure(HttpSecurity)但在日志中找不到我的任何AntMatchers:2018-07-04 19:20:02.829 DEBUG 2237 --- [ main] s.s.c.a.w.c.WebSecurityConfigurerAdapter : Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).2018-07-04 19:20:03.097 DEBUG 2237 --- [ main] edFilterInvocationSecurityMetadataSource : Adding web access control expression 'authenticated', for org.springframework.security.web.util.matcher.AnyRequestMatcher@12018-07-04 19:20:03.127 DEBUG 2237 --- [ main] o.s.s.w.a.i.FilterSecurityInterceptor : Validated configuration attributes2018-07-04 19:20:03.130 DEBUG 2237 --- [ main] o.s.s.w.a.i.FilterSecurityInterceptor : Validated configuration attributes2018-07-04 19:20:03.161 INFO 2237 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: 通??梢栽贛ockMvc測試期間使用我的具體Spring Security配置,還是必須在測試期間使用引導整個Spring上下文@SpringBootTest?我正在使用(帶有Java 1.8的Spring Boot 2.0.3.RELEASE)
添加回答
舉報
0/150
提交
取消