title: Spring MVC and Spring Security Integration
body: <p>I am trying to spring security into my project. I have found a sample from internet but I could not achieve correct integration.</p>
  <p>Below my dependencies,</p>
  <pre><code>    &lt;properties&gt;
          &lt;java-version&gt;1.8&lt;/java-version&gt;
          &lt;org.springframework-version&gt;5.2.9.RELEASE&lt;/org.springframework-version&gt;
          &lt;org.hibernate-validator-version&gt;5.4.1.Final&lt;/org.hibernate-validator-version&gt;
          &lt;org.hibernate-core-version&gt;5.3.7.Final&lt;/org.hibernate-core-version&gt;
          &lt;c3p0-version&gt;0.9.5.2&lt;/c3p0-version&gt;
          &lt;mysql-version&gt;5.1.47&lt;/mysql-version&gt;
          &lt;spring.version&gt;5.4.1&lt;/spring.version&gt;
          &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Spring MVC --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Spring ORM --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${org.springframework-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Hibernate Validator --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;${org.hibernate-validator-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Hibernate Core Libs --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;${org.hibernate-core-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- C3P0 Libs --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.mchange&lt;/groupId&gt;
              &lt;artifactId&gt;c3p0&lt;/artifactId&gt;
              &lt;version&gt;${c3p0-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- MySQL JDBC Lib --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;${mysql-version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- JSTL Libs --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;4.0.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Tag Libs --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;taglibs&lt;/groupId&gt;
              &lt;artifactId&gt;standard&lt;/artifactId&gt;
              &lt;version&gt;1.1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- JSP Libs --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;jstl&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Spring Security Core --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
           
          &lt;!-- Spring Security Config --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
           
          &lt;!-- Spring Security Web --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;2.4.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --&gt;
          &lt;dependency&gt;
             &lt;groupId&gt;commons-beanutils&lt;/groupId&gt;
             &lt;artifactId&gt;commons-beanutils&lt;/artifactId&gt;
              &lt;version&gt;1.9.3&lt;/version&gt;
         &lt;/dependency&gt;                
         &lt;!-- https://mvnrepository.com/artifact/javax.validation/validation-api --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.validation&lt;/groupId&gt;
              &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
              &lt;version&gt;2.0.1.Final&lt;/version&gt;
          &lt;/dependency&gt;  
      &lt;/dependencies&gt;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/aXydB.png" rel="nofollow noreferrer">Project Structure</a></p>
  <p>My AppContext,</p>
  <pre><code>import java.util.Properties;
  
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.core.env.Environment;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.hibernate5.HibernateTransactionManager;
  import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  @Configuration
  @PropertySource(&quot;classpath:database.properties&quot;)
  @EnableTransactionManagement
  public class AppContextX {
  
      @Autowired
      private Environment environmentX;
      
      @Bean
      public LocalSessionFactoryBean XSessionFactory() {
          LocalSessionFactoryBean sessionFactoryX = new LocalSessionFactoryBean();
          sessionFactoryX.setDataSource(dataSource());
          sessionFactoryX.setPackagesToScan(new String[] {&quot;com.mutfakapp.xmutfak.entity&quot;});
          sessionFactoryX.setHibernateProperties(XHibernateProperties());
          return sessionFactoryX;
      }
      
      
      private Properties XHibernateProperties() {
          Properties propertiesX = new Properties();
          propertiesX.put(&quot;hibernate.dialect&quot;, environmentX.getRequiredProperty(&quot;hibernate.dialect&quot;));
          propertiesX.put(&quot;hibernate.show_sql&quot;,environmentX.getRequiredProperty(&quot;hibernate.show_sql&quot;));
          propertiesX.put(&quot;hibernate.format_sql&quot;, environmentX.getRequiredProperty(&quot;hibernate.format_sql&quot;));
          propertiesX.put(&quot;hibernate.hbm2ddl.auto&quot;, environmentX.getRequiredProperty(&quot;hibernate.hbm2ddl&quot;));
          return propertiesX;
      }
      
      private DataSource dataSource() {
          DriverManagerDataSource dataSourceX = new DriverManagerDataSource();
          dataSourceX.setDriverClassName(environmentX.getRequiredProperty(&quot;jdbc.driverClassName&quot;));
          dataSourceX.setUrl(environmentX.getRequiredProperty(&quot;jdbc.url&quot;));
          dataSourceX.setUsername(environmentX.getRequiredProperty(&quot;jdbc.username&quot;));
          dataSourceX.setPassword(environmentX.getRequiredProperty(&quot;jdbc.password&quot;));
          return dataSourceX;
      }
      
      @Bean
      public HibernateTransactionManager getTransactionManager() {
          HibernateTransactionManager transactionManagerX = new HibernateTransactionManager();
          transactionManagerX.setSessionFactory(XSessionFactory().getObject());
          return transactionManagerX;
      }
  }
  </code></pre>
  <p>Database properties,</p>
  <pre><code>#JDBC
  jdbc.driverClassName=com.mysql.jdbc.Driver
  jdbc.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
  jdbc.username=root
  jdbc.password=123
  
  #Hibernate
  hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  hibernate.show_sql=true
  hibernate.format_sql=true
  hibernate.hbm2ddl=update
  </code></pre>
  <p>My App Initializer,</p>
  <pre><code>package com.mutfakapp.xmutfak.config;
  
  import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  
  public class AppInitializerX extends AbstractAnnotationConfigDispatcherServletInitializer
  {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
  
          return new Class[] {AppContextX.class};
          
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
  
          return new Class[] {WebMvcConfigX.class};
          
      }
  
      @Override
      protected String[] getServletMappings() {
  
          return new String[] { &quot;/&quot; };
      }
  
  }
  </code></pre>
  <p>WebMvcConfig as below,</p>
  <pre><code>package com.mutfakapp.xmutfak.config;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.Import;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  import org.springframework.web.servlet.view.JstlView;
  
  @Configuration
  @EnableWebSecurity
  @EnableWebMvc
  @ComponentScan(basePackages = {&quot;com.mutfakapp.xmutfak&quot;})
  @Import({ WebSecurityConfig.class })
  public class WebMvcConfigX implements WebMvcConfigurer {
  
      @Bean
      public InternalResourceViewResolver XResolver() {
          InternalResourceViewResolver resolverX = new InternalResourceViewResolver();
          resolverX.setViewClass(JstlView.class);
          resolverX.setPrefix(&quot;/WEB-INF/views/&quot;);
          resolverX.setSuffix(&quot;.jsp&quot;);
          return resolverX;
      }
      
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;/resources/&quot;);
      }
      
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  
  }
  </code></pre>
  <p>And Web Security Configuration</p>
  <pre><code>package com.mutfakapp.xmutfak.config;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  
  import com.mutfakapp.xmutfak.service.UserService;
  
  @EnableWebSecurity
  @Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private UserService userService;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
              .antMatchers(
                  &quot;/&quot;,
                  &quot;/home&quot;,
                  &quot;/lunch&quot;,
                  &quot;/dinner&quot;,
                  &quot;/registration&quot;).permitAll()
              .anyRequest().authenticated()
              .and()
              .formLogin().defaultSuccessUrl(&quot;/addreceipt&quot;)
              .loginPage(&quot;/login&quot;)
              .permitAll()
              .and()
              .logout()
              .invalidateHttpSession(true)
              .clearAuthentication(true)
              .logoutRequestMatcher(new AntPathRequestMatcher(&quot;/logout&quot;))
              .logoutSuccessUrl(&quot;/login?logout&quot;)
              .permitAll();
      }
  
      @Bean
      public BCryptPasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public DaoAuthenticationProvider authenticationProvider() {
          DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
          auth.setUserDetailsService(userService);
          auth.setPasswordEncoder(passwordEncoder());
          return auth;
      }
  }
  </code></pre>
  <p>When I run the project I am getting following error. I could not figure out.</p>
  <pre><code>SEVERE: Exception starting filter [springSecurityFilterChain]
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' available
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:816)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1288)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:207)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1115)
      at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:338)
      at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:243)
      at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:239)
      at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:270)
      at org.apache.catalina.core.ApplicationFilterConfig.&lt;init&gt;(ApplicationFilterConfig.java:106)
      at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4566)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5203)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
      at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
      at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:843)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
      at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
      at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:930)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:772)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:342)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:473)
  </code></pre>