title: Spring Boot/Spring Security 405 Error: dispatch for POST "/error", parameters={masked}
body: <p>I'm doing my first Spring Security project and I'm just trying to make a user form, user login and then have that form push to a MySQL database. I can't seem to get past this error for a RequestMappingHandlerMapping issue, and I'm not sure what I'm doing wrong. I'm not sure if it's something in Spring Security that is blocking the post request back or if I've mislabeled the controllers. I'd appreciate a fresh set of eyes. Thanks</p>
  <p>Error log:</p>
  <pre><code>2021-08-30 21:56:52.580 DEBUG 18992 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED
  2021-08-30 21:56:52.582 DEBUG 18992 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : &quot;ERROR&quot; dispatch for POST &quot;/error&quot;, parameters={masked}
  2021-08-30 21:56:52.584 DEBUG 18992 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
  2021-08-30 21:56:52.602 DEBUG 18992 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Exiting from &quot;ERROR&quot; dispatch, status 405
  </code></pre>
  <p><strong>User Controller</strong></p>
  <pre><code>@Controller
  public class UserController {
  
      @Autowired
      private UserRepository userRepository;
      @Autowired
      private SecurityUserDetailsService userDetailsManager;
      @Autowired
      private PasswordEncoder passwordEncoder;
  
      @GetMapping(&quot;/registration&quot;)
      public String displayRegistration(Model model) {
          model.addAttribute(new User());
          return &quot;registration&quot;;
      }
  
  
      @RequestMapping(
              value = &quot;/registration&quot;,
              consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
              produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }
              )
      public void addUser(@RequestParam Map&lt;String, String&gt; body) {
          User user = new User();
          user.setUsername(body.get(&quot;username&quot;));
          user.setPassword(passwordEncoder.encode(body.get(&quot;password&quot;)));
          user.setAccountNonLocked(true);
          userDetailsManager.createUser(user);
      }
  
      private String getErrorMessage(HttpServletRequest request, String key) {
          Exception exception = (Exception) request.getSession().getAttribute(key);
          String error = &quot;&quot;;
          if (exception instanceof BadCredentialsException) {
              error = &quot;Invalid username and password!&quot;;
          } else if (exception instanceof LockedException) {
              error = exception.getMessage();
          } else {
              error = &quot;Invalid username and password!&quot;;
          }
          return error;
      }
  
  </code></pre>
  <p><strong>Registration View</strong></p>
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;h1 class=&quot;my-5&quot;&gt;Create Account&lt;/h1&gt;
  &lt;body&gt;
  &lt;form action=&quot;/registration&quot; method=&quot;post&quot;&gt;
      &lt;div class=&quot;container&quot;&gt;
          &lt;h1&gt;Register&lt;/h1&gt;
          &lt;p&gt;Please fill in this form to create an account.&lt;/p&gt;
          &lt;hr&gt;
  
          &lt;label for=&quot;username&quot;&gt;&lt;b&gt;Username&lt;/b&gt;&lt;/label&gt;
          &lt;input type=&quot;text&quot; placeholder=&quot;Enter Username&quot; name=&quot;username&quot; id=&quot;username&quot; required&gt;
  
          &lt;label for=&quot;password&quot;&gt;&lt;b&gt;Password&lt;/b&gt;&lt;/label&gt;
          &lt;input type=&quot;password&quot; placeholder=&quot;Enter Password&quot; name=&quot;password&quot; id=&quot;password&quot; required&gt;
          &lt;button type=&quot;submit&quot; class=&quot;registerbtn&quot;&gt;Register&lt;/button&gt;
      &lt;/div&gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p><strong>SecurityUserDetailsService Controller</strong></p>
  <pre><code>
  @Service
  public class SecurityUserDetailsService implements UserDetailsService {
      @Autowired
      private UserRepository userRepository;
  
      @Override
      public UserDetails loadUserByUsername(String username)
              throws UsernameNotFoundException {
          User user = userRepository.findUserByUsername(username)
                  .orElseThrow(() -&gt; new UsernameNotFoundException(&quot;User not present&quot;));
          return user;
      }
  
      public void createUser(UserDetails user) {
          userRepository.save((User) user);
      }
  }
  </code></pre>
  <p><strong>WebSecurityConfig Controller</strong></p>
  <pre><code>@Configuration
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
                  .csrf().disable()
                  .authorizeRequests()
                  .antMatchers(&quot;/&quot;, &quot;/index&quot;,&quot;*/registration/*&quot;).permitAll() //for non-secured pages add to permitAll()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .loginPage(&quot;/login&quot;)
                  .permitAll()
                  .and()
                  .logout()
                  .permitAll();
      }
  </code></pre>