title: JWT is always returning unauthorized
body: <p>I have implemented the <a href="https://www.techgeeknext.com/spring/spring-boot-security-token-authentication-jwt-mysql" rel="nofollow noreferrer">JWT Application</a></p>
  
  <p>I am using JWT 0.9.1 version</p>
  
  <p>I am able to generate the Json web token, but when I pass to the <code>http://localhost:8080/api/v1/greeting</code> method I am always getting the same error like 401 Unauthorized.</p>
  
  <p><strong>SpringSecurityConfig.java</strong></p>
  
  <pre><code> @Configuration
  @EnableWebSecurity
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
  
      @Autowired
      private UserDetailsService jwtUserDetailsService;
  
      @Autowired
      private JwtRequestFilter jwtRequestFilter;
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return new BCryptPasswordEncoder();
      }
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          return super.authenticationManagerBean();
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          // configure AuthenticationManager so that it knows from where to load
          // user for matching credentials
          // Use BCryptPasswordEncoder
          auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
      }
  
      /*@Override
      protected void configure(HttpSecurity http) throws Exception{
          http.csrf().disable().authorizeRequests().antMatchers("/").permitAll();
      }*/
  
      @Override
      protected void configure(HttpSecurity httpSecurity) throws Exception {
          httpSecurity.csrf().disable()
                  // dont authenticate this particular request
                  .authorizeRequests().antMatchers("/api/v1/authenticate", "/api/v1/register","/api/v1/basicauth")
                  /*.permitAll().antMatchers(HttpMethod.OPTIONS, "/**")*/
                  // all other requests need to be authenticated
                  .permitAll().anyRequest().authenticated().and().
                  // make sure we use stateless session; session won't be used to
                  // store user's state.
                          exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                  .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  
          // Add a filter to validate the tokens with every request
          httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
      }
  
  }
  </code></pre>
  
  <p><strong>GreetingController.java</strong></p>
  
  <pre><code>@CrossOrigin(origins = "http://localhost:4200")
  @RestController
  @RequestMapping("/api/v1")
  public class GreetingController {
  
      private static final String template = "Hello, %s!";
      private final AtomicLong counter = new AtomicLong();
  
      @RequestMapping("/greeting")
      public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
          return new Greeting(counter.incrementAndGet(), String.format(template, name));
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BJNcy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BJNcy.png" alt="enter image description here"></a></p>