title: Spring Security for Rest @PreAuthorize 500 error
body: <p>I want basic security for rest ,this is my config:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  @Autowired
  private PacijentUserDetailsService pacijent;
  @Autowired
  private FizioterapeutUserDetailsService fizioterapeut;
  @Autowired
  private FizijatarUserDetailsService fizijatar;
  
  @Override
  protected void configure(AuthenticationManagerBuilder
          auth) throws Exception {
          auth.userDetailsService(pacijent)
          .passwordEncoder(new
          BCryptPasswordEncoder());
          auth.userDetailsService(fizioterapeut).passwordEncoder(new
          BCryptPasswordEncoder());
          auth.userDetailsService(fizijatar).passwordEncoder(new
          BCryptPasswordEncoder());
  }
  @Override
  protected void configure(HttpSecurity http) throws Exception
  {
      http
      .sessionManagement()
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
      .and()
      .authorizeRequests()
      .antMatchers("/pacijent/", "/fizijatar/","/fizioterapeut/").permitAll()
      .antMatchers("/pacijent/**","/fizijatar/**","/fizioterapeut/**").authenticated()
      .and()
      .httpBasic()
      .realmName("Ordinacija")
      .and()
      .csrf()
      .disable();
  
    }
    @Bean
     @Override
    public AuthenticationManager authenticationManagerBean() throws Exception          {
      return super.authenticationManagerBean();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I have 3 implements of userdetailservice this is one example:</p>
  
  <pre><code>  @Component
   public class PacijentUserDetailsService implements UserDetailsService {
  
  @Autowired
  private PacijentService pacijentService;
  
  @Override
  public UserDetails loadUserByUsername(String jmbg) throws UsernameNotFoundException {
  Pacijent pacijent = pacijentService.vratiPacijenta(jmbg);
      if (pacijent == null) {
          throw new UsernameNotFoundException(String.format("Pacijent nije pronadjen", jmbg));
  
      }
  
      List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;&gt;();
      if (pacijentService.postojiPacijentPoJmbgu(jmbg)) {
          authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
      }
  
      UserDetails userDetails = new org.springframework.security.core.userdetails.User(pacijent.getJmbg(),
              pacijent.getSifra(), authorities);
      return userDetails;
    }
  }
  </code></pre>
  
  <p>And my web xml file:</p>
  
  <pre><code>           &lt;context-param&gt;
   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;/WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
    &lt;/context-param&gt;
  </code></pre>
  
  <p>
      org.springframework.web.context.ContextLoaderListener
  </p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet- class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;session-config&gt;
      &lt;session-timeout&gt;
          30
      &lt;/session-timeout&gt;
  &lt;/session-config&gt;
  </code></pre>
  
  <p>When i start my app and go to rest method which have @PreAuthorize method i have error 500:
  Request processing failed; nested exception is org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContex.</p>