apache / apache/grails-core

Unable to get controller/action info from regular java filter (Spring Security Voter) if URL Mapping is dynamic

Open
#11,333 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Task List

- [x] Steps to reproduce provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

I'm trying to migrate existing Spring security Voter from Grails 2.3.5 to Grails 3.3.3. I have some specific security rules, that rely on controller/action names. So I need to retrieve this information inside Voter using **grailsUrlMappingsHolder**.
For some reason, **grailsUrlMappingsHolder** doesn't match default (dynamic) URL's in new Grails.
### Steps to Reproduce

1. Create a new Grails project.
2. Add Spring Security plugin and implement a simple Voter
```
@Override
int vote(Authentication authentication, Object object, Collection collection) {
grailsUrlMappingsHolder.match('/explicit/')//Works
grailsUrlMappingsHolder.match('/default/') //Doesn't work

return 1
}
```
3. Create 2 controllers: DefaultController and ExplicitController
Default controller (just to make sure that mappings work fine in controllers):
```
def index() {
grailsUrlMappingsHolder.match('/explicit/') //Works
grailsUrlMappingsHolder.match('/default/') //Works
}
```

4. Edit UrlMappings:
```
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/explicit" (controller: "explicit")
```

### Expected Behaviour

Url mappings holder should match **/default/** URL in Spring Security Voter. This worked in Grails 2.3.5

### Actual Behaviour

Voter matches **/explicit/** URL but fails for **/default/** URL with:

> Unable to establish controller name to dispatch for [org.grails.web.mapping.RegexUrlMapping$1@47724723]. Dynamic closure invocation returned null. Check your mapping file is correct, when assigning the controller name as a request parameter it cannot be an optional token!

### Environment Information

- **Operating System**: Windows 10
- **Grails Version:** 3.3.3
- **JDK Version:** 1.8.161
- **Container Version (If Applicable):** Tomcat

### Example Application

- https://github.com/tarasrng/MappingsIssue

Contributor guide

Open the contributing guide

Research direction

Start with the linked MappingsIssue example and reproduce the difference between the grailsUrlMappingsHolder.match calls in the Spring Security Voter and controller. Inspect the UrlMappings entry for the dynamic and explicit routes; done means the voter matches /default/ without the dispatch error while /explicit/ continues to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, spring
Domain
authorization, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.