如何允许某些用户仅在春季启动 /弹簧安全性的端点中访问自己的数据?
我有一个问题,该问题与应用程序列表限制给我的应用程序中的特定用户有关。我得到了一个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(” @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));
}
但是我有访问问题,您能帮我弄清楚吗?
提前致谢!
I have a question related to the limiting of the products list to specific User in my application. Ive got an API: "/api/v1/{userId}/products" and I want to use pagination in my UserRestController which I have already used in AdminRestController:
@GetMapping
public Response<Page<Product>> getProductPage(@PageableDefault(sort = "id") Pageable pageable) {
return Response.ok(productService.findAll(pageable));
}
I have read some threads and find some solutions with "@PreAuthorize("#userId == authentication.principal.id")". Now, I want to implement pagination in my endpoint in UserRestController which should return only the products list related to the specific User (not the list of all products). I have tried to use the following:
@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));
}
But I have got the access problem, could you help me to figure out?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它已经在
Spring-Secutiry
和spring-data
中实现。在config中,您需要添加@bean以将您的
principal
在queriing中提供:之后,您将要编写类似的内容:
另外,请阅读官方文档: https://docs.spring.io/spring-security/reference/features/features/integrations/data.html
It is already implemented into
Spring-Secutiry
andSpring-Data
.In config, you need to add a @Bean to provide your
principal
into the queriing :After that you'll be abble to write things like that :
Also, read the official doc : https://docs.spring.io/spring-security/reference/features/integrations/data.html