【问题标题】:spring permission evaluator, sending just idspring 权限评估器,只发送 id
【发布时间】:2016-01-04 21:33:12
【问题描述】:

自定义权限评估器

    @Component
    public class EventWritePermissionEvaluator implements PermissionEvaluator{

        @Override
        public boolean hasPermission(Authentication authentication,
                Object targetDomainObject, Object permission) {
            return true;
        }

        @Override
        public boolean hasPermission(Authentication authentication,
                Serializable targetId, String targetType, Object permission) {
            return true;
        }

    }



    @PreAuthorize("hasPermission(#event,'write')")
    @RequestMapping(value="/events/{id}/start")
    @ResponseBody
    public Map<String, Object> eventStart(@RequestBody Event event, @PathVariable("id") int id, HttpServletRequest request, HttpServletResponse response) throws MessagingException
    {
        event.setId(id);
        return eventService.eventStart(event, request, response);
    }

在上面的示例中,我将事件对象发送给权限评估器,在它之前放置一个“#”。为什么 ”#”?我如何只发送 id 而不是对象?

【问题讨论】:

标签: spring spring-mvc spring-security


【解决方案1】:

对你来说,它看起来像:

@PreAuthorize("hasPermission(#id, 'com.company.product.Event', 'read')")
@RequestMapping(value="/events/{id}")
@ResponseBody
public Event getEvent(@PathVariable("id") int id)
{
    // get Event from dao
}

注意hasPermission 的额外参数,它应该是您的域模型类的名称(如果您自己实现 PermissionEvaluator,则可以自定义)。

【讨论】:

    猜你喜欢
    • 2017-10-27
    • 1970-01-01
    • 2014-01-28
    • 2018-02-17
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2020-09-07
    • 2015-10-15
    相关资源
    最近更新 更多