|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2018 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.configuration;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Modifier; |
| 20 | + |
19 | 21 | import org.junit.jupiter.api.Test;
|
20 | 22 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 23 | +import org.mockito.Mockito; |
21 | 24 |
|
| 25 | +import org.springframework.aop.framework.ProxyFactory; |
22 | 26 | import org.springframework.beans.factory.BeanClassLoaderAware;
|
23 | 27 | import org.springframework.beans.factory.BeanFactoryAware;
|
24 | 28 | import org.springframework.beans.factory.DisposableBean;
|
|
31 | 35 | import org.springframework.context.MessageSourceAware;
|
32 | 36 | import org.springframework.context.annotation.Bean;
|
33 | 37 | import org.springframework.context.annotation.Configuration;
|
| 38 | +import org.springframework.core.NativeDetector; |
34 | 39 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
35 | 40 | import org.springframework.security.config.test.SpringTestContext;
|
36 | 41 | import org.springframework.security.config.test.SpringTestContextExtension;
|
37 | 42 | import org.springframework.web.context.ServletContextAware;
|
38 | 43 |
|
39 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
| 45 | +import static org.assertj.core.api.Assertions.assertThatException; |
40 | 46 | import static org.mockito.ArgumentMatchers.isNotNull;
|
| 47 | +import static org.mockito.BDDMockito.given; |
41 | 48 | import static org.mockito.Mockito.mock;
|
42 | 49 | import static org.mockito.Mockito.verify;
|
43 | 50 |
|
@@ -132,6 +139,59 @@ public void autowireBeanFactoryWhenBeanNameAutoProxyCreatorThenWorks() {
|
132 | 139 | assertThat(bean.doStuff()).isEqualTo("null");
|
133 | 140 | }
|
134 | 141 |
|
| 142 | + @Test |
| 143 | + void postProcessWhenObjectIsCgLibProxyAndInNativeImageThenUseExistingBean() { |
| 144 | + try (var detector = Mockito.mockStatic(NativeDetector.class)) { |
| 145 | + given(NativeDetector.inNativeImage()).willReturn(true); |
| 146 | + |
| 147 | + ProxyFactory proxyFactory = new ProxyFactory(new MyClass()); |
| 148 | + proxyFactory.setProxyTargetClass(!Modifier.isFinal(MyClass.class.getModifiers())); |
| 149 | + MyClass myClass = (MyClass) proxyFactory.getProxy(); |
| 150 | + |
| 151 | + this.spring.register(Config.class, myClass.getClass()).autowire(); |
| 152 | + this.spring.getContext().getBean(myClass.getClass()).setIdentifier("0000"); |
| 153 | + |
| 154 | + MyClass postProcessed = this.objectObjectPostProcessor.postProcess(myClass); |
| 155 | + assertThat(postProcessed.getIdentifier()).isEqualTo("0000"); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void postProcessWhenObjectIsCgLibProxyAndInNativeImageAndBeanDoesNotExistsThenIllegalStateException() { |
| 161 | + try (var detector = Mockito.mockStatic(NativeDetector.class)) { |
| 162 | + given(NativeDetector.inNativeImage()).willReturn(true); |
| 163 | + |
| 164 | + ProxyFactory proxyFactory = new ProxyFactory(new MyClass()); |
| 165 | + proxyFactory.setProxyTargetClass(!Modifier.isFinal(MyClass.class.getModifiers())); |
| 166 | + MyClass myClass = (MyClass) proxyFactory.getProxy(); |
| 167 | + |
| 168 | + this.spring.register(Config.class).autowire(); |
| 169 | + |
| 170 | + assertThatException().isThrownBy(() -> this.objectObjectPostProcessor.postProcess(myClass)) |
| 171 | + .havingRootCause() |
| 172 | + .isInstanceOf(IllegalStateException.class) |
| 173 | + .withMessage( |
| 174 | + """ |
| 175 | + Failed to resolve an unique bean (single or primary) of type [class org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessorTests$MyClass$$SpringCGLIB$$0] from the BeanFactory. |
| 176 | + Because the object is a CGLIB Proxy, a raw bean cannot be initialized during runtime in a native image. |
| 177 | + """); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + static class MyClass { |
| 182 | + |
| 183 | + private String identifier = "1234"; |
| 184 | + |
| 185 | + String getIdentifier() { |
| 186 | + return this.identifier; |
| 187 | + } |
| 188 | + |
| 189 | + void setIdentifier(String identifier) { |
| 190 | + this.identifier = identifier; |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
135 | 195 | @Configuration
|
136 | 196 | static class Config {
|
137 | 197 |
|
|
0 commit comments