我有类似的网址
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 安全性的grails 2.2.1。我试图通过requestmap解决它
def structureMap2 = Requestmap.findByUrl("contacts/contactProfile/*") ?: new Requestmap(url: "contacts/contactProfile/*",configAttribute: "ROLE_COMPANY").save(failOnError:true)
但是没有用。
如果我必须重组URL,我该怎么做。或者还有另一种方法。谢谢。
最佳答案
好吧,我会在Contacts/contactProfile
Controller 方法中做到这一点。
检查访问它的用户是否有权打开它。如果是,则呈现页面,否则,则返回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")
}
}
假设您已将公司分配给用户,并且联系人也有为其创建的公司,则将其写入。
关于url - 如何在grails中阻止对特定URL的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42307295/