2

I have the problem that HttpServletRequest request.getHeader("Authorization") is always null value, I send with angular service my get request with headers but these never seem to arrive.

I'm using Spring Boot as my REST Server. And Angular as my frontend.

user.service.ts

getUserDetails(username): Observable<any> {
    const headers = new HttpHeaders({
      'Content-Type': 'application/json',
      // tslint:disable-next-line: object-literal-key-quotes
      'Authorization': sessionStorage.getItem('token'),
    });
    // tslint:disable-next-line: object-literal-shorthand
    console.log(`http://localhost:8080/users/` + username, { headers: headers });
    return this.http.get(`http://localhost:8080/users/` + username, { headers: headers }).pipe(
      map(
        userData => {
          console.log(userData);
          return userData;
        }
      ));
  }

JWTRequestFilter.java

@Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        final String requestTokenHeader = request.getHeader("Authorization");
        String username = null;
        String jwtToken = null;
        // JWT Token is in the form "Bearer token". Remove Bearer word and get
        // only the Token
        if (requestTokenHeader != null && requestTokenHeader.startsWith("Bearer ")) {
            jwtToken = requestTokenHeader.substring(7);
            try {
                username = jwtTokenUtil.getUsernameFromToken(jwtToken);
            } catch (IllegalArgumentException e) {
                System.out.println("Unable to get JWT Token");
            } catch (ExpiredJwtException e) {
                System.out.println("JWT Token has expired");
            }
        } else {
            logger.warn("JWT Token does not begin with Bearer String");
        }
        // Once we get the token validate it.
        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
            UserDetails userDetails = this.jwtUserDetailsService.loadUserByUsername(username);
            // if token is valid configure Spring Security to manually set
            // authentication
            if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {
                UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
                        userDetails, null, userDetails.getAuthorities());
                usernamePasswordAuthenticationToken
                        .setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
                // After setting the Authentication in the context, we specify
                // that the current user is authenticated. So it passes the
                // Spring Security Configurations successfully.
                SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
            }
        }
        chain.doFilter(request, response);
    }

The Network Traffic:

enter image description here

When I'm going to the account page where I call the get request http://localhost:8080/users/testUser

The requestTokenHeader is always null?

Hope you guys can help me out. Thanks in advance.

Tom van den Bogaart
  • 143
  • 1
  • 4
  • 15

0 Answers0