-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
AopTestUtils.getUltimateTargetObject results in stack overflow for proxy backed by LazyInitTargetSource #29215
Comments
Thanks for the sample. It was very helpful in diagnosing the problem. I believe that the root cause is a bug in package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.util.AopTestUtils;
@SpringJUnitConfig
class MinimalTests {
@Test
void test(@Autowired AuthenticationManager authenticationManager) {
AopTestUtils.getUltimateTargetObject(authenticationManager);
}
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
static class MinimalConfiguration {
@Bean
AuthenticationManager authenticationManagerBean(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
}
} And these dependencies (using Spring Boot 2.7.4 purely for dependency management): <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency> We'll transfer this issue to the Spring Framework team so that they can investigate. |
@lako12, thanks for bringing this to our attention! @wilkinsona, thanks for analyzing it and providing the minimal reproducer! It turns out that If that scenario occurs, In light of that, my plan is to revise |
Hi everyone. Despite the AOP bug, the Spring Security team does not recommend exposing the @Bean
AuthenticationManager authenticationManager(UserDetailsService myUserDetailsService, PasswordEncoder encoder) {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(myUserDetailsService);
provider.setPasswordEncoder(encoder);
return new ProviderManager(provider);
} You can also rely on the See:
|
adding spring boot (inital smoke) test, workaround for spring-projects/spring-framework#29215
Team Decision After further consideration, we have decided not to make any change to
However, it does make sense to highlight the limitations of Regarding the use of public static <T> T getUltimateTargetObject(Object candidate) {
Assert.notNull(candidate, "Candidate must not be null");
try {
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
TargetSource targetSource = advised.getTargetSource();
if (targetSource.isStatic()) {
Object target = targetSource.getTarget();
if (target != null) {
return (T) getUltimateTargetObject(target);
}
}
}
}
catch (Throwable ex) {
throw new IllegalStateException("Failed to unwrap proxied object", ex);
}
return (T) candidate;
} In light of the above, we are closing this issue. On the Boot side, I've created spring-projects/spring-boot#32632 as a follow up. On the Framework side, I've created #29276 to address the documentation enhancement. |
@marcusdacoregio I tried to implement a spring boot microservice example. I have the same issue in my OrderServiceTest and OrderControllerTest in order service. I asked a question in stackoverflow. Here is the link : https://stackoverflow.com/questions/74633891/spring-boot-microservices-servicetest-and-controllertest-for-junit-throwing-ja |
Hi @Rapter1990, please see this issue and contribute if necessary. |
Security configuration class
Controller
Test class
I have a simple application, if I try to start the main it works, however when I start my test class I get this error:
In debug I tried to recover the error, but I only see this
java.lang.IllegalStateException: Failed to unwrap proxied object
.The test only works if I remove the
authenticationManagerBean
, but i needauthenticationManagerBean
to injectAutthenticationManager
in my app.I know that I could use
WebSecurityConfigurerAdapter
and then add the bean going to override the method of this class. But asWebSecurityConfigurerAdapter
is deprecated, I would not want to use it.This problem arose from the fact that I have switched from
WebSecurityConfigurerAdapter
toSecurityFilterChain
and am getting this error as I changed the config class.I created this simple project which replicates the error of my main project. If you try to start the application it works, but the test doesn't. https://github.com/lako12/demo
The project is very simple, there are only 3 classes plus a test class.
The text was updated successfully, but these errors were encountered: