Spring Repository PreAuthorize 给出“无法评估表达式”错误

use*_*618 0 spring spring-security

我添加了很多粗体,因为有人降级了我的问题,我认为这很奇怪......

我从这个工作开始,这意味着 @PreAuthorize 的配置正确...

@RestController
@RequestMapping('/people')
public PersonController extends BaseController {
   @PreAuthorize("#pesonId != principal.id")
   @RequestMapping(value="updatePerson", method={RequestMethod.POST}, produces = MediaType.APPLICATION_JSON_VALUE)
   public @ResponseBody SimpleResponseStatus updatePerson(@RequestParam(personId) final Long personId, @RequestParam(value) final String value, final HttpServerRequest request, final HttpServletResponse response)
   {
      Person p = personRepo.findById(personId);
      p.setValue(value);
      personRepo.save(p);
   }
}
Run Code Online (Sandbox Code Playgroud)

并移至此不起作用...存储库中的@PreAuthorize save()...

public interface PersonRepository extends JpaRepository<Person,Long> {

  @SuppressWarnings("unchecked")
  @Override
  @PreAuthorize("#p.id != principal.id")
  Person save(person p);

}
Run Code Online (Sandbox Code Playgroud)

现在我得到一个“无法评估表达式'#p.id!=principal.id'

在控制器上工作时的一个区别是我做了#personId而不是#p.id,所以我不知道表达式中的对象与基元是否是问题,或者控制器与存储库(我在其中进行评估)是否有问题是问题所在。

所以我有几个问题...

  1. 我是否需要做一些特殊的事情才能使预授权在存储库中正常工作?

  2. 与 Spring 安全无关,但为什么我被迫添加 SuppressWarnings?我可以看看我是否会回来,List<Person>但我觉得这很奇怪。

  3. 还有另一个例子,我想要执行一个 PreAuthorize 表达式,如“#p.group.id != 3”...评估中的级别是否有限制?即级别 = obj.obj.obj.obj.value

另一个有趣的事情是,当我让它与控制器一起工作时,我不需要大括号“#{userId != 3}”,但它与“#userId != 3”一起工作,我从这里得到了该语法

最重要的是,我让它在控制器中工作,但没有对象参数,现在我需要它在存储库中工作并带有对象参数。我尝试过 #person.id != 3 以及 #{person.id != 3} 但都不起作用。

use*_*618 5

我找到了我自己问题的答案: 这里

这基本上对于存储库来说,您必须通过注释添加参数名称,因为调试未编译到接口中。

我花了很长时间才终于找到答案,因为我尝试了不同的 EL 语法,最后我选择的一种语法给了我一个不同的(更好的)错误消息,从那里我找到了上面的链接。

不管怎样,无论是谁降级了我的问题,都应该发布我上面刚刚做的链接,而不是降级我。这真的很卑鄙。

public interface PersonRepository extends JpaRepository<Person,Long> {

  @SuppressWarnings("unchecked")
  @Override
  @PreAuthorize("#p.id != principal.id")
  Person save(@Param("p") person p);  //the @Param annotation is needed!

}
Run Code Online (Sandbox Code Playgroud)

另外,有趣的是,我看到有些地方需要 {},而另一些地方则不需要。我不需要牙套就能做到这一点。