http - CORS Java server side implementation -
i need implement cors support in jersey based rest server. i've gone through of available material , informative tutorials . found 2 approaches people using:
approach-1 :
simple , direct approach implement 1 http
filter adds cors
header response (jersey specific)
public class responsecorsfilter implements containerresponsefilter { public containerresponse filter(containerrequest req, containerresponse contresp) { responsebuilder resp = response.fromresponse(contresp.getresponse()); resp.header("access-control-allow-origin", "*") .header("access-control-allow-methods", "get, post, options"); string reqhead = req.getheadervalue("access-control-request-headers"); if(null != reqhead && !reqhead.equals(null)){ resp.header("access-control-allow-headers", reqhead); } contresp.setresponse(resp.build()); return contresp; } }
approach-2 :
fully implement cors per specification i.e. preflight request handling , header support. inspected source code of 1 such open-source java implementation cors-filter
my question approach should taken when? downside of approach-1 vs approach-2?
my use case origins/methods can allowed , authorization
http header part of rest
requests. inclined towards approach-1 seems of default cors settings suffice use case not sure if not having full cors specs implemented @ server side create issues whatsoever.
for purposes, approach #1 sounds sufficient. approach #2 more case have different responses based on request type, or want validate request information. if response same across request types, #1 should fine. note because implementation allowing requests succeed, should doing own checks make sure request valid. since allowing authorization header, i'm assuming aware of this, , validating authorization token?
Comments
Post a Comment