title: AuthenticationCredentialsNotFoundException: An Authentication object was not found in the Security Context
body: <p>I get the below error when I try to send JSON payload from POSTMAN plugin of chrome browser to controller exposed as REST URL - <a href="http://localhost:8080/services/acc/create" rel="noreferrer">http://localhost:8080/services/acc/create</a></p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/services] threw exception [Request processing failed; nested exception is org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext] with root cause
  org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
      at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:339)
      at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:198)
      at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
      at com.webadvisors.controller.HotelRestController$$EnhancerBySpringCGLIB$$e9f80d9.createHotel(&lt;generated&gt;)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
  </code></pre>
  
  <p>I selected BasicAuth as authorization type and entered username and password while sending JSON payload to REST URL in POSTMAN.</p>
  
  <p>1) Controller class</p>
  
  <pre><code>@RestController
  public class AccountRestController {
  
      @Autowired
      private AccountService accountService;
  
      @PreAuthorize("hasAnyRole('ADMINISTRATOR')")
      @RequestMapping(value= "/acc/create", method=RequestMethod.POST)
      public HotelDTO createHotel(@RequestBody AccountDTO accDTO) throws Exception{
          return accountService.create(accDTO);
      }
  } 
  </code></pre>
  
  <p>2) Security Configuration class</p>
  
  <pre><code>@Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  @EnableWebSecurity
  @ComponentScan(basePackages = "com.freelance", scopedProxy = ScopedProxyMode.INTERFACES)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      @Qualifier("authenticationService")
      private UserDetailsService userDetailsService;
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
          auth.authenticationProvider(authenticationProvider());
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
         http
                  .authorizeRequests()
                  .antMatchers("/user/**").permitAll()
                  .anyRequest().fullyAuthenticated();
          http.httpBasic();
          http.csrf().disable();
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      public DaoAuthenticationProvider authenticationProvider() {
          DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
          authenticationProvider.setUserDetailsService(userDetailsService);
          authenticationProvider.setPasswordEncoder(passwordEncoder());
          return authenticationProvider;
      }
  }
  </code></pre>
  
  <p>3) Spring security dependencies</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-config&lt;/artifactId&gt;
          &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
     &lt;/dependency&gt;
     &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
      &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
      &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I am sending the authentication credentials in the POSTMAN. But still why am I getting the above exception.</p>