title: Spring Boot 401 Unauthorized Postman
body: <p>I am new with spring boot, am trying to do an authorized request with postman but i get 401 Unauthorized.
  i am trying to store users in a database H2 using jpa with post request that takes a json as body,
  i don't know how to explain more so i ll post the code if anyone figure it out.</p>
  <pre><code>
  
  import engine.user.UserDetailsServiceImpl;
  import org.springframework.beans.factory.annotation.Autowired;
  
  import org.springframework.boot.context.properties.EnableConfigurationProperties;
  import org.springframework.context.annotation.Configuration;
  
  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.WebSecurityConfigurerAdapter;
  
  
  @Configuration
  @EnableConfigurationProperties
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      private final UserDetailsServiceImpl userDetailsService;
  
      @Autowired
      public WebSecurityConfig(UserDetailsServiceImpl userDetailsService) {
          this.userDetailsService = userDetailsService;
      }
  
  
  
     @Override
     protected void configure(HttpSecurity http) throws Exception {
         http
                 .csrf().disable().headers().frameOptions().disable().and()
                 .authorizeRequests().antMatchers(&quot;/api/register&quot;).permitAll()
                 .antMatchers(&quot;/actuator/shutdown&quot;).permitAll()
                 .and()
                 .httpBasic();
     }
  
      @Override
      public void configure(AuthenticationManagerBuilder builder)
              throws Exception {
          builder.userDetailsService(userDetailsService);
      }
  
  }
  </code></pre>
  <p>dependencies</p>
  <pre><code>    id 'org.springframework.boot' version '2.2.2.RELEASE'
      id 'java'
  }
  
  apply plugin: 'io.spring.dependency-management'
  
  sourceCompatibility = 11
  
  repositories {
      mavenCentral()
  }
  
  sourceSets.main.resources.srcDirs = [&quot;src/resources&quot;]
  
  dependencies {
      implementation 'org.springframework.boot:spring-boot-starter'
      implementation 'org.springframework.boot:spring-boot-starter-actuator'
      implementation 'org.springframework.boot:spring-boot-starter-security'
      implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
      compile(&quot;org.springframework.boot:spring-boot-starter-web&quot;)
      runtimeOnly 'com.h2database:h2'
  
  } 
  </code></pre>
  <p>the usercontroller</p>
  <pre><code>
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.PostMapping;
  import org.springframework.web.bind.annotation.RequestBody;
  
  import javax.validation.Valid;
  
  public class UserController {
  
      private final UserDetailsServiceImpl userDetailsService;
  
      @Autowired
      public UserController(UserDetailsServiceImpl userDetailsService) {
          this.userDetailsService = userDetailsService;
      }
  
      @PostMapping(value = &quot;/api/register&quot;, consumes = &quot;application/json&quot;)
      public ResponseEntity&lt;User&gt; saveUser(@Valid @RequestBody  User user) {
          userDetailsService.save(user);
          return new ResponseEntity&lt;&gt;(user, HttpStatus.OK);
      }
  } 
  </code></pre>
  <p><a href="https://i.stack.imgur.com/4bpAo.jpg" rel="nofollow noreferrer">Postman</a></p>