1

While implementing spring security with @EnableWebSecurity annotation and extending class "WebSecurityConfigurerAdapter" getting error.

I'm getting an error like

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager'

and the nested exception is:

Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError

Have used jetty server to run code.

Want to achieve basic authentication with spring security.

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.accept.ContentNegotiationManager]: Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError
Feb 11, 2019 8:41:52 PM org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.accept.ContentNegotiationManager]: Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at o

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.accept.ContentNegotiationManager]: Factory method 'mvcContentNegotiationManager' threw exception; nested exception is java.lang.AbstractMethodError

SpringWebConfigSecurity .java

    @Import({SecurityConfig.class, SpringWebConfig.class/*,and other config 
    classes*/})
    @Configuration
    @EnableWebSecurity
    @ComponentScan({ "com.uxpsystems.assignment.controller.*" })
    public class SpringWebConfigSecurity extends 
    WebSecurityConfigurerAdapter{

    protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests()
              .anyRequest().authenticated()
              .and()
              .formLogin()
              .and()
              .exceptionHandling().accessDeniedPage("/user");
      }

    @Override
    public void configure(AuthenticationManagerBuilder builder) throws 
    Exception {
          builder.inMemoryAuthentication()
                 .withUser("user1").password("password").roles("CUSTOMER")
                 .and()
                 .withUser("user2").password("password").roles("ADMIN");
      }

POM.xml

           <properties>
        <org.springframework-version>4.3.14.RELEASE</org.springframework- 
        version>
        <hibernate.version>4.3.5.Final</hibernate.version>
        <servletapi.version>3.1.0</servletapi.version>
        <spring.version>5.1.4.RELEASE</spring.version>
        <spring-security.version>5.0.4.RELEASE</spring-security.version>
        <hsqldb.version>2.3.2</hsqldb.version>
        <jstl.version>1.2</jstl.version>
     </properties>
     <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- Spring ORM -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <!-- HyperSQL DB -->
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>${hsqldb.version}</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servletapi.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>

        <!-- <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency> -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${org.springframework-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.1.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${spring-security.version}</version>
        </dependency>
    </dependencies>

SpringWebConfig .java

    @EnableWebMvc
    @Configuration
    @ComponentScan({ "com.uxpsystems.assignment" })
    public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {       
    registry.addResourceHandler("/resources/**").
    addResourceLocations("/resources/");
    }
    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new 
    InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
      }

WEB.xml

        <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>      
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

Gives me error while I have created class SpringWebConfigSecurity.java and add @EnableWebSecurity annotation

Nanhydrin
  • 4,332
  • 2
  • 38
  • 51
Avani Patel
  • 11
  • 1
  • 2
  • Hi. I've edited your question a bit to make the various files stand out from each other. However I think you've probably included too much code that's not actually relevant in your question. We ask that you try to give us just the minimal example of code that's causing the problem, look [here for guidance](https://stackoverflow.com/help/mcve). If you look at the end of the error that you're getting you'll see it's complaining about is actually an AbstractMethodError. – Nanhydrin Feb 12 '19 at 08:38
  • Possible duplicate of [Abstract Method Error](https://stackoverflow.com/questions/12137073/abstract-method-error) – Nanhydrin Feb 12 '19 at 08:39

0 Answers0