/** * Performs post processing of an object. The default is to delegate to the * {@link ObjectPostProcessor}. * * @param object the Object to post process * @return the possibly modified Object to use */ protected <P> P postProcess(P object) { return this.objectPostProcessor.postProcess(object); }
@Autowired(required = false) public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcessor) { this.objectPostProcessor = objectPostProcessor; this.defaultMethodExpressionHandler = objectPostProcessor .postProcess(defaultMethodExpressionHandler); }
@Autowired(required = false) public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcessor) { defaultExpressionHandler = objectPostProcessor.postProcess(defaultExpressionHandler); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public Object postProcess(Object object) { for (ObjectPostProcessor opp : postProcessors) { Class<?> oppClass = opp.getClass(); Class<?> oppType = GenericTypeResolver.resolveTypeArgument(oppClass, ObjectPostProcessor.class); if (oppType == null || oppType.isAssignableFrom(object.getClass())) { object = opp.postProcess(object); } } return object; }
@Autowired public void configure(ObjectPostProcessor<Object> p) { p.postProcess(this.toTest); } }
/** * Creates the Spring Security Filter Chain * @return the {@link Filter} that represents the security filter chain * @throws Exception */ @Bean(name = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME) public Filter springSecurityFilterChain() throws Exception { boolean hasConfigurers = webSecurityConfigurers != null && !webSecurityConfigurers.isEmpty(); if (!hasConfigurers) { WebSecurityConfigurerAdapter adapter = objectObjectPostProcessor .postProcess(new WebSecurityConfigurerAdapter() { }); webSecurity.apply(adapter); } return webSecurity.build(); }
@Autowired public void configure(ObjectPostProcessor<Object> p) { p.postProcess(new Object()); } }
/** * Creates {@link MvcRequestMatcher} instances for the method and patterns passed in * * @param method the HTTP method to use or null if any should be used * @param mvcPatterns the Spring MVC patterns to match on * @return a List of {@link MvcRequestMatcher} instances */ protected final List<MvcRequestMatcher> createMvcMatchers(HttpMethod method, String... mvcPatterns) { Assert.state(!this.anyRequestConfigured, "Can't configure mvcMatchers after anyRequest"); ObjectPostProcessor<Object> opp = this.context.getBean(ObjectPostProcessor.class); if (!this.context.containsBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) { throw new NoSuchBeanDefinitionException("A Bean named " + HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME +" of type " + HandlerMappingIntrospector.class.getName() + " is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext."); } HandlerMappingIntrospector introspector = this.context.getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, HandlerMappingIntrospector.class); List<MvcRequestMatcher> matchers = new ArrayList<>( mvcPatterns.length); for (String mvcPattern : mvcPatterns) { MvcRequestMatcher matcher = new MvcRequestMatcher(introspector, mvcPattern); opp.postProcess(matcher); if (method != null) { matcher.setMethod(method); } matchers.add(matcher); } return matchers; }
throws Exception { webSecurity = objectPostProcessor .postProcess(new WebSecurity(objectPostProcessor)); if (debugEnabled != null) { webSecurity.debug(debugEnabled);
@SuppressWarnings("unchecked") private <T> T lazyBean(Class<T> interfaceName) { LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource(); String[] beanNamesForType = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( applicationContext, interfaceName); if (beanNamesForType.length == 0) { return null; } String beanName; if (beanNamesForType.length > 1) { List<String> primaryBeanNames = Arrays.stream(beanNamesForType) .filter(i -> applicationContext instanceof ConfigurableApplicationContext) .filter(n -> ((ConfigurableApplicationContext) applicationContext).getBeanFactory().getBeanDefinition(n).isPrimary()) .collect(Collectors.toList()); Assert.isTrue(primaryBeanNames.size() != 0, () -> "Found " + beanNamesForType.length + " beans for type " + interfaceName + ", but none marked as primary"); Assert.isTrue(primaryBeanNames.size() == 1, () -> "Found " + primaryBeanNames.size() + " beans for type " + interfaceName + " marked as primary"); beanName = primaryBeanNames.get(0); } else { beanName = beanNamesForType[0]; } lazyTargetSource.setTargetBeanName(beanName); lazyTargetSource.setBeanFactory(applicationContext); ProxyFactoryBean proxyFactory = new ProxyFactoryBean(); proxyFactory = objectPostProcessor.postProcess(proxyFactory); proxyFactory.setTargetSource(lazyTargetSource); return (T) proxyFactory.getObject(); }
@Test public void getAuthenticationManagerWhenPostProcessThenUsesBeanClassLoaderOnProxyFactoryBean() throws Exception { this.spring.register(Sec2531Config.class).autowire(); ObjectPostProcessor<Object> opp = this.spring.getContext().getBean(ObjectPostProcessor.class); when(opp.postProcess(any())).thenAnswer(a -> a.getArgument(0)); AuthenticationConfiguration config = this.spring.getContext().getBean(AuthenticationConfiguration.class); config.getAuthenticationManager(); verify(opp).postProcess(any(ProxyFactoryBean.class)); }
@Test public void configureWhenRegisteringObjectPostProcessorThenInvokedOnExceptionTranslationFilter() { this.spring.register(ObjectPostProcessorConfig.class, DefaultSecurityConfig.class).autowire(); verify(ObjectPostProcessorConfig.objectPostProcessor) .postProcess(any(ExceptionTranslationFilter.class)); }
@Test public void buildWhenAddAuthenticationProviderThenDoesNotPerformRegistration() throws Exception { ObjectPostProcessor<Object> opp = mock(ObjectPostProcessor.class); AuthenticationProvider provider = mock(AuthenticationProvider.class); AuthenticationManagerBuilder builder = new AuthenticationManagerBuilder(opp); builder.authenticationProvider(provider); builder.build(); verify(opp, never()).postProcess(provider); }
@Test public void configureWhenRegisteringObjectPostProcessorThenInvokedOnExceptionTranslationFilter() { this.spring.register(ObjectPostProcessorConfig.class, DefaultSecurityConfig.class).autowire(); verify(ObjectPostProcessorConfig.objectPostProcessor) .postProcess(any(RequestCacheAwareFilter.class)); }
@Test public void customAuthenticationEventPublisherWithWeb() throws Exception { ObjectPostProcessor<Object> opp = mock(ObjectPostProcessor.class); AuthenticationEventPublisher aep = mock(AuthenticationEventPublisher.class); when(opp.postProcess(any())).thenAnswer(a -> a.getArgument(0)); AuthenticationManager am = new AuthenticationManagerBuilder(opp) .authenticationEventPublisher(aep) .inMemoryAuthentication() .and() .build(); try { am.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); } catch (AuthenticationException success) {} verify(aep).publishAuthenticationFailure(any(), any()); }
@Test public void postProcessWhenDisposableBeanThenAwareInvoked() throws Exception { this.spring.register(Config.class).autowire(); DisposableBean toPostProcess = mock(DisposableBean.class); this.objectObjectPostProcessor.postProcess(toPostProcess); this.spring.getContext().close(); verify(toPostProcess).destroy(); }
@Test public void postProcessWhenApplicationContextAwareThenAwareInvoked() { this.spring.register(Config.class).autowire(); ApplicationContextAware toPostProcess = mock(ApplicationContextAware.class); this.objectObjectPostProcessor.postProcess(toPostProcess); verify(toPostProcess).setApplicationContext(isNotNull()); }
@Test public void postProcessWhenBeanClassLoaderAwareThenAwareInvoked() { this.spring.register(Config.class).autowire(); BeanClassLoaderAware toPostProcess = mock(BeanClassLoaderAware.class); this.objectObjectPostProcessor.postProcess(toPostProcess); verify(toPostProcess).setBeanClassLoader(isNotNull()); }
@Test public void postProcessWhenBeanFactoryAwareThenAwareInvoked() { this.spring.register(Config.class).autowire(); BeanFactoryAware toPostProcess = mock(BeanFactoryAware.class); this.objectObjectPostProcessor.postProcess(toPostProcess); verify(toPostProcess).setBeanFactory(isNotNull()); }
@Test public void postProcessWhenEnvironmentAwareThenAwareInvoked() { this.spring.register(Config.class).autowire(); EnvironmentAware toPostProcess = mock(EnvironmentAware.class); this.objectObjectPostProcessor.postProcess(toPostProcess); verify(toPostProcess).setEnvironment(isNotNull()); }