Many answers to similar questions explains that permitAll()
means "allow all authenticated users", and that if you want to skip authentication, you need to do
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/chores");
}
This does not work for me.
In particular, here is my controller test that I would like to pass.
@SpringBootTest
@AutoConfigureMockMvc
public class ChoreApplicationTest
{
@Autowired
private MockMvc mockMvc;
@Test
public void choresShouldBeEmptyAtStart() throws Exception
{
this.mockMvc.perform(get("/chores")).
andExpect(status().isOk());
}
Test results:
java.lang.AssertionError: Status expected:<200> but was:<401>
Expected :200
Actual :401
Here's how I'm setting up the security:
@Configuration
@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter
{
@Override
public void configure(WebSecurity web) throws Exception
{
web.ignoring().antMatchers("/chores");
super.configure(web);
}
}
I reckon you can imagine what's in the Chores controller, but for completeness here's the relevant part:
@RestController
public class ChoreController
{
private final ChoreRepository repository;
ChoreController(ChoreRepository repository)
{
this.repository = repository;
}
@GetMapping("/chores")
List<Chore> all()
{
return this.repository.findAll();
}
}
Here is the request that the test prints in the output:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /chores
Parameters = {}
Headers = []
Body = null
Session Attrs = {SPRING_SECURITY_SAVED_REQUEST=DefaultSavedRequest[http://localhost/chores]}
So why is my test getting a return code of 401, and how can I fix it?