url - 如何在grails中阻止对特定URL的访问

标签 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 安全性的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/

相关文章:

mysql - 无法在 Grails 应用程序中更新 mysql 中的表

Java Spring Security - User.withDefaultPasswordEncoder() 已弃用?

java - 哪种 Spring 安全身份验证方案适合移动和 Web 应用程序的 REST API?

php - 如何重定向到我在表单中提交的页面(分页)

python - 使用 Python 和正则表达式查找正确的 URL

java - 如何通过传递 json URL 使 URL 中的空格可读

grails - 适用于业务逻辑的单独的Application Server vs Grails框架?

grails - 将记录器添加到 Grails 类

facebook - 使用Spring Security Facebook Grails插件重命名FacebookUser上的用户字段

javascript - 为什么 JavaScript 中 "decodeURIComponent"或 "decodeURI"无法将 "hello+world"解码为 "hello world"?