title: Getting forbidden error in Spring Security (spring boot)
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>package com.project.agro.service;
  
  
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.project.agro.model.Role;
  import com.project.agro.model.User;
  import com.project.agro.repos.UserRepository;
  
  import java.util.HashSet;
  import java.util.Set;
  
  @Service
  public class UserDetailsServiceImpl implements UserDetailsService{
      @Autowired
      private UserRepository userRepository;
  
      @Override
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) {
          User user = userRepository.findByUsername(username);
          if (user == null) throw new UsernameNotFoundException(username);
  
          Set&lt;GrantedAuthority&gt; grantedAuthorities = new HashSet&lt;&gt;();
          for (Role role : user.getRoles()){
              grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
          }
  
          return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
      }
  }
  package com.project.agro.service;
  
  
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.project.agro.model.Role;
  import com.project.agro.model.User;
  import com.project.agro.repos.UserRepository;
  
  import java.util.HashSet;
  import java.util.Set;
  
  @Service
  public class UserDetailsServiceImpl implements UserDetailsService{
      @Autowired
      private UserRepository userRepository;
  
      @Override
      @Transactional(readOnly = true)
      public UserDetails loadUserByUsername(String username) {
          User user = userRepository.findByUsername(username);
          if (user == null) throw new UsernameNotFoundException(username);
  
          Set&lt;GrantedAuthority&gt; grantedAuthorities = new HashSet&lt;&gt;();
          for (Role role : user.getRoles()){
              grantedAuthorities.add(new SimpleGrantedAuthority(role.getName()));
          }
  
          return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), grantedAuthorities);
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
    &lt;link href="${contextPath}/resources/css/bootstrap.min.css" rel="stylesheet"&gt;
        &lt;link href="${contextPath}/resources/css/style.css" rel="stylesheet"&gt;
        &lt;link href="${contextPath}/resources/css/fixed.css" rel="stylesheet"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;%@ include file="common/navbar.jsp"%&gt;
  
   &lt;div class="container-fluid"&gt;
    
    
    &lt;div class="row"&gt;
    
    &lt;div class="col-3"&gt;
    &lt;div class="side-nav"&gt;
     &lt;nav&gt;
      &lt;ul&gt;
       &lt;li&gt;&lt;a href="#"&gt; &lt;span&gt;Title&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
                     &lt;li&gt;&lt;a href="crops.html"&gt; &lt;span&gt;Crops&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
       &lt;li class="active"&gt;&lt;a href="fertilizer.html"&gt; &lt;span&gt;Fertilizer&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
       &lt;li&gt;&lt;a href="#"&gt; &lt;span&gt;Agro News&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
     &lt;/nav&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-9"&gt;
     &lt;form class="form-horizontal" action="/admin/crop/addcrop"
      method="post"  enctype="multipart/form-data" style="margin-top:5rem;" &gt;
      &lt;fieldset&gt;
       &lt;legend class="center-block"&gt;
        New crop Information
       &lt;/legend&gt;
  
       
       &lt;!-- title --&gt;
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;label class="col-sm-2 control-label" for="title"&gt;Crop Name&lt;/label&gt;
  
        &lt;div class="col-sm-8"&gt;
         &lt;input type="text" name="cName" class="form-control" id="cName"
           required="required" placeholder="Title" /&gt; 
        &lt;/div&gt;
       &lt;/div&gt;
  
       &lt;!-- author --&gt;
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;label class="col-md-2 control-label" for="cScientificName"&gt;
         Scientific Name&lt;/label&gt;
        &lt;div class="col-md-8"&gt;
         &lt;input type="text" name="cScientificName" class="form-control"
          id="cScientificName" required="required"
          placeholder="Scientific Name" /&gt;
        &lt;/div&gt;
       &lt;/div&gt;
  
  
  
       &lt;!-- description --&gt;
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;label class="col-md-2 control-label" for="description"&gt;Description&lt;/label&gt;
        &lt;div class="col-md-8"&gt;
         &lt;textarea name="description" rows="5" class="form-control"
          id="description" placeholder="Description"&gt;&lt;/textarea&gt;
        &lt;/div&gt;
       &lt;/div&gt;
  
       &lt;!-- upload image --&gt;
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;div class="col-md-2"&gt;
         &lt;label for="cImage"&gt;Upload crop image&lt;/label&gt;
        &lt;/div&gt;
        &lt;div class="col-md-8"&gt;
         &lt;input id="cImage" type="file" name="cImage" value="cImage" /&gt;
        &lt;/div&gt;
       &lt;/div&gt;
  
       &lt;!-- description --&gt;
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;label class="col-md-2 control-label" for="description"&gt;Associated
         Disease&lt;/label&gt;
        &lt;div class="col-md-8"&gt;
         &lt;input type="text" name="associatedDisease" class="form-control"
          id="description" 
          placeholder="Associated Disease" /&gt; 
        &lt;/div&gt;
       &lt;/div&gt;
  
       &lt;div class="form-group" style="display:flex;"&gt;
        &lt;div class="col-md-2"&gt;&lt;/div&gt;
        &lt;div class="col-md-8"&gt;
         &lt;button type="submit" class="btn btn-success"&gt;Add Book&lt;/button&gt;
         &lt;a class="btn btn-danger" href=""&gt;Cancel&lt;/a&gt;
        &lt;/div&gt;
       &lt;/div&gt;
      &lt;/fieldset&gt;
     &lt;/form&gt;
     &lt;/div&gt;
    &lt;/div&gt;
   &lt;/div&gt;
     &lt;script src="${contextPath}/resources/js/bootstrap.min.js" &gt;&lt;/script&gt;
   &lt;script src="${contextPath}/resources/js/jquery-3.3.1.min.js" &gt;&lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <ul>
   - 
  </ul>
  
  
  
  <pre><code>package com.project.agro;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  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.UserDetailsService;
  import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  
  @Configuration
  @EnableWebSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      @Autowired
      private UserDetailsService userDetailsService;
  
      @Bean
      public BCryptPasswordEncoder bCryptPasswordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
                  .antMatchers("/admin/**").hasAuthority("ADMIN")
                  .antMatchers("/resources/**", "/registration","/home").permitAll()
                  .anyRequest().authenticated()
                  .and()
  
              .formLogin()
                  .loginPage("/login")
                  .permitAll().defaultSuccessUrl("/welcome")
                  .and()
              .logout()
                  .permitAll();
      }
  
      @Bean
      public AuthenticationManager customAuthenticationManager() throws Exception {
          return authenticationManager();
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
      }
  }
  </code></pre>
  
  
  
  <pre><code>package com.project.agro.controller;
  
  
  
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.multipart.MultipartFile;
  
  import com.project.agro.model.Crop;
  import com.project.agro.service.CropService;
  
  
  
  @Controller
  public class CropController {
  
      @Autowired
      private CropService cropService;
  
      @RequestMapping(value="/admin/crop/addcrop" ,method = RequestMethod.GET)
      public String addcrop(Model model) {
          Crop crop = new Crop();
          model.addAttribute("crop", crop);
          return "addcrop";
      }
  
      @RequestMapping(value="/admin/crop/addcrop" , method = RequestMethod.POST)
      public  String addcroppost(@ModelAttribute(value="crop") Crop crop ,HttpServletRequest request){
          cropService.save(crop);
          MultipartFile cImage=crop.getcImage();
          try {
              byte[] bytes = cImage.getBytes();
              String name = crop.getCropID() + ".png";
              BufferedOutputStream stream = new BufferedOutputStream(
                      new FileOutputStream(new File("src/main/webapp/image/crop/" + name)));
              stream.write(bytes);
              stream.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return "redirect:/cropList";
  
  
  
      }
  
      @RequestMapping("/cropList")
      public String cropList(Model model) {
          /*List&lt;Book&gt; bookList = bookService.findAll();*/
  
          return "cropList";
  
      }
  }
  </code></pre>
  
  <p>I am trying to build an web application using Spring Boot and Spring Security.I am submitting a form with POST method as an admin to add some details into database but everytime I hit that submit button it shows the error page.I have added all the dependencies .The registration and login page works fine What am I suppose to do here?</p>