Preamble
I am designing an API gateway for the Grails microservice federation. This problem seems to be related to a bunch of problems already filed in this repository , but nothing gives a solution.
Versions and Configurations
Grails: 3.2.2
Tomcat: 8.5
Plugin version:
compile 'org.grails.plugins:spring-security-core:3.1.2' compile "org.grails.plugins:spring-security-rest:2.0.0.M2"
I use spring security plugin to check only token. I do the authorization part myself, returning ROLE_NO_ROLES for all users in getAuthorities() . I intercept all requests and allow access based on my own authorization scheme stored in the database.
Problem:
With these configurations and strategies, my code works as needed when I run it on my local system. When I deploy it to the server as a war file in tomcat, it works fine for all requests to the gateway, i.e. For all requests of the template /umm/controller/action . spring there is a security context, and the user is well rated.
When I try to call other microservices by redirecting with requests of the form /umm/microservice/controller/action , springSecurityService.getCurrentUser() and springSecurityService?.principal?.username , it will return null. Although my token is rated excellent, I am not getting any security context.
See issue for more details . Details for reproducing the error are also indicated in the above problem. The whole project is available here .
Update: May 19, 2017
I tried to deploy my war on Tomcat on my local machine. This question and question provides the following solutions.
- disable tomcat cache
- setting
grails.plugin.springsecurity.sch.strategyName = org.springframework.security.core.context.SecurityContextHolder.MODE_INHERITABLETHREADLOCAL
Nothing is working yet. SecurityContextHolder returns null anyway. All custom SpringSecurityService return functions, namely. getCurrentUser() , getPrincipal() , getAuthentication() and loadCurrentUser() returns null.
Update: May 23, 2017
To narrow down the problem, I carried out an offline war using
java -Dgrails.env=prod -jar build/libs/mywar-0.1.war
Now for any request other than umm, I get 404, page not found . I think the problem is with the production environment . The application works great in development.
Also tried grails run-app , which works great. To eliminate the problem with the production environment, I created a war using grails dev war , but to no avail. So far, nothing is working for war .
Update: May 25, 2017
I should probably ask this http://security.stackexchange.com , but for the record I also ask for it here.
The answer below provides a fix to the workaround. The mechanism by which the fix works is explained in the answer. My question is:
- Does this approach provide any security vulnerability or loophole?
- Is this authorization scheme secure or needs to be reviewed?
- I authenticate through the plugin but allow myself. Can someone bypass the security filters and directly get into the authorization interceptor? Because if someone can do this, he will need to give me the administrator name in the same format as the token, and he will have access to everything.