springangular7angular2-jwt

Cannot includ/send jwt token using auth0/angular-jwt


I use "https://github.com/auth0/angular2-jwt" to send JWT token to my server, I can see the token when I debug http request (angular) but at the server (java) the token is not found

this is my config jwt

JwtModule.forRoot({
  config: {
    headerName: 'API_TOKEN',
    tokenGetter: function tokenGetter() {
      return localStorage.getItem('API_TOKEN');
    },
    whitelistedDomains: ['localhost:8092'],
    // blacklistedRoutes: ['https://localhost:8092/login'],
    authScheme: ''
  }
}),

I added a JwtHttpInterceptor for debug my request :

@Injectable()
export class JwtHttpInterceptor implements HttpInterceptor {
  constructor() {}
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
 return next.handle(req);
}
}

and this is a screenshot : JWT token in header request

but server could not find this token.

When I try to add token with a chrome pluging, It work and server can find my token : token added with chrome pluging

Can you help me please?


Solution

  • I found the solution if it will help another person, in fact it comes from the backend, I reversed the spring security filters and my request "OPTIONS" should go through the filter CORS first

            httpSecurity
    .addFilter(jwtAuthenticationFilter)
    .addFilterBefore(corsInputFilter, UsernamePasswordAuthenticationFilter.class) // OPTIONS REQUEST SHOULD COME HERE IN THE FIRST AND RETURN THE RESPONSE WITHOUT CONTINUE OTHERS FILTERS
    .addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);