我正在制作一个简单的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