暢購商城源碼chapter10購物車講義第10章_第1頁
暢購商城源碼chapter10購物車講義第10章_第2頁
暢購商城源碼chapter10購物車講義第10章_第3頁
暢購商城源碼chapter10購物車講義第10章_第4頁
暢購商城源碼chapter10購物車講義第10章_第5頁
已閱讀5頁,還剩19頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

SpringSecurity用戶每次微服務(wù)的時(shí)候,先去oath.服務(wù)登錄,登錄后再微服務(wù)網(wǎng)關(guān),微服務(wù)網(wǎng)關(guān)將請(qǐng)求轉(zhuǎn)發(fā)給其他微服務(wù)處理。普通用戶角色不能使用用戶的刪除操作,只有管理員才可以使用,那么這個(gè)時(shí)候就需要使用到SpingSecuity的權(quán)限控制功能了。在chango-sroat服務(wù)中,co.cangg.ot.co?g.UserDtaisServiceImpl上述代碼給登錄用戶定義了三個(gè)角色,分別為salesman,accountant,user,這一塊我們目前使用的@PreFilter@PostFilter。其中前兩者可以用來在方法調(diào)用前或者調(diào)用后進(jìn)行權(quán)限檢查,后兩者@PreAuthorize注解,用于方法執(zhí)行前進(jìn)行權(quán)限檢查,校驗(yàn)用戶當(dāng)前角色是否能該方法。在changgou-user-service的ResourceServerConfig類上添加@EnableGlobalMethodSecurity注在changgoug-service-user微服務(wù)的com.changgou.user.controller.UserControllerdelete()方法上添限控制注解@PreAuthorize,代碼如下我們使用Pstmn測試,先創(chuàng)建令牌,然后將令牌數(shù)存放到頭文件中微服務(wù)網(wǎng)關(guān)來調(diào)用s的seac方法,效果如下:地址 提交方式發(fā)現(xiàn)上面無法,因?yàn)橛脩舻卿浀臅r(shí)候,角色不包含admi角色,而seac方法需要admin角,所以被了。購物車分為用戶登錄購物車和未登錄購物車操作,國內(nèi)知名京東用戶登錄和不登錄都可以操作購物車,如果用戶不登錄,操作購物車可以將數(shù)據(jù)到 ,用戶登錄后購物車數(shù)據(jù)可以到s中,再將之前未登錄加入的購物車合并到s中即可。我們今天實(shí)現(xiàn)的購物車是天貓解決方案,即用戶必須先登錄才能使用購物車功能。用戶在商品詳細(xì)頁點(diǎn)擊加入購物車,提交商品SKU編號(hào)和數(shù)量,添加到購物車。購物車展示頁面下:changgou_order數(shù)據(jù)中tb_order_itemCREATECREATETABLE`tb_order_item``id`varchar(20)COLLATEutf8_binNOTNULLCOMMENT`category_id1`int(11)DEFAULTNULLCOMMENT'1級(jí)分類`category_id2`int(11)DEFAULTNULLCOMMENT'2級(jí)分類`category_id3`int(11)DEFAULTNULLCOMMENT'3級(jí)分類`spu_id`varchar(20)COLLATEutf8_binDEFAULTNULLCOMMENT`sku_id`bigint(20)NOTNULLCOMMENT`order_id`bigint(20)NOTNULLCOMMENT'訂單`name`varchar(200)COLLATEutf8_binDEFAULTNULLCOMMENT'商品名稱`price`int(20)DEFAULTNULLCOMMENT'單價(jià)`num`int(10)DEFAULTNULLCOMMENT'數(shù)量`money`int(20)DEFAULTNULLCOMMENT'總金額`pay_money`int(11)DEFAULTNULLCOMMENT'實(shí)付金額`image`varchar(200)COLLATEutf8_binDEFAULTNULLCOMMENT 地址`weight`int(11)DEFAULTNULLCOMMENT'重量`post_fee`int(11)DEFAULTNULLCOMMENT'運(yùn)費(fèi)`is_return`char(1)COLLATEutf8_binDEFAULTNULLCOMMENT'是否退貨',PRIMARYKEY(`id`),KEY`item_id`(`sku_id`),KEY`order_id`(`order_id`))ENGINE=InnoDBDEFAULTCHARSET=utf8 publicclassSkuControllerprivateSkuServicepublicResult<Sku>findById(@PathVariable("id")Stringid){Skusku=skuService.findById(id);returnnewResult(true,StatusCode.OK,"查詢成功}}@param publicResult<Sku>findById(@PathVariable("id")String訂單服務(wù)新建 publicclassCartControllerprivateCartService@param@param Integernum){Stringusername=returnnewResult(true,StatusCode.OK,"加入購物車成功}}publicclassCartServiceImplimplementsCartServiceprivatestaticfinalStringprivate te privateSkuFeignprivateSpuFeign@parampublicvoidadd(StringskuId,Integernum,Stringusername)OrderItemorderItem=(OrderItem)if(orderItem!=Skusku=skuResult.getData();Spuspu=orderItem=}}privateOrderItemsku2OrderItem(Skusku,Spuspu,Integer{OrderItemorderItem=newOrderItem(); //單價(jià)* returnorderItem;}}接著我們實(shí)現(xiàn)一次購物車列表操作。因?yàn)榇娴臅r(shí)候是根據(jù)用戶名往s中用戶的購物車數(shù)據(jù)的所以我們這里可以將用戶的名字作為y去es中查詢對(duì)應(yīng)的數(shù)據(jù)。3.4.2com.changgou.order.controller.CartController (value="/list")publicMaplist(){Stringusername=" returncartService.list(username);}Maplist(String@parampublicMaplist(String{Mapmap=newList<OrderItem>orderItemList IntegertotalNum=0;IntegertotalPrice0;for(OrderItemorderItem:{totalNum+=orderItem.getNum();}returnmap;}使用Postman 如上圖所示,務(wù),再通過fign調(diào)用購物車訂單微服務(wù)來實(shí)現(xiàn)購物車的操作,例如:加入購物車、購物車列表。在changgou_web中搭建訂單購物車微服務(wù)工程changgou_web_order,該工程主要實(shí)現(xiàn)購物車和訂單 application.ymlname:order-weballow-bean-definition-overriding: :defaultZone:prefer-ip-address:true: #配置全局的feignconnectTimeout60000指定的是消費(fèi)者連接服務(wù)提供者的連接超時(shí)時(shí)間是否能連接readTimeout:80000#指定的是調(diào)用服務(wù)提供者的服務(wù)的超時(shí)時(shí)間()單位是毫秒#hystrixenabled:truestrategy:SEMAPHORE#熔斷器超時(shí)時(shí)間,默認(rèn):1000/timeoutInMilliseconds80000ReadTimeout4000ConnectTimeout:創(chuàng)建com.changgou.OrderWebApplicationpublicpublicclassOrderWebApplicationpublicstaticvoidmain(String[]{}}Feign在changou_servic_odri中添加Crtegn接口,并在接口中創(chuàng)建添加購物車和查詢購物車列表,代碼如下:publicinterfaceCartFeign@param@param Integernum); (value="/cart/list")publicMaplist();} publicclassCartControllerprivateCartFeign*publicStringlist(Modelmodel){Mapmap=return"cart";}* publicResult<Map>add(Stringid,IntegercartFeign.add(id,Mapmap=returnnewResult<>(true,StatusCode.OK,"添加購物車成功}3.2.3<!--<!--vueloadlist--<divclass="cart-list"v-for="iteminitems.orderItemList":key="item.index"><ulclass="goods-listyui3-<liclass="yui3-u-1-<inputtype="checkbox"name="chk_list"value=""<liclass="yui3-u-6-<divclass="good-<divclass="item-<img:src="item.image"<divclass="item-<liclass="yui3-u-5-<divclass="item-<liclass="yui3-u-1-<span<liclass="yui3-u-1-<ahref="javascript:void(0)"@click="add(item.skuId,-1)"class="incrementmins">-</a> plete="off"type="text"v-model="item.num"@blur="add(item.skuId,item.num)"value="1"minnum="1"class="itxt"/><ahref="javascript:void(0)"@click="add(item.skuId,1)"class="incrementplus">+</a><liclass="yui3-u-1-<spanvalue=""

<liclass="yui3-u-1-<ahref="#none">刪除<br<ahref="#none">移到收藏<divclass="cart-<divclass="select-<inputclass="chooseAll"type="checkbox"name=""<div<ahref="#none">刪除選中的商品<ahref="#none">移到我的關(guān)注<ahref="#none">清除下柜商品<divclass="money-<divclass="chosed"><div<em>總價(jià)(不含運(yùn)費(fèi)):<i<div<<scriptth:inline="javascript">varapp=newVue({el:data()returnitems:}add:function(skuId,{axios.get("/wcart/add?skuId="+skuId+"&num="+num).then(function(response){(response.data.flag){app.items=resp}}}修改微服務(wù)網(wǎng)關(guān)hanggo-gatewa-web的aplictio.yl配置文件,添加ode的路由過濾配置,配置如下:id:changgou_order_routeuri:lb://order-id:changgou_order_web_routeuri:lb://order-web用戶可以點(diǎn)擊號(hào)或者號(hào),或者手動(dòng)輸入一個(gè)數(shù)字,然后更新購物車列表,我們可以給-+數(shù)字框一個(gè)失去焦點(diǎn),然后調(diào)用,實(shí)現(xiàn)購物車的更新。修改cago .cango.or.seric.ilCarServceImp的add法添加如下代碼:oauth3)@EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true)privatestaticfinalStringPUBLIC_KEY=@parampublicTokenStoretokenStore(JwtAccessTokenConverterjwtAccessTokenConverter){returnnew}publicJwtAccessTokenConverterreturn}獲取非對(duì)稱加密公鑰@return公鑰trytryInputStreamReaderinputStreamReader=newreturnbr.lines().collect(Collectors.joining("\n"));}catch(IOException{return}} @param@throwspublicvoidconfigure(HttpSecurityhttp)throwsException }}如上圖:因?yàn)槲⒎?wù)之間并沒有傳遞頭文件,所以我們可以定義一個(gè)器,每次微服務(wù)調(diào)用之前都檢查下頭文件,將請(qǐng)求的頭文件中的令牌數(shù)據(jù)再放入到headr中,再調(diào)用其他微服務(wù)即可。publicclassFeignInterceptorimplementsRequestInterceptorpublicvoid te te)RequestAttributesrequestAttributes=ifHttpServletRequestrequest=((ServletRequestAttributes)ifEnumeration<String>headerNames=request.getHeaderNames();if(headerNames!=null){whileStringheaderName=headerNames.nex if(headerName.equals("authorization")){StringheaderValue=request.getHead

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論