001/*
002 * Copyright 2012-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.security;
018
019import java.util.Map;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.context.ApplicationContext;
027import org.springframework.context.annotation.Bean;
028import org.springframework.context.annotation.Configuration;
029import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
030import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
031import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
032
033/**
034 * {@link GlobalAuthenticationConfigurerAdapter} to trigger early initialization of
035 * {@code @EnableAutoConfiguration} beans. This configuration is imported from
036 * {@link AuthenticationConfiguration} to ensure that users are able to configure the
037 * {@link AuthenticationManagerBuilder} from their {@code @EnableAutoConfiguration} or
038 * {@code @SpringBootApplication} configuration class:
039 *
040 * <pre class="code">
041 * &#064;Autowired
042 * public void configureGlobal(AuthenticationManagerBuilder auth) {
043 *     ...
044 * }
045 * </pre>
046 *
047 * @author Rob Winch
048 * @since 1.1.11
049 */
050@Configuration
051@ConditionalOnClass(GlobalAuthenticationConfigurerAdapter.class)
052public class BootGlobalAuthenticationConfiguration {
053
054        @Bean
055        public static BootGlobalAuthenticationConfigurationAdapter bootGlobalAuthenticationConfigurationAdapter(
056                        ApplicationContext context) {
057                return new BootGlobalAuthenticationConfigurationAdapter(context);
058        }
059
060        private static class BootGlobalAuthenticationConfigurationAdapter
061                        extends GlobalAuthenticationConfigurerAdapter {
062
063                private static final Log logger = LogFactory
064                                .getLog(BootGlobalAuthenticationConfiguration.class);
065
066                private final ApplicationContext context;
067
068                BootGlobalAuthenticationConfigurationAdapter(ApplicationContext context) {
069                        this.context = context;
070                }
071
072                @Override
073                public void init(AuthenticationManagerBuilder auth) {
074                        Map<String, Object> beansWithAnnotation = this.context
075                                        .getBeansWithAnnotation(EnableAutoConfiguration.class);
076                        if (logger.isDebugEnabled()) {
077                                logger.debug("Eagerly initializing " + beansWithAnnotation);
078                        }
079                }
080
081        }
082
083}