0

Im using spring security annotations to secure my Spring MVC application. After adding annotation @EnableWebSecurity I have got stacktrace like this :

StackTrace : http://pastebin.com/szigdd69

My source codes:

WebAppInitializer.class

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer  {

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

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

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

RootConfig.class :

@Configuration
@ComponentScan(basePackages={"com.antek.demoMVC"},
        excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

    @Bean
    public TemplateEngine templateEngine(TemplateResolver templateResolver){
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;

    }

    @Bean
    public TemplateResolver templateResolver(){
        TemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML5);
        return templateResolver;
    }

}

WebConfig.class :

@Configuration
@EnableWebMvc
@ComponentScan("com.antek.demoMVC")
public class WebConfig extends WebMvcConfigurerAdapter{

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        super.configureDefaultServletHandling(configurer);
        configurer.enable();
    }


    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine);
        return viewResolver;        
    }

}

SecurityWebInitializer.class :

public class SecurityWebInitializer extends AbstractSecurityWebApplicationInitializer {
//nothing to do here.
}

WebSecurityConfig.class :

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin().and()
            .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
        .withUser("user").password("1234").roles("USER")
        .and()
        .withUser("Admin").password("admin123").roles("USER","ADMIN");
    }

}

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

</web-app>

I noticed that adding @EnableWebSecurity annotation on WebSecurityConfig.class causes exception. Any idea how to resolve this problem?

maxshuty
  • 9,708
  • 13
  • 64
  • 77
  • 2
    Check your pom.xml. This usually happens when you have different versions of Spring libraries. – Alexey Soshin Dec 02 '16 at 13:18
  • Hint: java.lang.NoClassDefFoundError: org/springframework/beans/factory/SmartInitializingSingleton. As Alexey written, check pom and respective dependencies and versions. – pringi Dec 02 '16 at 13:22
  • Thanks a lot thats right, my spring version and spring-security version was different in pom.xml. Works fine now. – Krzysztof Antczak Dec 02 '16 at 13:30

1 Answers1

0

I faced the same issue, so I update my spring security dependency with its version which was not added before.

<version>2.6.4</version>