没有定义名为“ springSecurityFilterChain”的bean Java配置

时间:2019-03-05 12:49:42

标签: java spring spring-mvc spring-security

我有应该自行初始化过滤器链的类

public class SecurityWebApplicationInitializer
        extends AbstractSecurityWebApplicationInitializer {

}

但仍然出现noSuchBeanDefinException。

我的webSecurityConfig

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
    }

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

        http.authorizeRequests().antMatchers("/admin/**")
                .access("hasRole('ADMIN')").and().formLogin()
                .loginPage("/login").failureUrl("/login?error")
                .usernameParameter("username")
                .passwordParameter("password")
                .and().logout().logoutSuccessUrl("/login?logout")
                .and().csrf()
                .and().exceptionHandling().accessDeniedPage("/403");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        return encoder;
    }
}

还有我的AppInitialiser

public class WebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {


    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { HibernateConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

我尝试在AppInit中使用WebSecurityConfig.class,但随后显示一个错误,当AppInit类工作时,我在SecurityConfig中自动装配的服务类未初始化。 每个在线示例也都像我一样。他们只是使用SecurityWebApplicationInitializer。

0 个答案:

没有答案