gpt4 book ai didi

Spring - 如何保护 RESTful 私有(private)资源?

转载 作者:行者123 更新时间:2023-12-04 02:13:55 25 4
gpt4 key购买 nike

我有一些 RESTful 服务,使用 Spring MVC 实现,公开了一组资源。我已经使用基于 HTTPBasicAuthentication 和 HTTPS 的身份验证。某些资源必须仅供某些用户访问。

例如,我希望 URI /users/{userid}/photos 中的所有子资源只能由用户 userid 访问。实际上在我的应用程序中,所有经过身份验证的用户都可以访问它们。除了 userid 之外,我如何保护它们免受其他用户的侵害?如果我只想允许一部分用户(例如 userid 的 friend )访问此资源怎么办?

最佳答案

你可以在你的方法上做这样的事情:

    @ResponseBody
@RequestMapping(value = "/users/{userid}/photos", method = RequestMethod.GET)
@Secured(value = {"userid"})
public ResponseEntity<ModelMap> getPhotos(....) throws Exception {

如果以后需要,您可以添加更多用户

    @Secured(value = {"ROLE_ADMIN", "userid"})

关于Spring - 如何保护 RESTful 私有(private)资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14260135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com