Describe the bug
Version: Spring 5.2.7.RELEASE
I'm using springSecurity() method in SecurityMockMvcConfigurers class.
public final class SecurityMockMvcConfigurers {
// I'm using this method
public static MockMvcConfigurer springSecurity() {
return new SecurityMockMvcConfigurer();
}
// ...
}
springSecurity() method is using default constructor in SecurityMockMvcConfigurer.
SecurityMockMvcConfigurer
final class SecurityMockMvcConfigurer extends MockMvcConfigurerAdapter {
// ....
SecurityMockMvcConfigurer() {
this.delegateFilter = new DelegateFilter();
}
// ...
private Filter getSpringSecurityFilterChain() {
return this.delegateFilter.delegate;
}
}
DelegateFilter
static class DelegateFilter implements Filter {
private Filter delegate;
DelegateFilter() {
}
// ...
}
If I use default constructor, getSpringSecurityFilterChain() method always return null.
To Reproduce
An exception occurs when the test is executed and used as follows.
@BeforeEach
fun setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply<DefaultMockMvcBuilder>(springSecurity())
.build()
}
java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
Expected behavior
Methods that use that default generator and that default generator should be deprecated.
Sample
final class SecurityMockMvcConfigurer extends MockMvcConfigurerAdapter {
private final DelegateFilter delegateFilter;
@Deprecated
SecurityMockMvcConfigurer() {
this.delegateFilter = new DelegateFilter();
}
// ...
}
public final class SecurityMockMvcConfigurers {
@Deprecated
public static MockMvcConfigurer springSecurity() {
return new SecurityMockMvcConfigurer();
}
// ...
}
and, This is a different problem.
private Filter getSpringSecurityFilterChain() {
return this.delegateFilter.delegate;
}
The following null check will not work.
Filter getDelegate() {
Filter result = this.delegate;
if (result == null) {
throw new IllegalStateException("delegate cannot be null. Ensure a Bean with the name "
+ BeanIds.SPRING_SECURITY_FILTER_CHAIN
+ " implementing Filter is present or inject the Filter to be used.");
}
return result;
}
This method seems to have to be changed as follows.
private Filter getSpringSecurityFilterChain() {
return this.delegateFilter.getDelegate();
}
If this was wrong, I want to fix it.
If it wasn't wrong, can you tell me how to use it?
Comment From: eleftherias
Thanks for the report @tramyu. I am not able to reproduce this issue, could you please share a minimal sample that reproduces the issue that you are seeing?
Comment From: kyucumber
I'm sorry. I solved it.
It was a problem caused by the disappearance of MockMvcSecurityAutoConfiguration.
Comment From: kyucumber
@eleftherias
I'm closing this issue because I think there's no problem.
I have a question. 🙏
Why not use the 'getDelegate()' method?
SecurityMockMvcConfigurer
private Filter getSpringSecurityFilterChain() {
return this.delegateFilter.delegate;
}