{"action":"created","issue":{"url":"https://api.github.com/repos/spring-projects/spring-security/issues/3825","repository_url":"https://api.github.com/repos/spring-projects/spring-security","labels_url":"https://api.github.com/repos/spring-projects/spring-security/issues/3825/labels{/name}","comments_url":"https://api.github.com/repos/spring-projects/spring-security/issues/3825/comments","events_url":"https://api.github.com/repos/spring-projects/spring-security/issues/3825/events","html_url":"https://github.com/spring-projects/spring-security/issues/3825","id":149281036,"number":3825,"title":"When enabling global method security, AuthenticationManager gets built twice, but only in test environment","user":{"login":"whittle","id":21054,"avatar_url":"https://avatars.githubusercontent.com/u/21054?v=3","gravatar_id":"","url":"https://api.github.com/users/whittle","html_url":"https://github.com/whittle","followers_url":"https://api.github.com/users/whittle/followers","following_url":"https://api.github.com/users/whittle/following{/other_user}","gists_url":"https://api.github.com/users/whittle/gists{/gist_id}","starred_url":"https://api.github.com/users/whittle/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/whittle/subscriptions","organizations_url":"https://api.github.com/users/whittle/orgs","repos_url":"https://api.github.com/users/whittle/repos","events_url":"https://api.github.com/users/whittle/events{/privacy}","received_events_url":"https://api.github.com/users/whittle/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/spring-projects/spring-security/labels/waiting-for-feedback","name":"waiting-for-feedback","color":"eb6420"}],"state":"open","locked":false,"assignee":{"login":"jgrandja","id":10884212,"avatar_url":"https://avatars.githubusercontent.com/u/10884212?v=3","gravatar_id":"","url":"https://api.github.com/users/jgrandja","html_url":"https://github.com/jgrandja","followers_url":"https://api.github.com/users/jgrandja/followers","following_url":"https://api.github.com/users/jgrandja/following{/other_user}","gists_url":"https://api.github.com/users/jgrandja/gists{/gist_id}","starred_url":"https://api.github.com/users/jgrandja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jgrandja/subscriptions","organizations_url":"https://api.github.com/users/jgrandja/orgs","repos_url":"https://api.github.com/users/jgrandja/repos","events_url":"https://api.github.com/users/jgrandja/events{/privacy}","received_events_url":"https://api.github.com/users/jgrandja/received_events","type":"User","site_admin":false},"milestone":null,"comments":4,"created_at":"2016-04-18T21:45:40Z","updated_at":"2016-06-06T22:01:23Z","closed_at":null,"body":"### Summary\r\n\r\nWhen I enable global method security on an app that already subclasses `WebSecurityConfigurerAdapter`, the application context fails to load in my test environment, with the message that the `AuthenticationManager` was already built. \r\n\r\n### Actual Behavior\r\n\r\nI have a vacuous test class that is just intended to check if the application context is loading properly: \r\n\r\n```java\r\n@RunWith(SpringJUnit4ClassRunner.class)\r\n@SpringApplicationConfiguration(classes=ShibaApplication.class)\r\n@WebAppConfiguration\r\npublic class ShibaApplicationTests {\r\n    @Test\r\n    public void contextLoads() {}\r\n}\r\n```\r\n\r\n(I have other test classes of course, and they fail with the same error.) \r\n\r\nWhen I add the annotation `@EnableGlobalMethodSecurity(prePostEnabled=true)` to an existing (working) subclass of `WebSecurityConfigurerAdapter` and add a `@PreAuthorize` annotation to any of my controller classes, my test classes fail (including the vacuous one above). The stack trace is enormous, but the outermost thread gives the following cause (linebreaks added): \r\n\r\n```\r\nCaused by:\r\norg.springframework.beans.factory.BeanCreationException:\r\nError creating bean with name 'channelMarksController' defined in file [/Users/jason/Code/shiba/build/classes/main/co/masslab/shiba/transfer/ChannelMarksController.class]:\r\nInitialization of bean failed;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nError creating bean with name 'methodSecurityInterceptor' defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]:\r\nBean instantiation via factory method failed;\r\n\r\nnested exception is org.springframework.beans.BeanInstantiationException:\r\nFailed to instantiate [org.aopalliance.intercept.MethodInterceptor]:\r\nFactory method 'methodSecurityInterceptor' threw exception;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nError creating bean with name 'webSecurityConfig':\r\nInjection of autowired dependencies failed;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nCould not autowire field:\r\nprivate com.auth0.spring.security.auth0.Auth0AuthenticationFilter co.masslab.shiba.WebSecurityConfig.auth0Filter;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nError creating bean with name 'auth0Filter':\r\nInjection of autowired dependencies failed;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nCould not autowire field:\r\nprivate org.springframework.security.authentication.AuthenticationManager com.auth0.spring.security.auth0.Auth0AuthenticationFilter.authenticationManager;\r\n\r\nnested exception is org.springframework.beans.factory.BeanCreationException:\r\nError creating bean with name 'authenticationManager' defined in class path resource [org/springframework/boot/autoconfigure/security/AuthenticationManagerConfiguration.class]:\r\nBean instantiation via factory method failed;\r\n\r\nnested exception is org.springframework.beans.BeanInstantiationException:\r\nFailed to instantiate [org.springframework.security.authentication.AuthenticationManager]:\r\nFactory method 'authenticationManager' threw exception;\r\n\r\nnested exception is org.springframework.security.config.annotation.AlreadyBuiltException:\r\nThis object has already been built\r\n```\r\n\r\nInterestingly, outside of the test environment, the app runs without error and the `@PreAuthorize` annotation even works as expected. \r\n\r\n### Expected Behavior\r\n\r\nI expect the application context to load successfully within the test environment. \r\n\r\n### Configuration\r\n\r\n```java\r\n@EnableWebSecurity\r\n@EnableGlobalMethodSecurity(prePostEnabled=true)\r\npublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {\r\n\r\n    @Autowired\r\n    private Auth0AuthenticationEntryPoint auth0EntryPoint;\r\n\r\n    @Autowired\r\n    private Auth0AuthenticationFilter auth0Filter;\r\n\r\n    @Autowired\r\n    private Auth0AuthenticationProvider auth0AuthenticationProvider;\r\n\r\n    @Override\r\n    protected void configure(HttpSecurity http) throws Exception {\r\n        http\r\n            .authorizeRequests().anyRequest().hasRole(\"USER\")\r\n            .and()\r\n            .exceptionHandling().authenticationEntryPoint(auth0EntryPoint)\r\n            .and()\r\n            .addFilterAfter(auth0Filter, SecurityContextPersistenceFilter.class)\r\n            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)\r\n            .and()\r\n            .csrf().disable();\r\n    }\r\n\r\n    @Autowired\r\n    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {\r\n        auth.authenticationProvider(auth0AuthenticationProvider);\r\n    }\r\n}\r\n```\r\n\r\n### Version\r\n\r\nThe app uses Spring Boot v1.3.3 BOMs. The observed issue is also present using Spring Boot v1.3.2 BOMs. \r\n\r\n### Sample\r\n\r\nThe project in question is private, but I have privileges sufficient to add individuals as contributors. Please contact me directly @whittle for access. "},"comment":{"url":"https://api.github.com/repos/spring-projects/spring-security/issues/comments/224102284","html_url":"https://github.com/spring-projects/spring-security/issues/3825#issuecomment-224102284","issue_url":"https://api.github.com/repos/spring-projects/spring-security/issues/3825","id":224102284,"user":{"login":"rwinch","id":362503,"avatar_url":"https://avatars.githubusercontent.com/u/362503?v=3","gravatar_id":"","url":"https://api.github.com/users/rwinch","html_url":"https://github.com/rwinch","followers_url":"https://api.github.com/users/rwinch/followers","following_url":"https://api.github.com/users/rwinch/following{/other_user}","gists_url":"https://api.github.com/users/rwinch/gists{/gist_id}","starred_url":"https://api.github.com/users/rwinch/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwinch/subscriptions","organizations_url":"https://api.github.com/users/rwinch/orgs","repos_url":"https://api.github.com/users/rwinch/repos","events_url":"https://api.github.com/users/rwinch/events{/privacy}","received_events_url":"https://api.github.com/users/rwinch/received_events","type":"User","site_admin":false},"created_at":"2016-06-06T22:01:22Z","updated_at":"2016-06-06T22:01:22Z","body":"@davidkey  Thanks for the reply! I created #3916 to address your issue since it is slightly different. "}}
{"actor":{"display_login":"rwinch"}}