Я пишу тест для простого контроллера с Spring Security.Форма входа включена.Когда пользователь вводит /books
URL, он перенаправляется на страницу входа.И это то, что я вижу в веб-консоли.GET
on /books
возвращает 302, затем /login
и статус 200.
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = BookController.class)
public class BookControllerIT {
@Autowired
private MockMvc mockMvc;
// ... some mock beans
@Test
public void shouldReturnUnauthorizedStatus() throws Exception {
mockMvc.perform(get("/books")).andExpect(status().is3xxRedirection());
}
}
Вот моя конфигурация безопасности:
@Configuration
@EnableWebSecurity
public class BasicSecurityConfiguration extends WebSecurityConfigurerAdapter {
private DataSource dataSource;
private BCryptPasswordEncoder encoder;
@Autowired
public BasicSecurityConfiguration(@Qualifier("security.datasource") DataSource dataSource, BCryptPasswordEncoder encoder) {
this.dataSource = dataSource;
this.encoder = encoder;
}
@Override
public void configure(WebSecurity web) throws Exception {
super.configure(web);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers("/").permitAll()
.and()
.authorizeRequests().antMatchers("/h2-console/**").permitAll()
.anyRequest()
.authenticated()
.and()
.formLogin()
.and()
.httpBasic()
.and()
.csrf().disable()
.headers().frameOptions().disable();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(encoder);
}
}
Почему мой тест не перенаправляется какв браузере ниже?
Я попытался добавить @Import(BasicSecurityConfiguration.class)
в свой тест, но я все еще получаю 401.
Это версия Spring Boot, которую я использую: springBootVersion = '2.1.0.M2'