title: spring security gives the error while redirecting to logout.jsp
body: <p>I have the </p>
  
  <blockquote>
    <p>ERR_TOO_MANY_REDIRECTS</p>
  </blockquote>
  
  <p>exception while redirecting to logout.jsp page. I think I give this exception because of wrong spring security config file but I don`t know where is it. If somebody know, how to fix it, than please tell me.
  What does it mean? My code:</p>
  
  <p>spring security config:</p>
  
  <pre><code>@Configuration
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .csrf().disable()
                  .authorizeRequests()
                      .antMatchers("/**").access("IS_AUTHENTICATED_ANONYMOUSLY")
                      .antMatchers("/**").access("hasRole('ROLE_USER')")
                      .antMatchers("/login").permitAll()
                      .antMatchers("/register").permitAll()
                      .antMatchers("/logout").permitAll()
                      .antMatchers("/js/**", "/css/**").permitAll()
                      .anyRequest().authenticated()
                      .and()
                  .formLogin()
                      .loginPage("/login")
                      .loginProcessingUrl("/j_spring_security_check")
                      .failureForwardUrl("/login?error=true")
                      .permitAll()
                      .and()
                  .logout()
                      .logoutUrl("/logout")
                      .logoutSuccessUrl("/logout")
                      .permitAll();
  
      }
  }
  </code></pre>
  
  <p>logout buttom:</p>
  
  <pre><code>  &lt;div class="navbar navbar-default navbar-fixed-top" role="navigation"&gt;
          &lt;div class="container"&gt;
              &lt;a class="navbar-brand"&gt;Contacts List&lt;/a&gt;
  
              &lt;div class="collapse navbar-collapse"&gt;
                  &lt;form class="navbar-form navbar-right"&gt;
                      &lt;li&gt;
                                  &lt;a class="btn btn-primary" role="button" href="logout"&gt;Logout&lt;/a&gt;
                      &lt;/li&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  and login.jsp:
  &lt;html&gt;
      &lt;jsp:include page="headTag.jsp"/&gt;
  &lt;body&gt;
  &lt;div class="navbar navbar-default navbar-fixed-top" role="navigation"&gt;
      &lt;div class="container"&gt;
          &lt;div class="navbar-collapse collapse"&gt;
              &lt;ul class="nav navbar-nav navbar-right"&gt;
                  &lt;li&gt;
                      &lt;c:url value="/j_spring_security_check" var="loginUrl"/&gt;
                      &lt;form:form class="navbar-form" role="form" action="${loginUrl}"
                           method="post"&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label for="username"&gt; Login: &lt;/label&gt;
                              &lt;div class="col-sm-3"&gt;
                                  &lt;input type="text" placeholder="Login" class="form-control" name='username' id="username"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="form-group"&gt;
                              &lt;label for="password"&gt; Password: &lt;/label&gt;
                              &lt;div class="col-sm-3"&gt;
                                  &lt;input type="password" placeholder="Password" class="form-control" name='password' id="password"&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="form-group"&gt;
                          &lt;button type="submit" class="btn btn-success"&gt;Sign in&lt;/button&gt;
                          &lt;/div&gt;
                      &lt;/form:form&gt;
                      &lt;br/&gt;
                      &lt;form class="navbar-form" action="&lt;c:url value="register.jsp" /&gt;"&gt;
                          &lt;button class="btn btn-sm btn-block btn-primary" role="button"&gt;Register&lt;/button&gt;
                      &lt;/form&gt;
                  &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="jumbotron"&gt;
      &lt;div class="container"&gt;
          &lt;c:if test="${not empty error}"&gt;
              &lt;div class="error"&gt;${error}&lt;/div&gt;
          &lt;/c:if&gt;
          &lt;c:if test="${not empty logout}"&gt;
              &lt;div class="message"&gt;${logout}&lt;/div&gt;
          &lt;/c:if&gt;
  
          &lt;p&gt;
              &lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;
          &lt;p&gt;User login: &lt;b&gt; Bill &lt;/b&gt;&lt;/p&gt;
          &lt;p&gt;User password: &lt;b&gt; 112233 &lt;/b&gt;&lt;/p&gt;
  
          &lt;p&gt;Стек технологий: &lt;a href="http://projects.spring.io/spring-security/"&gt;Spring Security&lt;/a&gt;,
              &lt;a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html"&gt;Spring MVC&lt;/a&gt;,
              &lt;a href="http://projects.spring.io/spring-data-jpa/"&gt;Spring Data JPA&lt;/a&gt;,
              &lt;a href="http://spring.io/blog/2014/05/07/preview-spring-security-test-method-security"&gt;Spring Security
                  Test&lt;/a&gt;,
              &lt;a href="http://hibernate.org/orm/"&gt;Hibernate ORM&lt;/a&gt;,
              &lt;a href="http://hibernate.org/validator/"&gt;Hibernate Validator&lt;/a&gt;,
              &lt;a href="http://www.slf4j.org/"&gt;SLF4J&lt;/a&gt;,
              &lt;a href="https://github.com/FasterXML/jackson"&gt;Json Jackson&lt;/a&gt;,
              &lt;a href="http://ru.wikipedia.org/wiki/JSP"&gt;JSP&lt;/a&gt;,
              &lt;a href="http://en.wikipedia.org/wiki/JavaServer_Pages_Standard_Tag_Library"&gt;JSTL&lt;/a&gt;,
              &lt;a href="http://tomcat.apache.org/"&gt;Apache Tomcat&lt;/a&gt;,
              &lt;a href="http://www.webjars.org/"&gt;WebJars&lt;/a&gt;,
              &lt;a href="http://datatables.net/"&gt;DataTables plugin&lt;/a&gt;,
              &lt;a href="http://ehcache.org"&gt;Ehcache&lt;/a&gt;,
              &lt;a href="http://www.postgresql.org/"&gt;PostgreSQL&lt;/a&gt;,
              &lt;a href="http://junit.org/"&gt;JUnit&lt;/a&gt;,
              &lt;a href="http://hamcrest.org/JavaHamcrest/"&gt;Hamcrest&lt;/a&gt;,
              &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;,
              &lt;a href="http://ned.im/noty/"&gt;jQuery notification&lt;/a&gt;,
              &lt;a href="http://getbootstrap.com/"&gt;Bootstrap&lt;/a&gt;.&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;jsp:include page="footer.jsp"/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@Controller
  public class RootController extends AbstractUserController implements ErrorController {
  
      private static final String PATH = "/error";
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String root() {
          return "redirect:/contacts";
      }
  
      @RequestMapping(value = "/contacts", method = RequestMethod.GET)
      public String contactList() {
          return "contacts";
      }
  
      @RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
      @PreAuthorize("hasRole('ROLE_USER')")
      public String login(Model model, @RequestParam(value = "error", required = false) boolean error){
          model.addAttribute("error", error);
          return "login";
      }
  
      @RequestMapping(value="/logout", method = {RequestMethod.GET, RequestMethod.POST})
      public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
          if (auth != null){
              new SecurityContextLogoutHandler().logout(request, response, auth);
          }
          return "logout";
      }
  
      @RequestMapping(value = "/register", method = RequestMethod.GET)
      public String register(ModelMap model) {
          model.addAttribute("userDTO", new UserDTO());
          model.addAttribute("register", true);
          return "register";
      }
  
      @RequestMapping(value = PATH)
      public String error(){
          return "redirect:/login";
      }
  
      @RequestMapping(value = "/register", method = RequestMethod.POST)
      public String saveRegister(@Valid UserDTO userDTO, BindingResult result, SessionStatus status, ModelMap model) {
          if (!result.hasErrors()) {
              try {
                  super.create(UserUtil.createNewUserFromDTO(userDTO));
                  status.setComplete();
                  return "redirect:login?message=app.registered";
              } catch (DataIntegrityViolationException ex) {
                  result.rejectValue("Login", "---");
              }
          }
          model.addAttribute("register", true);
          return "contacts";
      }
  
      @Override
      public String getErrorPath() {
          return PATH;
      }
  }
  </code></pre>
  
  <p>Thanks guys.</p>