将Spring启动与Spring Security集成

时间:2015-04-05 03:59:24

标签: java spring spring-mvc spring-security spring-boot

这是我的应用程序类。

@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]`

编辑:新的Spring安全配置

@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 个答案:

答案 0 :(得分:1)

缺少完整的代码,因为我无法确定导致此问题的行,但我一定会尝试解释它,以便您自己修复

<强> @EnableWebSecurity

  

JavaDoc文档:

     

将此注释添加到@Configuration类以获得Spring   任何WebSecurityConfigurer中定义的安全配置   可能通过扩展WebSecurityConfigurerAdapter基类和   压倒个别方法。

似乎你错过了覆盖WebSecurityConfigurerAdapter基类的“configure”方法或者没有正确实现“configureGlobal”方法,或者你可能想要创建一个类extends extendsSecurityWebApplicationInitializer,它会自动加载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/