title: Spring Boot Authorization returning 401 error?
body: <p>I have the problem that when I'm successfully logged in, I want to make a request to get the user details.
  the request I am sending is an secured GET request which is <code>https://localhost:8080/users/userOne</code> this request is send by my Angular frontend. </p>
  
  <p>But my problem now is, Spring Boot is telling me that the user isn't authorized and sending an 401 error. 
  I'am sending an Authorization token when I make the request from frontend to backend. In my code below you can see what I'am doing.</p>
  
  <p><strong>Angular Service</strong>:</p>
  
  <pre><code>  getUserDetails(username) {
      const headers = new HttpHeaders().append('Authorization', this.cookieService.get('token')).append('Content-Type', 'application/json');
      return this.http.get(`http://localhost:8080/users/` + username, { headers }).pipe(
        map(
          userData =&gt; {
            console.log(userData);
            return userData;
          }
        ));
    }
  </code></pre>
  
  <p><strong>My UserController:</strong></p>
  
  <pre><code>@RestController
  public class UserController {
  
      @Autowired
      private UserService userService;
  
      @CrossOrigin(origins = "http://localhost:4200/", maxAge = 3600)
      @RequestMapping(value = "/users", method = RequestMethod.GET)
      public ResponseEntity&lt;User&gt; getUserByUsername (@RequestBody String username){
          final User user = userService.getUserByUsername(username);
          return ResponseEntity.ok(user);
      }
  }
  </code></pre>
  
  <p><strong>My WebSecurityConfig</strong></p>
  
  <pre><code>@Override
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable().authorizeRequests()
                  .antMatchers(HttpMethod.POST,"/authenticate").permitAll()
                  .anyRequest().authenticated().and()
                  .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                  .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                  .and().formLogin().disable();
          http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
      }
  </code></pre>
  
  <p><strong>My Filter</strong> </p>
  
  <pre><code>@Override
      protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws ServletException, IOException {
          res.setHeader("Access-Control-Allow-Origin", "*");
          res.setHeader("Access-Control-Allow-Credentials", "true");
          res.setHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,PATCH,OPTIONS");
          res.setHeader("Access-Control-Allow-Headers", "Authorization, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
  
          if ("OPTIONS".equalsIgnoreCase(req.getMethod())) {
              res.setStatus(HttpServletResponse.SC_OK);
          } else {
              chain.doFilter(req, res);
          }
      }
  </code></pre>
  
  <p><strong>Authentication request in Angular:</strong></p>
  
  <pre><code>authenticate(username, password) {
        const headers = new HttpHeaders({
          'Content-Type': 'application/json',
          Authorization: sessionStorage.getItem('token'),
        });
        return this.httpClient.post&lt;any&gt;('http://localhost:8080/authenticate', {username, password}).pipe(
         map(
           userData =&gt; {
            sessionStorage.setItem('username', username);
            sessionStorage.setItem('token', userData.token);
            this.isLoggedIn = true;
            return userData;
           }
         )
        );
      }
  </code></pre>
  
  <p>The Error I'm getting:</p>
  
  <pre><code>GET http://localhost:8080/users/userOne 401
  </code></pre>
  
  <pre><code>ERROR HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "OK", url: "http://localhost:8080/users/Bogatom", ok: false, …}
  </code></pre>
  
  <p>I don't know what I am doing wrong...</p>