Skip to content

Grails 3.0: When app has context path, url mappings only working for '/context/' with trailing slash when spring security implemented. #9024

@kschmit90

Description

@kschmit90

Simply, I have a standard Spring Security authentication configuration, which works fine when I am not using a context path for the server.

environments:
    development:
        server: 
            contextPath: '/context'

When I add a context path, my assumption is that a URL like 'localhost:8080/context' should map to whatever is mapped to "/" in UrlMappings. In this instance it is the index action of a controller.

class UrlMappings {

  static mappings = {
    "/$controller/$action?/$id?(.$format)?"{
      constraints {
        // apply constraints here
      }
    }

    "/"(controller:'myController', action:'index')
    "500"(view:'/error')
    "404"(view:'/notFound')
  }
}

This is true when not using Spring Security to place a log in form in front of whatever URL is being requested.

The security configuration looks like this. It is very basic, block some resources, permit viewing assets, then make sure everything else is authenticated and place a log in form in front of not authenticated requests.

class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/assets/**").permitAll()
        .antMatchers("/category/**").hasRole("ADMIN")
        .antMatchers("/subCategory/**").hasRole("ADMIN")
        .antMatchers("/audience/**").hasRole("ADMIN")
        .antMatchers("/location/**").hasRole("ADMIN")
        .anyRequest()
        .authenticated()
        .and()
      .formLogin()
        .loginPage('/authentication/login')
        .permitAll()
        .and()
      .logout()
        .logoutUrl("/authentication/logoutRedirect")
        .invalidateHttpSession(true)
        .permitAll()
  }

The issue occurs when there is a context path. If a URL like 'localhost:8080/context' is used, and authentication is successful, the application does NOT proceed to "/"(controller:'knowledgeAsset', action:'index') as expected.

However, when a URL like 'localhost:8080/context/' with the explicitly added trailing slash is used, and authentication is successful, the application DOES proceed to "/"(controller:'knowledgeAsset', action:'index') as expected.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions