2

谁能建议一个可行的解决方案来防止直接访问 Grails 上的 *.gsp 页面?

在查看了拦截“/**.gsp”之后,我发现无法使用它,因为它不仅会过滤掉直接访问,还会过滤掉来自控制器的页面渲染等。

我尝试在 UrlMapping.groovy 中设置以下内容,即使我可以阻止 *.gsp 直接访问,但我也弄乱了页面的导航;然后所有链接似乎都转到主页。

    "/**.gsp" {
isEligible = {
    System.err.println("ALL PARAMS: " + params)
   request.requestURL.toString().endsWith(".gsp")
}
controller = {
    if (request.requestURL.toString().endsWith(".gsp")) {
        "public"
    } else {
        "*"
    }
}
action = {
    if (request.requestURL.toString().endsWith(".gsp")) {
        "home"
    } else {
        "*"
    }
}
}

曾经我考虑过像 org.springframework.web.filter.OncePerRequestFilter 这样的设置过滤器,但不太确定如何定义它,因为 Grails 倾向于自行生成 web.xml 过滤器部分。

有什么想法吗?

非常感谢!汤姆

4

4 回答 4

1

将这些添加到 UrlMappings:

"/**.gsp" {
    controller = {
        if(request.requestURL.toString().endsWith(".gsp")) {
            "forbidden"
        } else params.controller
    }
}

并创建一个 ForbiddenController 和一个 index.gsp 并带有“永远不要考虑直接访问 GSP,老兄”。作为它的内容。

干杯。

于 2011-05-31T06:44:18.333 回答
1

不幸的是,我没有找到 UrlMappings 的解决方案。这是一个有点难看的解决方案,但如果您在所有页面上使用相同的布局(例如 main.gsp),您可以将此行添加到布局(main.gsp)中。

    <%  if (request.requestURL.toString().endsWith(".gsp")) {
       response.sendRedirect("${request.contextPath}/")
    } %>

这样,如果用户尝试直接访问 gsp 页面,他将被重定向到主页。

也许不是最好的解决方案,但到目前为止确实对我有用。

欢呼

于 2011-03-07T16:35:34.167 回答
0

according to the grails FAQ the "/**.gsp" configuration in the UrlMapping.groovy should work.

couldn't try it out yet.

How did you add the links to the page ? Are the links also broken when you use the link tag ?

<g:link controller="book" action="list">Book List</g:link>
于 2011-03-07T11:55:56.343 回答
0

编写一个将在每个请求上执行的过滤器怎么样?

于 2011-03-09T09:17:42.040 回答