【发布时间】:2022-07-05 21:09:50
【问题描述】:
我有一个关于在我的应用程序中将产品列表限制为特定用户的问题。我有一个 API:“/api/v1/{userId}/products”,我想在我已经在 AdminRestController 中使用过的 UserRestController 中使用分页:
@GetMapping
public Response<Page<Product>> getProductPage(@PageableDefault(sort = "id") Pageable pageable) {
return Response.ok(productService.findAll(pageable));
}
我已经阅读了一些主题并找到了一些使用“@PreAuthorize("#userId == authentication.principal.id")”的解决方案。现在,我想在 UserRestController 的端点中实现分页,它应该只返回与特定用户相关的产品列表(而不是所有产品的列表)。我尝试使用以下内容:
@GetMapping("/api/v1/{userId}/products")
@PreAuthorize("#userId == authentication.principal.id")
public Response<Page<Product>> getProductPage(@PageableDefault(sort = "id") Pageable pageable) {
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return Response.ok(productService.findAll(pageable));
}
但是我遇到了访问问题,你能帮我弄清楚吗?
提前致谢!
【问题讨论】:
标签: java spring spring-boot spring-security pagination