Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何保护Spring数据REST关联请求?_Java_Spring_Spring Security_Spring Data Rest - Fatal编程技术网

Java 如何保护Spring数据REST关联请求?

Java 如何保护Spring数据REST关联请求?,java,spring,spring-security,spring-data-rest,Java,Spring,Spring Security,Spring Data Rest,我使用SpringDataREST创建了RESTAPI。我有实体User和Post,其中User可以有多个帖子(一对多)。现在我需要向我的用户添加帖子。但是我需要知道,userA不能删除或更新userB的帖子 Api结构 { "_links": { "users": { "href": "http://localhost:8081/api/users{?page,size,sort}", "templated": true

我使用SpringDataREST创建了RESTAPI。我有实体
User
Post
,其中
User
可以有多个帖子(一对多)。现在我需要向我的用户添加帖子。但是我需要知道,
userA
不能删除或更新
userB
的帖子

Api结构

{
    "_links": {
        "users": {
            "href": "http://localhost:8081/api/users{?page,size,sort}",
            "templated": true
        },
        "posts": {
            "href": "http://localhost:8081/api/posts{?page,size,sort}",
            "templated": true
        }
        "profile": {
            "href": "http://localhost:8081/api/profile"
        }
    }
}
{
    "id": 1,
    "username": null,
    "password": null,
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/users/1"
        },
        "user": {
            "href": "http://localhost:8081/api/users/1"
        },
        "posts": {
            "href": "http://localhost:8081/api/users/1/posts"
        }
    }
}
用户结构

{
    "_links": {
        "users": {
            "href": "http://localhost:8081/api/users{?page,size,sort}",
            "templated": true
        },
        "posts": {
            "href": "http://localhost:8081/api/posts{?page,size,sort}",
            "templated": true
        }
        "profile": {
            "href": "http://localhost:8081/api/profile"
        }
    }
}
{
    "id": 1,
    "username": null,
    "password": null,
    "_links": {
        "self": {
            "href": "http://localhost:8081/api/users/1"
        },
        "user": {
            "href": "http://localhost:8081/api/users/1"
        },
        "posts": {
            "href": "http://localhost:8081/api/users/1/posts"
        }
    }
}
有几种方法可以添加相关的实体抛出链接。 使用
PUT
方法和
text/uri列表
内容类型:

PUT /api/posts/1/user? HTTP/1.1
Host: localhost:8081
Content-Type: text/uri-list
Authorization: Bearer 270c6dc3-04a5-48cc-b42e-c275472df459
cache-control: no-cache
http://localhost:8081/api/users/1
但通过这种方式,我可以将任何URI添加到正文中,并将任何随机用户添加到随机帖子中,我认为,这里存在安全问题。 添加相关资源的下一个方法是将其添加到JSON中,如下所示:

PATCH /api/posts/1? HTTP/1.1
Host: localhost:8081
Content-Type: application/json
Authorization: Bearer 270c6dc3-04a5-48cc-b42e-c275472df459
cache-control: no-cache
{
    "user": "http://localhost:8081/api/users/1"
}
但是在这个方法中,同样的问题。任何用户都可以添加到任何帖子

现在我看到这个问题只有一个解决方案——定制rest存储库并检查添加的用户是否是当前经过身份验证的用户。

查看您的用例“只有用户负责发布CRUD操作”

是的,解决这个问题的一种方法是“定制rest存储库并检查添加的用户是否是当前经过身份验证的用户。”

假设你有春季安全

我建议您不要为您的帖子传递任何用户id,而是从安全上下文或令牌中的登录用户id获取用户

这样,您的帖子将在API级别独立于用户