【问题标题】:Protect REST URL with same path and different http methods使用相同的路径和不同的 http 方法保护 REST URL
【发布时间】:2014-05-02 06:52:26
【问题描述】:

我有一个需要帮助的情况。我有四个具有相同路径和不同 http 方法的 REST URL

/api/users/** GET,POST,PUT,DELETE

我想使用 Shiro 来处理 PUP、POST、DELETE 并保持 GET 是匿名的。我配置了以下网址,但没有运气

/api/users/** =rest[user:update,user:delete,user:create]
/api/users/** =anon

【问题讨论】:

    标签: rest shiro


    【解决方案1】:

    也许你可以这样做:

    /api/users/**=rest[user]
    

    然后,这在某种程度上取决于您创建 REST API 的方式。使用 JAX-RS 实现,例如 Jersey,您可以执行以下操作:

    @Path("/api/users")
    public class SomeResource {
    
        @RequiresPermissions("user:read")
        @GET
        public Response getResource() {..}
    
        @RequiresPermissions("user:create")
        @PUT
        public Response putResource() {..}
    
        @RequiresPermissions("user:update")
        @POST
        public Response postResource() {..}
    
        @RequiresPermissions("user:delete")
        @DELETE
        public Response deleteResource() {..}
    }
    

    这是假设您使用基于注释的授权。您还可以使用 SecurityUtils.getSubject() 机制。

    【讨论】:

    • 谢谢阿里特拉。第一个建议 /api/users/**=rest[user] 不起作用。因为。它会使 GET 需要身份验证。我尝试使用注释。但它没有任何效果。四种资源不受保护
    • 你是否为注解添加了 aspectj 依赖项?
    猜你喜欢
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2021-04-22
    • 2016-08-31
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多