Spring Boot Rest控制器上的@RolesAllowed与request.isUserInRole()

时间:2018-07-18 15:38:50

标签: spring-boot spring-security

我目前正在研究Spring Boot Rest Controller,想知道我应该使用哪种形式的角色检查以及这两者之间有什么区别。

选项1:

    @RequestMapping(value = "/getmypage", method = RequestMethod.GET)
    @RolesAllowed({"TEST_ROLE"}) 
    public String  getMessage(Model model )  {

      //my code goes here

      }

选项2:

@RequestMapping(value = "/getmypage", method = RequestMethod.GET)
    public String  getMessage(Model model,  HttpServletRequest request )  {

   if (request.isUserInRole("TEST_ROLE")){
        //my code goes here
      }
   }

1 个答案:

答案 0 :(得分:1)

我认为在控制器或服务中以编程方式检查安全约束不是一个好主意。 AOP试图实现的最重要的事情之一就是将Cross Cutting Conscerns与业务逻辑分开,使您的代码更具可维护性,并且不允许它们分散在您的代码中。

因此,我认为最好选择Option1。