如何在spring引导应用程序中按用户筛选数据



我正在制作一个简单的spring引导应用程序,我想通过登录用户或用户会话合并过滤数据。但是我不知道如何最好地设置应用程序,使每个用户都可以访问特定于他的数据。我已经把认证和授权作为app的一部分。

我也在研究这种应用程序,我已经通过将参数@AuthenticationPrincipal CustomUserDetails userDetails添加到想要获得userId的方法来实现这一点。然后拨打:Integer userId = userDetails.getUserId();

例如:

@GetMapping("/demo")
public String showTemplate(@AuthenticationPrincipal CustomUserDetails userDetails) {
Integer userId = userDetails.getUserId();
return "index";
}

然后你可以用这个userId做任何你想做的事情。例如,在存储库findByUserId

中创建方法

最新更新