SuperPlan(14)Winner Seller Server - JSON Cross Domain

SuperPlan(14)WinnerSellerServer-JSONCrossDomain

16.JSONcrossdomain

IwillrockandrollinJSONRESTandJSONclientnow.JSONPisnotwhatIwant.

ItiscomplexthanIthought.Actually,ImaketheJSONbasicauthworkingwithJSONPbasicauth.JSONPisforthenavigatebars,JSONisworkingfortheproductsrightnow.

16.1ServerSide

IneedtoextendtheHttpHeadertoenablethecrossdomainaccess.Thedocumentsinsprayarenotindetail,ittaketimetounderstandandreadthesourcescalacodesinspray.

packageobjectCrossDomainHeaders{

caseclassOrigin(origin:String)extendsHttpHeader{

defname="Origin"

deflowercasename="origin"

defvalue=origin

}

caseclass`Access-Control-Allow-Origin`(origin:String)extendsHttpHeader{

defname="Access-Control-Allow-Origin"

deflowercasename="access-control-allow-origin"

defvalue=origin

}

caseclass`Access-Control-Allow-Headers`(origin:String)extendsHttpHeader{

defname="Access-Control-Allow-Headers"

deflowercasename="access-control-allow-headers"

defvalue=origin

}

caseclass`Access-Control-Allow-Methods`(origin:String)extendsHttpHeader{

defname="Access-Control-Allow-Methods"

deflowercasename="access-control-allow-methods"

defvalue=origin

}

caseclass`Access-Control-Allow-Credentials`(allowed:Boolean)extendsHttpHeader{

defname="Access-Control-Allow-Credentials"

deflowercasename="access-control-allow-credentials"

defvalue=if(allowed)"true"else"false"

}

}

Inmyrouter,Ineedtoconsidertogettherequestheader,findouttheoriginURLandmatchmywhitelistofthedomainnames.

…snip...

implicitvalproductFormatter=ProductJsonProtocol.ProductJsonFormat

headerValueByName("Origin"){originHeader=>

respondWithHeaders(`Access-Control-Allow-Origin`(originHeader),`Access-Control-Allow-Credentials`(true)){

authenticate(BasicAuth(newBrandUserPassAuthenticator(dao),"Realm")){user=>

…snip…

Rightnow,itwillaccessallthedomainnames,becauseitjustsayoriginHeader,Iwillrewriteittomatchawhitelist.

Hereisthewaytoreadthewhitelistinconfigurationfile.

server{

address="0.0.0.0"

port=9001

crossdomain.eanble=true

crossdomain.list=["http://localhost:9000","http://localhost:9001"]

}

vallists=config.getStringList("server.crossdomain.list")

16.2ClientSide

Thereisnothingspecialontheclient,becausewesendthecredentialtoserverviajsonp.

//json

if(config.productsProvider=='json'){

options.dataType="json";

options.crossDomain=true;

options.xhrFields={withCredentials:true};

//options.beforeSend=function(xhr){

//xhr.setRequestHeader('Authorization','BasicY3VzdG9tZXI6Y3VzdG9tZXI=');

//};

varurl_str='http://'+config.remoteServerURL+':'+config.remoteServerPort

url_str=url_str+'/'+config.apiVersion+'/'+config.brandName+'/'+'products';

options.url=url_str;

}

17.Jasmine

comesonn...

18.Integration(Backbone+Require+Jasmine+Phantom+Grunt+Bootstrap)

comesoon…

References:

superPlan13

integration

http://hdnrnzk.me/2013/01/10/backbone-requirejs-jasmine-phantomjs-and-grunt/

https://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup

superplan(11)phantom

相关推荐