後端驗證用戶修改的商品是否屬於當前登錄用戶

後端驗證用戶修改的商品是否屬於當前登錄用戶分兩個驗證
一個是根據用戶修改的商品數據中的商家信息和當前登錄的商家信息進行對比
另一個是根據用戶修改的商品信息中的id,在數據庫中進行查詢,將查詢出來數據中的商家信息和當前登錄信息進行對比

@RequestMapping("/update")
public Result update(@RequestBody Goods goods){
	//驗證商品是否是當前商家的id
	Goods goods2 = goodsService.findOne(goods.getGoods().getId());
	//從spring security中獲取商家id
	String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();
	//如果傳遞過來的商家ID並不是當前登錄的用戶的ID,則屬於非法操作
	if (!goods2.getGoods().getSellerId().equals(sellerId) || !goods.getGoods().getSellerId().equals(sellerId)){
		return new Result(false, "操作非法");
	}
	try {
		goodsService.update(goods);
		return new Result(true, "修改成功");
	} catch (Exception e) {
		e.printStackTrace();
		return new Result(false, "修改失敗");
	}
}
public Goods findOne(Long id){
	Goods goods = new Goods();
	goods.setGoods(goodsMapper.selectByPrimaryKey(id));
	goods.setGoodsDesc(goodsDescMapper.selectByPrimaryKey(id));
	TbItemExample example = new TbItemExample();
	TbItemExample.Criteria criteria = example.createCriteria();
	criteria.andGoodsIdEqualTo(id);
	goods.setItemList(itemMapper.selectByExample(example));
	return goods;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章