如何阻止访问grails中的特定URL

时间:2017-02-17 21:00:46

标签: url grails spring-security

我有网址喜欢 http://192.168.0.226:8080/crm/contacts/contactProfile/5

公司可以创建帐户然后创建联系人。它会生成URL中使用的联系人ID。任何公司都可以通过用

这样的随机数替换id来访问另一家公司的联系人

http://192.168.0.226:8080/crm/contacts/contactProfile/675

如何阻止一家公司访问另一家公司的联系人。而且如果id不在数据库中,它将显示错误。如果id不存在,如何重定向到404页面。

我正在使用带有spring security的grails 2.2.1。我尝试通过requestmap解决它

    def structureMap2 = Requestmap.findByUrl("contacts/contactProfile/*") ?: new Requestmap(url: "contacts/contactProfile/*",configAttribute: "ROLE_COMPANY").save(failOnError:true)

但它没有奏效。

如果我必须重新构建URL,我该怎么做。或者还有另一种方法。谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,我会在Contacts/contactProfile控制器方法中执行此操作。 如果访问它的用户有权打开它,请检查。如果是,则渲染页面,如果没有,则返回flash.error(或任何其他)并重定向到404.如下所示:

def contactProfile() {
    def user = = springSecurityService.currentUser
    def contact = Contact.get(params.id)
    if (!contact) {
        flash.error = "There is no such contact!"
        redirect(controller: "errors", action: "404")
    } else if (contact.company.id == user.company.id) {
        //create the stuff
        [contact: contact] //render the page
    } else {
        flash.error = "You are not authorised to view this contact!"
        redirect(controller: "errors", action: "404")
    }
}

这是假设您已将公司分配给用户并且联系人还拥有为其创建的公司而编写的。