如何为使用 Grails Spring Security Plugin 时不存在的页面返回 404



我正在使用Grails 3.3.9和spring安全核心插件3.2.3来保护网站。

我遇到的问题是,现在用户会收到不存在的页面的 403 而不是 404。

如何确保如果页面不存在,则返回 404 未找到 http 状态代码?


grails.plugin.springsecurity.securityConfigType = "Annotation"
grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.website.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName = 'com.website.UserRole'
grails.plugin.springsecurity.authority.className = 'com.website.Role'
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
  [pattern: '/',               access: ['permitAll']],
    [pattern: '/error',          access: ['permitAll']],
  [pattern: '/404',            access: ['permitAll']],
    [pattern: '/index',          access: ['permitAll']],
    [pattern: '/index.gsp',      access: ['permitAll']],
    [pattern: '/shutdown',       access: ['permitAll']],
    [pattern: '/assets/**',      access: ['permitAll']],
    [pattern: '/**/js/**',       access: ['permitAll']],
    [pattern: '/**/css/**',      access: ['permitAll']],
    [pattern: '/**/images/**',   access: ['permitAll']],
    [pattern: '/**/favicon.ico', access: ['permitAll']],
        [pattern: '/**',   access: ['ROLE_SUPER_ADMIN']]
]
grails.plugin.springsecurity.filterChain.chainMap = [
    [pattern: '/**/js/**',       filters: 'none'],
    [pattern: '/**/css/**',      filters: 'none'],
    [pattern: '/**/images/**',   filters: 'none'],
    [pattern: '/**/favicon.ico', filters: 'none'],
    [pattern: '/**',             filters: 'JOINED_FILTERS']
]

在我的网址映射中,我有

class UrlMappings {

    static mappings = {

        name root: "/" (controller: "search", action: "create")

        // DO NOT DELETE, we need this for acJson and other actions.
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }
        "500"(view:'/error')
        "404"(view:'/404')
    }

你需要一个控制器

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{
            constraints {
                // apply constraints here
            }
        }
        "/"(controller: "portal", action: "index")
        "500"(controller: "error", action: "page500")
        "404"(controller: "error", action: "page404")
    }
}
class ErrorController {
    def page404() { render view: "page404" }
}

最新更新