Skip to content

configureGlobal on @SpringBootApplication fails #2473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rwinch opened this issue Feb 10, 2015 · 5 comments
Closed

configureGlobal on @SpringBootApplication fails #2473

rwinch opened this issue Feb 10, 2015 · 5 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@rwinch
Copy link
Member

rwinch commented Feb 10, 2015

The following fails:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;

@SpringBootApplication
public class HelloWebSecurityApplication {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth)
            throws Exception {
        // @formatter:off
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
        // @formatter:on
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloWebSecurityApplication.class, args);
    }
}

We should allow Spring Security's Global Authentication to be configured from any class annotated with @EnableGlobalAuthentication, @EnableAutoConfiguration, or @SpringBootApplication. This can be done by adding the following:

@Configuration
@ConditionalOnClass(GlobalAuthenticationConfigurerAdapter.class)
public class BootGlobalAuthenticationConfiguration {

    @Bean
    public static BootGlobalAuthenticationConfigurationAdapter bootGlobalAuthenticationConfigurationAdapter(ApplicationContext context) {
        return new BootGlobalAuthenticationConfigurationAdapter(context);
    }

    private static class BootGlobalAuthenticationConfigurationAdapter extends GlobalAuthenticationConfigurerAdapter {
        private final ApplicationContext context;
        private static final Log logger = LogFactory.getLog(BootGlobalAuthenticationConfiguration.class);

        public BootGlobalAuthenticationConfigurationAdapter(ApplicationContext context) {
            this.context = context;
        }

        @Override
        public void init(AuthenticationManagerBuilder auth) {
            Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(EnableAutoConfiguration.class);
            if(logger.isDebugEnabled()) {
                logger.debug("Eagerly initializing " + beansWithAnnotation);
            }
        }
    }
}
@rwinch rwinch added the type: bug A general bug label Feb 10, 2015
@rwinch rwinch self-assigned this Feb 10, 2015
@rwinch rwinch added this to the 1.1.11 milestone Feb 10, 2015
rwinch pushed a commit to rwinch/spring-boot that referenced this issue Feb 10, 2015
rwinch pushed a commit to rwinch/spring-boot that referenced this issue Feb 10, 2015
@rwinch
Copy link
Member Author

rwinch commented Feb 10, 2015

See #2475

@rwinch rwinch modified the milestones: 1.2.2, 1.1.11 Feb 18, 2015
@rwinch rwinch closed this as completed in f9816ea Feb 18, 2015
@dsyer
Copy link
Member

dsyer commented Feb 18, 2015

I didn't see the 1.1.11 milestone on this. Is it a change we want/need in 1.1?

@rwinch
Copy link
Member Author

rwinch commented Feb 18, 2015

Yes I view this as a bug (this use to work). Issues like this are also one of the most frequent issues I see when dealing with Spring Security and Boot.

wilkinsona added a commit that referenced this issue Feb 19, 2015
@wilkinsona
Copy link
Member

@rwinch I've backported this to 1.1.x. For future reference, if you have a pull request that you'd like to be included in a maintenance branch, please open the PR against that branch rather than master as we prefer to merge things forwards into master rather than cherry picking them backwards. If you're interested, the rationale for this approach is in the wiki.

@rwinch
Copy link
Member Author

rwinch commented Feb 19, 2015

Thanks for back porting this into 1.1.x @wilkinsona

Also thanks for the information. I will keep that in mind in the future.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants