2015-04-05 173 views
0

这是我的应用程序类。集成Spring Boot与Spring Security

@SpringBootApplication 
@ComponentScan({"org.app.genesis.client.controller","org.app.genesis.commons.service", 
    "org.app.genesis.commons.security","org.app.genesis.inventory.service","org.app.genesis.client.auth"}) 
@EnableJpaRepositories(basePackages = "org.app.genesis.*.repo") 
@EntityScan(basePackages = "org.app.genesis.*.model") 
public class Application extends SpringBootServletInitializer { 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(Application.class, args); 
    } 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

    ..other configs here 

    @Configuration 
    @EnableWebSecurity 
    @ComponentScan({"org.app.genesis.client.auth"}) 
    public class SecurityConfig extends WebSecurityConfigurerAdapter { 

     @Autowired 
     private AuthenticationProvider customAuthProvider; 

     @Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.authenticationProvider(customAuthProvider); 
     } 
    } 
} 

但是每当我构建应用程序。它总是抛出此异常

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:run (default-cli) on project app-client-webapp: An exception occured while running. null: 
InvocationTargetException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void `org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.setFilterChainProxySecurityConfigurer(org.springframework.security.config.annotation.ObjectPostProcessor,java.util.List) throws java.lang.Exception; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.app.genesis.client.Application$SecurityConfig': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.app`enter code here`.genesis.client.Application$SecurityConfig$$EnhancerBySpringCGLIB$$b49171d7]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.app.genesis.client.Application$SecurityConfig$$EnhancerBySpringCGLIB$$b49171d7.<init>() -> [Help 1]` 

编辑:新春安全配置

@Configuration 
@EnableWebSecurity 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
protected static class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    @Qualifier("customAuthenticationProvider") 
    private AuthenticationProvider customAuthProvider; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth){ 
     auth.authenticationProvider(customAuthProvider); 
    } 

    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests().anyRequest().authenticated(); 
     http 
      .formLogin().failureUrl("/login?error") 
      .defaultSuccessUrl("/dashboard") 
      .loginPage("/login") 
      .permitAll() 
      .and() 
      .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login") 
      .permitAll().and().csrf().disable(); 
    } 
} 

回答

1

完整代码丢失,因为这些我不能针点,这是造成这一问题的线,但我会绝对让试图解释它,这样你可以自己修复它

@EnableWebSecurity

的JavaDoc的文件建立:

这个注释添加到@Configuration类,以便在任何WebSecurityConfigurer以上 可能定义的春天 安全配置通过扩展WebSecurityConfigurerAdapter基类和压倒一切的 独立的方法。

好像无论你错过了覆盖WebSecurityConfigurerAdapter基类的“配置”方法或没有正确实施“configureGlobal”的方法,或者你可能要创建一个类继承AbstractSecurityWebApplicationInitializer,它会自动加载springSecurityFilterChain 。

但是,我建议你通过以下来源,你应该能够找出你失踪的是什么。

  1. https://github.com/spring-projects/spring-security-oauth-javaconfig/blob/master/samples/oauth2-sparklr/src/main/java/org/springframework/security/oauth/examples/sparklr/config/SecurityConfiguration.java
  2. http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/
+0

我设法得到它,每当我登录,在不使用我的customAuthenticationProvider然而时运行。任何方式来检查这个问题?另外,我已经更新了我的代码。看到我的更新 – user962206 2015-04-05 05:32:33

+0

据我了解,你似乎只是自动装配自定义AuthenticationProvider,但你没有重写“保护无效配置(AuthenticationManagerBuilder身份验证)”或“公共AuthenticationManager authenticationManagerBean()”,这意味着你没有告诉Spring Security使用您的自定义“AuthenticationProvider”。 – 2015-04-05 05:50:58

+0

我以为我把它传递给http.authenticationProvider(customAuthProvider); ?那是不同的? – user962206 2015-04-05 05:54:37