title: Spring security error Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup
body: <p>I am getting following error while running spring boot security app with gradle -</p>
  
  <p>javax.servlet.ServletException: Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
      at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) [tomcat-embed-core-9.0.29.jar:9.0.29]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) [spring-webmvc-5.2.2.RELEASE.jar:5.2.2.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) [tomcat-embed-core-9.0.29.jar:9.0.29]</p>
  
  <p>I have added following code in application -</p>
  
  <p>src/main/resources/templates/index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"&gt;
      &lt;head&gt;
          &lt;title&gt;Spring Security Example&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Welcome!&lt;/h1&gt;
  
          &lt;p&gt;Click &lt;a th:href="@{/main}"&gt;here&lt;/a&gt; to see a greeting.&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  
  <p>src/main/resources/templates/main.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
        xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"&gt;
      &lt;head&gt;
          &lt;title&gt;Hello World!&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Hello world!&lt;/h1&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>src/main/resources/templates/mylogin.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org"
        xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity3"&gt;
      &lt;head&gt;
          &lt;title&gt;Spring Security Example &lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div th:if="${param.error}"&gt;
              Invalid username and password.
          &lt;/div&gt;
          &lt;div th:if="${param.logout}"&gt;
              You have been logged out.
          &lt;/div&gt;
          &lt;form th:action="@{/mylogin}" method="post"&gt;
              &lt;div&gt;&lt;label&gt; User Name : &lt;input type="text" name="username"/&gt; &lt;/label&gt;&lt;/div&gt;
              &lt;div&gt;&lt;label&gt; Password: &lt;input type="password" name="password"/&gt; &lt;/label&gt;&lt;/div&gt;
              &lt;div&gt;&lt;input type="submit" value="Sign In"/&gt;&lt;/div&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>src/main/com/example/security/MVCConfig.java</p>
  
  <pre><code>package com.example.security;
  
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  
  @Configuration
  public class MVCConfig implements WebMvcConfigurer {
  
    public void addViewControllers(ViewControllerRegistry registry) {
      registry.addViewController("/index").setViewName("index");
      registry.addViewController("/").setViewName("index");
      registry.addViewController("/main").setViewName("main");
      registry.addViewController("/mylogin").setViewName("mylogin");
    }
  }
  
  </code></pre>
  
  <p>src/main/com/example/security/WebSecurityConfig.java</p>
  
  <pre><code>
  package com.example.security;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  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.core.userdetails.User;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  
  @Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
          .antMatchers("/", "/main").permitAll()
          .anyRequest().authenticated()
          .and()
        .formLogin()
          .loginPage("/mylogin")
          .permitAll()
          .and()
        .logout()
          .permitAll();
    }
  
    @Bean
    @Override
    public UserDetailsService userDetailsService() {
      UserDetails user =
         User.withDefaultPasswordEncoder()
          .username("user")
          .password("password")
          .roles("USER")
          .build();
  
      return new InMemoryUserDetailsManager(user);
    }
  }
  </code></pre>
  
  <p>src/main/com/example/security/SecurityApplication.java</p>
  
  <pre><code>
  package com.example.security;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  public class SecurityApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(SecurityApplication.class, args);
      }
  
  }
  </code></pre>
  
  <p>build.gradle</p>
  
  <pre><code>plugins {
      id 'org.springframework.boot' version '2.2.2.RELEASE'
      id 'io.spring.dependency-management' version '1.0.8.RELEASE'
      id 'java'
  }
  
  group = 'com.example'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '1.8'
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      implementation 'org.springframework.boot:spring-boot-starter-security'
      implementation 'org.springframework.boot:spring-boot-starter-web'   
      testImplementation('org.springframework.boot:spring-boot-starter-test') {
          exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
      }
      testImplementation 'org.springframework.security:spring-security-test'
  }
  
  test {
      useJUnitPlatform()
  }
  </code></pre>