Spring-Boot-setup-Sicherheit für die Prüfung
Ich bin nicht korrekt konfigurieren der security in meinen tests.
Meine web-security-Konfiguration:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/**").hasRole("USER")
.and()
.httpBasic()
;
}
}
Und meine test-Klasse:
@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())
;
}
}
Bekomme ich einen 404-Statuscode Bedeutung der security layer ist nicht ausgeführt, so ist es nicht richtig konfiguriert in meiner test-Klasse.
Ich habe versucht, wechseln die Klassen aus @ContextConfiguration
zu @SpringApplicationConfiguration
ohne Erfolg.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Nehmen Sie die folgenden änderungen an Ihrem code:
Wie gesagt in Referenz für Spring Security 4.0.4:
Beispiel: