0

我的 grails3.3.9应用程序有不同的模块,它们ROLE使用 spring security 实现了不同的基于访问。

AdminController 通过访问ROLE_ADMIN。如果非管理员用户登录并尝试访问,Admin URL则 grails 显示

[Image]Grails 错误消息:Sorry, you're not authorized to view this page.

我想显示我的自定义消息。

[Image: ]自定义消息:403 Access Forbidden

应用程序.groovy

grails.plugin.springsecurity.interceptUrlMap = [
        [pattern: '/user/**', access: ['IS_AUTHENTICATED_FULLY']],
        [pattern: '/role/**', access: ['ROLE_ADMIN']],
        [pattern: '/', access: ['IS_AUTHENTICATED_FULLY']],
        [pattern: '/admin/*', access: ['ROLE_ADMIN']]]

URL映射

"500"(controller: "error", action: "internalServerError")
"404"(controller: "error", action: "notFound")
"403" (controller: "error", action: "forbidden")
"403"(view: "/error/forbidden")

我分别为403.

500 & 404工作正常。

4

2 回答 2

2

在 URlMappings 中,您可以设置到响应代码的映射并选择一个控制器或视图来响应 - 例如:

static mappings = {
    // ... other mappings ...
    // Send 403's to a controller if you'd like to do additional logic
    "403"(controller: "errors", action: "forbidden")
    // Or just render a static view under grails-app/views/errors/forbidden.gsp
    "403"(view: "/errors/forbidden")
}

请参阅:http ://docs.grails.org/4.0.1/guide/single.html#mappingToResponseCodes

于 2020-02-18T14:11:16.417 回答
0

您还需要在 application.groovy 中设置以下配置。

grails.plugin.springsecurity.adh.errorPage = null //to throw 403 page on access denied
于 2021-06-16T18:51:15.190 回答