-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Page with permitAll is no longer accessible via auto-configured MockMvc #28759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the sample, @martinvisser. The cause of the problem is that |
A workaround for this problem is to remove the error page security filter by adding the following bean to the configuration used in your application's tests: @Bean
static BeanFactoryPostProcessor removeErrorSecurityFilter() {
return (beanFactory) ->
((DefaultListableBeanFactory)beanFactory).removeBeanDefinition("errorPageSecurityInterceptor");
} |
I also have the same problem with @Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
class SecurityConfig(private val userService: UserService) : WebSecurityConfigurerAdapter() {
companion object {
private val whitelist = arrayOf(
"/registration", "/registration/*", "/login", "/user/exists/*"
)
}
override fun configure(webSecurity: WebSecurity) { webSecurity.ignoring().antMatchers(*whitelist) }
override fun configure(auth: AuthenticationManagerBuilder) { auth.authenticationProvider(authProvider()) }
@Bean override fun authenticationManagerBean(): AuthenticationManager = super.authenticationManagerBean()
@Bean fun passwordEncoder(): PasswordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder()
@Bean fun authProvider (): DaoAuthenticationProvider = DaoAuthenticationProvider().apply { setUserDetailsService(userService); setPasswordEncoder(BCryptPasswordEncoder()) }
Result java.lang.AssertionError: Status expected:<200> but was:<401>
Expected :200
Actual :401 My test: @SpringBootTest
@AutoConfigureMockMvc
internal class LoginControllerTest(
@Autowired private val mockMvc: MockMvc,
@Autowired private val userService: UserService,
) {
private val loginUrl = "/login"
private val email = """[email protected]"""
private val principal = """{ "email": "$email", "password": "test" }"""
@Test
fun `login and logout`() {
val result = mockMvc.perform(
post(loginUrl).contentType(APPLICATION_JSON)
.content(principal)
)
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk)
} |
@steklopod That does look like the same problem. If you haven't done so already, please try the workaround. |
Thank you. It solved my problem. I just put it my
@Bean
fun removeErrorSecurityFilter(): BeanFactoryPostProcessor =
BeanFactoryPostProcessor { beanFactory: ConfigurableListableBeanFactory ->
(beanFactory as DefaultListableBeanFactory).removeBeanDefinition("errorPageSecurityInterceptor")
} |
I opened a similar ticket in Spring Security's Github repo: spring-projects/spring-security#10544. So...
I like to understand what's going on in Spring and especially security-wise, but here I feel a little lost: 1. Finding the root of the problemI have turned on debugging on all my configs and set
Apart from that, the Was is from this that you found that this filter was the culprit or did I miss something? 2. What does
|
I investigated the matter myself and found an additional bug. Explanations to the above and info about this bug can be found at #28818 |
As asked on gitter I found an issue after upgrading to Spring Boot 2.6.0: running a
@SpringBootTest
with@AutoConfigureMockMvc
a login page (not limited to) is no longer accessible after the upgrade. The same configuration that worked on Spring Boot 2.5.7 now triggers a 401. Tracing this lead me to theErrorPageSecurityFilter
.Since 2.6.0 the initial request gets granted, but the (mock) filter chain goes through the
ErrorPageSecurityFilter
and denies in later.I made a small example project to reproduce the issue: https://github.com/martinvisser/error-page-security-filter-issue.
For reasons I can't exactly remember I had multiple configuration extending from
WebSecurityConfigurerAdapter
which worked in Spring Boot 2.5.7. Merging the two configurations into one fixed the issue for me, but it does still sound like unforeseen and unwanted behavior.This worked in 2.5.7, but fails in 2.6.0:
This works in both though:
The text was updated successfully, but these errors were encountered: