'How to return 404 for pages that do not exist when using Grails Spring Security Plugin

I'm using Grails 3.3.9 with the spring security core plugin 3.2.3 to secure a website.

The problem I have is that now users receive a 403 for pages that do not exist instead of a 404.

How can I make sure that if a page does not exist, a 404 NOT FOUND http status code is returned?


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']
]

In my url mappings I have

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')

    }


Solution 1:[1]

u need a controller

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" }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lonyui