2016-07-27 40 views
0

我在自學Spring Security。我目前有兩個java文件和pom文件。由於某種原因,當我在WebSecurityConfigurerAdapter中覆蓋protected void configure(HttpSecurity http)方法時,它會提供AlreadyBuiltException。如果我刪除重寫的方法,它將運行沒有錯誤。我的代碼如下:Spring安全配置:AlreadyBuiltException:這個對象已經建立

IntegrationTest.java

@SpringBootApplication 
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) 
@ComponentScan({"com.socialsignin", "test.com.socialsignin"}) 
public class IntegrationTest { 
    public static void main(String[] args) { 
     ConfigurableApplicationContext context = SpringApplication.run(IntegrationTest.class, args); 
    } 
} 

SecurityConfig.java

package com.socialsignin.config; 

@EnableWebSecurity 
@Component 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

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

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
      .ignoring() 
      .antMatchers("/resources/**"); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     http 
      .formLogin() 
       .loginPage("/signin") 
       .loginProcessingUrl("/signin/authenticate") 
       .failureUrl("/signin?param.error=bad_credentials") 
      .and() 
       .logout() 
       .logoutUrl("/signout") 
        .deleteCookies("JSESSIONID") 
      .and() 
       .authorizeRequests() 
       .antMatchers("/admin/**", "/favicon.ico", "/resources/**", "/auth/**", "/signin/**", "/signup/**", "/disconnect/facebook").permitAll() 
       .antMatchers("/**").authenticated() 
      .and() 
       .rememberMe() 
      .and() 
       .apply(new SpringSocialConfigurer()); 
    } 

} 

日誌

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: SpringSocialConfigurer depends on org.springframework.social.connect.UsersConnectionRepository. No single bean of that type found in application context. 
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060) ~[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:235) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:199) ~[spring-web-4.2.3.RELEASE.jar:4.2.3.RELEASE] 
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:109) ~[tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4689) [tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5329) [tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) [tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407) [tomcat-embed-core-8.0.36.jar:8.0.36] 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397) [tomcat-embed-core-8.0.36.jar:8.0.36] 
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_66] 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_66] 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66] 
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66] 

產生的原因:org.springframework.beans.BeanInstantiationException:無法實例化[javax.servlet.Filter]:工廠方法'springSecurityFilterChain'拋出了exceptio N;嵌套異常是java.lang.IllegalStateException:SpringSocialConfigurer依賴於org.springframework.social.connect.UsersConnectionRepository。在應用程序上下文中找不到該類型的單個bean。 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans。 factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] ... 23通用幀被省略 引起:java.lang。 IllegalStateException:SpringSocialConfigurer依賴於org.springframework.social.connect.UsersConnectionRepository。在應用程序上下文中找不到該類型的單個bean。 at org.springframework.social。org.springframework.social.security.SpringSocialConfigurer.getDependency(SpringSocialConfigurer.java:117)〜[spring-social-security-1.1.4.RELEASE.jar:1.1.4.RELEASE] 。 security.SpringSocialConfigurer.configure(SpringSocialConfigurer.java:71)〜[spring-social-security-1.1.4.RELEASE.jar:1.1.4.RELEASE] at org.springframework.social.security.SpringSocialConfigurer.configure(SpringSocialConfigurer。 java:44)〜[spring-social-security-1.1.4.RELEASE.jar:1.1.4.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.configure(AbstractConfiguredSecurityBuilder.java:383)〜[ spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:329)〜[spring-sec urity-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:41)〜[spring-security-config-4.0。 4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.web.builders.WebSecurity.performBuild(WebSecurity.java:289)〜[spring-security-config-4.0.4。 RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.web.builders.WebSecurity.performBuild(WebSecurity.java:74)〜[spring-security-config-4.0.4.RELEASE。 jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:333)〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4。發佈] at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractS (安全配置)。 java:105)〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93。 CGLIB $ springSecurityFilterChain $ 3()〜[spring-security-config-4.0.4。RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93 $$ FastClassBySpringCGLIB $$ 164abca.invoke()〜[spring-security-config -4.0.4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)〜[spring-core-4.2.7.RELEASE.jar:4.2 .7.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)〜[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org .springframework.security.config.annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93.springSecurityFilterChain()〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at sun。 reflect.NativeMethodAccessorImpl.invoke0(母語方法)〜[NA:1.8.0_66] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[NA:1.8.0_66] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_66] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate([Method.java:497] SimpleInstantiationStrategy.java:162)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] ...刪除了24個常用框架 由org.springframework.beans.factory.NoSuchBeanDefinitionException引發:無資格在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFac)中定義了[org.springframework.social.connect.UsersConnectionRepository]類型的bean tory.java:372)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:332)〜[ spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1066)〜[spring-context-4.2.7.RELEASE。 jar:4.2.7.RELEASE] at org.springframework.social.security.SpringSocialConfigurer.getDependency(SpringSocialConfigurer.java:114)〜[spring-social-security-1.1.4.RELEASE.jar:1.1.4.RELEASE] ...刪除44個常用框架

2016-07-27 12:13:30.582錯誤3869 --- [ost-startStop-1] o.apache.catalina.core.StandardContext:一個或多個過濾器失敗開始。詳細信息將在適當的容器日誌文件中找到 2016-07-27 12:13:30.583錯誤3869 --- [ost-startStop-1] o.apache.catalina.core.StandardContext:上下文[]啓動失敗到以前的錯誤 2016-07-27 12:13:30.627 WARN 3869 --- [main] ationConfigEmbeddedWebApplicationContext:在上下文初始化期間遇到的異常 - 取消刷新嘗試:org.springframework.beans.factory.BeanCreationException:創建名爲'通過工廠方法的Bean實例化失敗;在類路徑資源[org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]中定義了springSecurityFilterChain。嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化[javax.servlet.Filter]:工廠方法'springSecurityFilterChain'拋出異常;嵌套異常是org.springframework.security.config.annotation.AlreadyBuiltException:這個對象已經建好了 2016-07-27 12:13:30.632 INFO 3869 --- [main] o.apache.catalina.core.StandardService:停止服務Tomcat 2016-07-27 12:13:30.638錯誤3869 --- [main] osboot.SpringApplication:應用程序啓動失敗

org.springframework.beans.factory.BeanCreationException:創建名爲'通過工廠方法的Bean實例化失敗;在類路徑資源[org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]中定義了springSecurityFilterChain。嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化[javax.servlet.Filter]:工廠方法'springSecurityFilterChain'拋出異常;嵌套異常是org.springframework.security.config.annotation。AlreadyBuiltException:該對象已經在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]中被構建爲 。 at org.springframework.beans。org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans。 factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean( AbstractAutowireCapableBeanFactory.java:510)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframewor在org.springframework.beans.factory.support上可以使用[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 來創建一個抽象自動化BeanBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)。 AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java :230)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)〜[spring-beans 4.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.2.7.RELEASE.jar :4.2.7.RELEASE] at org.springframework.beans.factory.su pport.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory。 java:197)〜[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)〜[spring- beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)〜[spring-context-4.2.7.RELEASE.jar: 4.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)〜[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org。 springframework.boot.context.embedd ed.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)〜[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:760) [spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE] at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:360)[spring-boot-1.3.6.RELEASE.jar: 1.3.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE] at org.springframework.boot .SpringApplication.run(SpringApplication.java:1185)[spring-boot-1.3.6.RELEASE.jar:1.3.6.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)[spring -boot-1.3.6.RELEASE.jar:1.3.6.RELEASE] at test.com.socialsignin.app .IntegrationTest.main(IntegrationTest.java:15)[test-classes /:na] 原因:org.springframework.beans.BeanInstantiationException:無法實例化[javax.servlet.Filter]:工廠方法'springSecurityFilterChain'拋出異常;嵌套異常是org.springframework.security.config.annotation.AlreadyBuiltException:該對象已經在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)〜[spring-beans-4.2 .7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)〜[spring-beans-4.2.7.RELEASE.jar:4.2 .7.RELEASE] ...省略了20個常見幀 由org.springframework.security.config.annotation引起。AlreadyBuiltException:該對象已經在org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:44)〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4]中被構建爲 。發佈] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:105)〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93.CGLIB $ springSecurityFilterChain $ 3()〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4。 RELEASE] at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93 $$ FastClassBySpringCGLIB $$ 164abca.invoke()〜[spring-security-config-4.0.4.RELEASE。 jar:4.0.4.RELEASE] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)〜[spring-core-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)〜[spring-context-4.2.7.RELEASE.jar:4.2.7.RELEASE] at org.springframework.security.config。 annotation.web.configuration.WebSecurityConfiguration $$ EnhancerBySpringCGLIB $$ 2ba5cd93.springSecurityFilterChain()〜[spring-security-config-4.0.4.RELEASE.jar:4.0.4.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)〜[na:1.8.0_66] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_66] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j ava:43)〜[na:1.8.0_66] at java.lang.reflect.Method.invoke(Method.java:497)〜[na:1.8.0_66] at org.springframework.beans.factory.support。 SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)〜[彈簧豆-4.2.7.RELEASE.jar:4.2.7.RELEASE] ... 21個共同幀省略

+0

可以嘗試爲您的SecurityConfig類使用@Configuration註釋嗎? – Unknown

+0

是的,我應該使用它。只是將其更改爲@Configuration,但仍然得到相同的錯誤:( – user1011376

+0

你有沒有想過這個? –

回答

0

我在此錯誤,以及無任何可以檢測到的變化。也許當我升級到Spring Boot 1.4但不知道。令人沮喪的是,從我的IDE運行並沒有顯示錯誤,它只發生在部署在Heroku上時。我所做的解決方法是將「@Order(0)」添加到內部類中,請參閱下面的代碼示例。設置安全配置非常混亂,所以我不清楚它爲什麼可行,但也許可以幫助其他人嘗試。

同樣對於使用configureGlobal的情況,請查看https://github.com/spring-projects/spring-security/issues/3042Can not apply DaoAuthenticationConfigurer to already built object,因爲它們也可能是相關的。

@Configuration 
@EnableWebSecurity 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class SpringSecurityConfigurer { 

    @Configuration 
    @Order(1) 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

     /** We expect that the URLs secured here are called as part of the application or via websocket. */ 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .requestMatchers().antMatchers("/restapi/requests/**", "/simulation/**", "/api/**", "/wsconnect/**").and() 
        .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
        .httpBasic() 
        .and() 
        .csrf().disable(); 
     } 
    } 

    @Configuration 
    @Order(2) 
    public static class StatelessApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

     /** We expect that the URLs secured here are called called externally. */ 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .requestMatchers().antMatchers("/restapi/**").and() 
        .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
        .httpBasic() 
        .and() 
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
        .and() 
        .csrf().disable(); 
     } 
    } 

    @Configuration 
    @Order(3) 
    public static class SubscriptionWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .requestMatchers().antMatchers("/subscribe**","/subscribe/**").and() 
        .addFilterBefore(new ApplicationSecurityTokenFilter(), UsernamePasswordAuthenticationFilter.class) 
        .authorizeRequests() 
        .anyRequest().authenticated() 
        .and() 
        .httpBasic() 
        .and() 
        .csrf().disable(); 
     } 
    } 

    @Configuration 
    @Order(0) 
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter { 

     @Autowired 
     ApplicationUserDetailsManager applicationUserDetailsManager; 

     @Autowired // need Autowired instead of Override here, not clear why! 
     public void configure(AuthenticationManagerBuilder auth) throws Exception { 
      // configure the repository user details service 
//   PasswordEncoder encoder = new BCryptPasswordEncoder(); 
      auth.userDetailsService(applicationUserDetailsManager); 
      // make sure we have the default user if it is not there 
      if(!applicationUserDetailsManager.userExists("user")) { 
       ApplicationUser defaultApplicationUser = new ApplicationUser(); 
       defaultApplicationUser.setUsername("user"); 
       defaultApplicationUser.setPassword("password"); 
       defaultApplicationUser.setEnabled(true); 
       applicationUserDetailsManager.createUser(new ApplicationUserDetails(defaultApplicationUser)); 
      } 
     } 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      // configure our web security that uses a form login 
      http 
        .authorizeRequests() 
         .anyRequest().authenticated() 
         .and() 
        .formLogin() 
         .loginPage("/login") 
         .permitAll() 
         .and() 
        .logout() 
         .permitAll() 
         // note that this overrides CSRF for logout as it allows a GET to logout 
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
         .and() 
        .rememberMe(); 
     } 

     @Override 
     public void configure(WebSecurity web) throws Exception { 
      web 
        .ignoring() 
        // Spring Security should completely ignore URLs starting with /icons/ and /images/ 
        .antMatchers("/icons/**") 
        .antMatchers("/images/**") 
        .antMatchers("/favicon.ico"); 
     } 
    } 
}