Как пользовательская аннотация @EnableX может использовать свои атрибуты в импортированной конфигурации? ⇐ JAVA
-
Anonymous
Как пользовательская аннотация @EnableX может использовать свои атрибуты в импортированной конфигурации?
I decided to write this simple annotation for my Spring tests. Its purpose is to import a mock implementation of an AuthenticationExtractor interface. It's a custom interface that extracts a Mono of Authentication from a ServerWebExchange (similar to Spring's ReactiveAuthenticationConverter)
It has a limitation: the attribute in no way affects the mock object. It's instantiated without any knowledge of my annotation or its internals (which is totally expected, of course). In this example, the username stays user, not overriden as mickey_m. How can I share that information with the configuration class?
@ExtendWith(SpringExtension.class) @WebFluxTest(SomeController.class) @EnableMockAuthentication(username = "mickey_m") class SomeControllerTest { // ... import org.springframework.context.annotation.Import; @Import(MockAuthenticationExtractorConfiguration.class) public @interface EnableMockAuthentication { String username() default "user"; } import com.example.dynamicgateway.service.authenticationExtractor.AuthenticationExtractor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.List; @Configuration public class MockAuthenticationExtractorConfiguration { @Bean public AuthenticationExtractor mockAuthenticationExtractor() { return new AuthenticationExtractor() { @Override public Mono tryExtractAuthentication(ServerWebExchange exchange) { return Mono.just(new TestingAuthenticationToken("user", "password", List.of(new SimpleGrantedAuthority("user")))); } @Override public boolean isSupportedSource(ServerWebExchange exchange) { return true; } }; } }
Источник: https://stackoverflow.com/questions/781 ... configurat
I decided to write this simple annotation for my Spring tests. Its purpose is to import a mock implementation of an AuthenticationExtractor interface. It's a custom interface that extracts a Mono of Authentication from a ServerWebExchange (similar to Spring's ReactiveAuthenticationConverter)
It has a limitation: the attribute in no way affects the mock object. It's instantiated without any knowledge of my annotation or its internals (which is totally expected, of course). In this example, the username stays user, not overriden as mickey_m. How can I share that information with the configuration class?
@ExtendWith(SpringExtension.class) @WebFluxTest(SomeController.class) @EnableMockAuthentication(username = "mickey_m") class SomeControllerTest { // ... import org.springframework.context.annotation.Import; @Import(MockAuthenticationExtractorConfiguration.class) public @interface EnableMockAuthentication { String username() default "user"; } import com.example.dynamicgateway.service.authenticationExtractor.AuthenticationExtractor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.TestingAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.List; @Configuration public class MockAuthenticationExtractorConfiguration { @Bean public AuthenticationExtractor mockAuthenticationExtractor() { return new AuthenticationExtractor() { @Override public Mono tryExtractAuthentication(ServerWebExchange exchange) { return Mono.just(new TestingAuthenticationToken("user", "password", List.of(new SimpleGrantedAuthority("user")))); } @Override public boolean isSupportedSource(ServerWebExchange exchange) { return true; } }; } }
Источник: https://stackoverflow.com/questions/781 ... configurat