1

I have an end point (API) (built with Jersey) i want to access from Angular based client. Now the endpoint works fine at the first try. Then i wanted to add some security and wanted to use JWT token authentication. To fulfill this i added a header to the request then the client side started responding 403 Forbidden. I have browsed some resources and i found out that it is due to CORS. Anyone with any suggestions will be deeply appreciated. Here is what i tried so far.

public Response respondOk(String message){
    response.setStatusCode(200);
    response.setMessage("Ok");
    response.setDescription(message);
    return Response.status(200).entity(json.toJson(response))
            .header("Access-Control-Allow-Origin", "*")
            .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS")
            .header("Access-Control-Allow-Credentials",true)
            .header("Access-Control-Allow-Headers", "Accept, Accept-CH, Accept-Charset, Accept-Datetime, Accept-Encoding, Accept-Ext, Accept-Features, Accept-Language, Accept-Params, Accept-Ranges, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Request-Headers, Access-Control-Request-Method, Age, Allow, Alternates, Authentication-Info, Authorization, C-Ext, C-Man, C-Opt, C-PEP, C-PEP-Info, CONNECT, Cache-Control, Compliance, Connection, Content-Base, Content-Disposition, Content-Encoding, Content-ID, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Script-Type, Content-Security-Policy, Content-Style-Type, Content-Transfer-Encoding, Content-Type, Content-Version, Cookie, Cost, DAV, DELETE, DNT, DPR, Date, Default-Style, Delta-Base, Depth, Derived-From, Destination, Differential-ID, Digest, ETag, Expect, Expires, Ext, From, GET, GetProfile, HEAD, HTTP-date, Host, IM, If, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Keep-Alive, Label, Last-Event-ID, Last-Modified, Link, Location, Lock-Token, MIME-Version, Man, Max-Forwards, Media-Range, Message-ID, Meter, Negotiate, Non-Compliance, OPTION, OPTIONS, OWS, Opt, Optional, Ordering-Type, Origin, Overwrite, P3P, PEP, PICS-Label, POST, PUT, Pep-Info, Permanent, Position, Pragma, ProfileObject, Protocol, Protocol-Query, Protocol-Request, Proxy-Authenticate, Proxy-Authentication-Info, Proxy-Authorization, Proxy-Features, Proxy-Instruction, Public, RWS, Range, Referer, Refresh, Resolution-Hint, Resolver-Location, Retry-After, Safe, Sec-Websocket-Extensions, Sec-Websocket-Key, Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Version, Security-Scheme, Server, Set-Cookie, Set-Cookie2, SetProfile, SoapAction, Status, Status-URI, Strict-Transport-Security, SubOK, Subst, Surrogate-Capability, Surrogate-Control, TCN, TE, TRACE, Timeout, Title, Trailer, Transfer-Encoding, UA-Color, UA-Media, UA-Pixels, UA-Resolution, UA-Windowpixels, URI, Upgrade, User-Agent, Variant-Vary, Vary, Version, Via, Viewport-Width, WWW-Authenticate, Want-Digest, Warning, Width, X-Content-Duration, X-Content-Security-Policy, X-Content-Type-Options, X-CustomHeader, X-DNSPrefetch-Control, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto, X-Frame-Options, X-Modified, X-OTHER, X-PING, X-PINGOTHER, X-Powered-By, X-Requested-With")
            .allow("OPTIONS")
            .build();
}

This is my response class. and here is my resource class

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response test(@HeaderParam("Authorization")String authorization){

    if(validatorService.isNullOrEmpty(authorization)){
        return responderService.respondUnAuthorized("Sorry world");
    }
    return responderService.respondOk("Hello world");
}

Now when i access the endpoint from Postman it works perfect. But when i try it from my client built with angular it responds Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

Appreciate the help. Thanx!!!

djdere
  • 321
  • 2
  • 10
  • 2
    Don't set your CORS headers in resource methods. The [CORS preflight](https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request) will never reach it. [Use a filter](http://stackoverflow.com/a/28067653/2587435) instead. – Paul Samsotha May 12 '17 at 13:36
  • You can also use filters for your security instead of handling it in every resource method. See [this post](http://stackoverflow.com/q/26777083/2587435) – Paul Samsotha May 12 '17 at 13:50

0 Answers0