を削除します。私は2つのエンティティUser
とAd
を持っています.1人のユーザーが複数の広告を投稿でき、1人のユーザーのみで1つの広告を作成できるため、削除オプションを実行しようとしています安全ではありますが、それでも脆弱です。ユーザーは自分の広告ではない広告を削除することができます。つまり、彼はオーナーではありません。 私がしたアプローチは、ad
を削除する前に、私はid
の広告所有者がセッションに接続されている人のid
と等しいかどうかを確認しています。今私は、その検証のトリックなしで私が従うことができるより良いアプローチがあるかどうか尋ねています。私のウェブアプリケーションで
広告コントローラー
@RequestMapping(value="/delete_ad/{id_ad}", method=RequestMethod.GET)
public String doDeleteAd(@PathVariable("id_ad") Long id_ad, RedirectAttributes redirectAttributes) {
Ad ad = adService.findAdById(id_ad);
if (ad == null) {
redirectAttributes.addFlashAttribute("alert", "alert-danger");
redirectAttributes.addFlashAttribute("messageDelete","<strong>Oops !</strong> no Ad founded with that id");
return "redirect:/myads";
}else {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetail = (UserDetails) auth.getPrincipal();
User u = (User) userDetail;
if (ad.getPublisher().getId_user() == u.getId_user()) {
adService.deleteAdById(id_ad);
redirectAttributes.addFlashAttribute("alert", "alert-success");
redirectAttributes.addFlashAttribute("messageDelete", "<strong>Done !</strong> Ad deleted succesfully");
return "redirect:/myads";
}else {
redirectAttributes.addFlashAttribute("alert", "alert-info");
redirectAttributes.addFlashAttribute("messageDelete", "<strong>Opps !</strong> you are not the owner of the ad");
return "redirect:/myads";
}
}
}
とmy_ads
ビューで、私は、各広告一般に
<a href="delete_ad/${ad.id_ad }" class="btn btn-sm btn-danger" >delete</a>
フロントエンドの誰も実際にDBからIDを見ることができないように、ハッシュされたIDを使用することを検討することもできますが、これは既に実装されているものと一緒に使用されます –
@BenLonsdaleそのトークンに基づいて削除を実行する私のスキーマ! – saul
@スール - あなたが持っているものは大丈夫で、従来の方法です。アクションを実行する前に、1つのIDを取り、そのIDがユーザーに関連付けられていることを確認します。 – andre3wap