我无法在我的测试中正确配置安全性。 我的网络安全性配置:用于测试的Spring Boot安装安全性
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").hasRole("USER")
.and()
.httpBasic()
;
}
}
而我的测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@ContextConfiguration(classes = {Application.class, AppConfig.class, WebMvcConfig.class, WebSecurityConfig.class})
@WebAppConfiguration
public class TestControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(wac).dispatchOptions(true).build();
}
@Test
public void getTest() throws Exception {
mockMvc
.perform(get("/api/test"))
.andExpect(status().isForbidden())
;
}
}
我得到的是不执行404个状态码意味着安全层,所以它不是在我的测试类配置正确。 我尝试将@ContextConfiguration
的类切换为@SpringApplicationConfiguration
,但未成功。
谢谢!它解决了我的问题 – user3168219
这很好听! – geoand
谢谢你的回答!我花了一整天搞清楚我在做什么错了:) – jeremija