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

时间:2019-06-24 08:38:22

标签: grails spring-security

我正在将Grails 3.3.9与spring security core插件3.2.3结合使用来保护网站。

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

如何确保如果不存在页面,则返回404 NOT FOUND 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')

    }

1 个答案:

答案 0 :(得分:-1)

你需要一个控制器

 select to_date('sysdate') from dual;
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")
    }

}