后端验证用户修改的商品是否属于当前登录用户分两个验证
一个是根据用户修改的商品数据中的商家信息和当前登录的商家信息进行对比
另一个是根据用户修改的商品信息中的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;
}