title: Error 403 when I submit the register form in spring boot project
body: <p>I am learning spring boot and write a register form, but when I run it in idea and submit the form, the browser occurs </p>
  
  <blockquote>
    <p>There was an unexpected error (type=Forbidden, status=403).
    Forbidden</p>
  </blockquote>
  
  <p>I create the project by using spring initializr in idea, choose web+jpa+h2+thymeleaf.
  I defined an Entity called Worker and set error messages in ValidationMessages.properties, here is the Worker entity</p>
  
  <pre><code>@Entity
  public class Worker implements UserDetails {
  
      private static final long serialversionUID = 1L;
  
      @Id
      @NotNull
      @Size(min = 5, max = 16, message = "{username.size}")
      private String username;
      @NotNull
      @Size(min = 2, max = 30, message = "{firstName.size}")
      private String firstname;
      @NotNull
      @Size(min = 2, max = 30, message = "{lastName.size")
      private String lastname;
      @NotNull
      @Size(min = 5, max = 25,message = "{password.size}")
      private String password;
      @NotNull
      @Size(min = 2, max = 30, message = "{profession,size}")
      private String profession;
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getFirstname() {
          return firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getProfession() {
          return profession;
      }
  
      public void setProfession(String profession) {
          this.profession = profession;
      }
  
      //UserDetails methods
  
  
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          return Arrays.asList(new SimpleGrantedAuthority("WORKER"));
      }
  
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
  
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
  
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
  
      @Override
      public boolean isEnabled() {
          return true;
      }
  }
  </code></pre>
  
  <p>and WorkersRepository</p>
  
  <pre><code>public interface WorkersRepository extends JpaRepository&lt;Worker, String&gt; {
  Worker findByUsername(String username);
  </code></pre>
  
  <p>}</p>
  
  <p>I have added spring security, and wrote the config:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private WorkersRepository workersRepository;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
              .antMatchers("/submit").access("hasRole('WORKER')")
              .anyRequest().permitAll()
              .and()
              .formLogin()
              .loginPage("/login")
              .and()
              .logout()
              .logoutSuccessUrl("/")
              .and()
              .rememberMe()
              .tokenValiditySeconds(4838400)
              .key("workerKey");
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws      Exception {
          auth.userDetailsService(new UserDetailsService() {
              @Override
              public UserDetails loadUserByUsername(String username) throws       UsernameNotFoundException {
                  return workersRepository.findByUsername(username);
              }
          });
      }
  }
  </code></pre>
  
  <p>If the register input occurs error, the controller returns the registerForm.html to ask user to input again correctly. If the register has no error, the controller redirects to "/", a simple welcome.html. But whether the input is correct or not, I always get the error 403. When I input<code>http://localhost:8080/</code>, I can get the welcome.html,which is a simple page with "welcome!" words. My controller is</p>
  
  <pre><code>private WorkersRepository workersRepository;
  
  @Autowired
  public  WorkingHoursController(
          WorkersRepository workersRepository) {
      this.workersRepository = workersRepository;
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public String welcomePage() {
      return "welcome";
  }
  
  @RequestMapping(value = "/register", method = RequestMethod.GET)
  public  String showRegistrationForm(Model model) {
      model.addAttribute(new Worker());
      return "registerForm";
  }
  
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public String registrationProcessing(@Valid Worker worker, Errors errors, RedirectAttributes model) {
      if(errors.hasErrors()) {
          return "registerForm";
      }
      workersRepository.save(worker);
      model.addAttribute("username", worker.getUsername());
      model.addFlashAttribute("worker", worker);
      return "redirect:/";
  }
  ...
  </code></pre>
  
  <p>I wrote the registerForm.html using thymeleaf and add error validations. My registerForm.html is</p>
  
  <pre><code>&lt;form class="form-signin" method="post" th:object="${worker}"&gt;
  &lt;div class="errors" th:if="${#fields.hasErrors('*')}"&gt;
      &lt;ul&gt;
          &lt;li th:each="err : ${#fields.errors('*')}"
              th:text="${err}"&gt;Input is in correct.&lt;/li&gt;
      &lt;/ul&gt;
  &lt;/div&gt;
  &lt;img class="mb-4" src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" alt="" width="72" height="72"&gt;
  &lt;h1 class="h3 mb-3 font-weight-normal"&gt;Please register&lt;/h1&gt;
  
  &lt;!-- input username --&gt;
  &lt;label for="inputUsername" th:class="${#fields.hasErrors('username')}? 'error'"&gt;Username&lt;/label&gt;
  &lt;input type="text" id="inputUsername" th:field="*{username}" th:class="${#fields.hasErrors('username')}? 'error form-control':'form-control'" placeholder="Username"&gt;
  ...
  &lt;!-- input password --&gt;
  &lt;label for="inputPassword" th:class="${#fields.hasErrors('password')}? 'error'"&gt;Password&lt;/label&gt;
  &lt;input type="password" id="inputPassword" th:field="*{password}" th:class="${#fields.hasErrors('password')}? 'error form-control':'form-control'" placeholder="Password"&gt;
  &lt;div class="checkbox mb-3"&gt;
      &lt;label&gt;
          &lt;input type="checkbox" id="remember-me" name="remember-me"&gt; Remember me
      &lt;/label&gt;
  &lt;/div&gt;
  &lt;button class="btn btn-lg btn-primary btn-block" type="submit"&gt;Register&lt;/button&gt;
  </code></pre>
  
  <p></p>
  
  <p>Before I add validations in thymeleaf and add spring security, everything seems to work properly. </p>