简体   繁体   English

在Play Framework中同时使用CRUD和Secure模块

[英]Using both CRUD and Secure module in Play Framework

I'm wondering how to use CRUD with Secure module without securing the entire controller. 我想知道如何在不保护整个控制器的情况下将CRUD与安全模块一起使用。

I have a BlogPost model with a controller named BlogPosts. 我有一个BlogPost模型,其控制器名为BlogPosts。 Since I want the administrators to be able to use the CRUD back-office to create, update and delete posts, the controller extends CRUD and uses Secure : 由于我希望管理员能够使用CRUD后台创建,更新和删除帖子,因此控制器扩展了CRUD并使用了Secure:

@With(Secure.class)
public class BlogPosts extends CRUD {

}

But now I want to list the blog posts on the home page. 但现在我想列出主页上的博客文章。 I cannot use this controller since it is restricted to authenticated users. 我无法使用此控制器,因为它仅限于经过身份验证的用户。 And I do not want to create another controller. 而且我不想创建另一个控制器。

So what is the best way to do it? 那么最好的方法是什么?

You can write your own @Before method that calls the Security check for all methods except list . 您可以编写自己的@Before方法,为除list之外的所有方法调用Security检查。 Something like: 就像是:

   @Before(unless="list")
   public static void before() {
      // Do security check
   }

It won't be as handy as the annotation but it will work. 它不会像注释那样方便,但它会起作用。

See the documentation 请参阅文档

EDIT ON COMMENT TO CLARIFY 编辑对澄清的评论

I see the answer was slightly ambiguous. 我看到答案有点含糊不清。 The idea is to remove the @With() annotation and create your own local @Before method that will be executed on all methods except list . 我们的想法是删除@With()注释并创建自己的本地@Before方法,该方法将在 list 之外的所有方法上执行。 That method can then delegate the processing to Secure.before (as it is a static method with no params) 然后该方法可以将处理委托给Secure.before(因为它是一个没有参数的静态方法)

Now that I think about it, you could just add the unless restriction to the method in Secure class, it should work for this scenario although it means mixing some concepts (so I would not do it in my project). 现在我考虑一下,你可以在Secure类中添加unless对方法的限制,它应该适用于这种情况,虽然它意味着混合一些概念(所以我不会在我的项目中这样做)。

Just get the BlogPost items via JPA in your other controller: 只需通过另一个控制器中的JPA获取BlogPost项:

public static void listBlogs() {
    render(BlogPost.findAll());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM