You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Javid Jamae <ja...@gmail.com> on 2010/02/14 02:39:32 UTC
Issue with Grails / Shiro
I ran into an issue while running Grails 1.2.1 and Shiro for security. I'm
not sure if I'm doing something wrong, or if this is a bug, but it seems
like a bug. Please advise if and where I should report this.
1) grails create-app newapp
2) cd newapp
3) grails install-plugin shiro
4) grails quick-start
5) Create a controller called ErrorsController.groovy
class ErrorsController {
def serverError = {
render(view:"error")
}
def notFound = {
render(view:'notFound')
}
}
[Note: The error view already exists from when you generated the app, but
the notFound one doesn't. You can create the notFound view, but it won't
make a difference. ]
6) Change grails-app/conf/UrlMappings.groovy and add the following 404
definition -
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
"404"(controller:"errors", action:"notFound")
}
}
7) grails run-app
8) Go to
http://localhost:8080/newapp/asdf
and you'll get the following Stack Trace:
java.lang.IllegalStateException: No SecurityManager accessible to the
calling code, either bound to the org.apache.shiro.util.ThreadContext or as
a vm static singleton. This is an invalid application configuration.
at
org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:115)
at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:57)
at ShiroGrailsPlugin.accessControlMethod(ShiroGrailsPlugin.groovy:414)
at ShiroGrailsPlugin.accessControlMethod(ShiroGrailsPlugin.groovy)
at
ShiroGrailsPlugin$_closure3_closure19.doCall(ShiroGrailsPlugin.groovy:228)
at
SecurityFilters$_closure1_closure2_closure3.doCall(SecurityFilters.groovy:12)
at
SecurityFilters$_closure1_closure2_closure3.doCall(SecurityFilters.groovy)
at java.lang.Thread.run(Thread.java:619)
9) Now if you want to see things get nasty, add the following 500 definition
to you UrlMappings.groovy and watch the console:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
"/"(view:"/index")
"500"(controller:"errors", action:"serverError")
"404"(controller:"errors", action:"notFound")
}
}
--
View this message in context: http://n2.nabble.com/Issue-with-Grails-Shiro-tp4568683p4568683.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: Issue with Grails / Shiro
Posted by Les Hazlewood <lh...@apache.org>.
Sounds good - I just saw the commit. Thanks!
On Tue, Feb 23, 2010 at 1:28 AM, Peter Ledbrook <pe...@cacoethes.co.uk> wrote:
>> This is interesting. The OncePerRequestFilter was just copied from
>> Spring - did they not have the same issue then?
>
> Here's the latest code I just grabbed from src.springsource.org:
>
>
> public final void doFilter(ServletRequest request, ServletResponse
> response, FilterChain filterChain)
> throws ServletException, IOException {
>
> if (!(request instanceof HttpServletRequest) || !(response
> instanceof HttpServletResponse)) {
> throw new ServletException("OncePerRequestFilter just supports HTTP
> requests");
> }
> HttpServletRequest httpRequest = (HttpServletRequest) request;
> HttpServletResponse httpResponse = (HttpServletResponse) response;
>
> String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
> if (request.getAttribute(alreadyFilteredAttributeName) != null ||
> shouldNotFilter(httpRequest)) {
> // Proceed without invoking this filter...
> filterChain.doFilter(request, response);
> }
> else {
> // Do invoke this filter...
> request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
> try {
> doFilterInternal(httpRequest, httpResponse, filterChain);
> }
> finally {
> // Remove the "already filtered" request attribute for this request.
> request.removeAttribute(alreadyFilteredAttributeName);
> }
> }
> }
>
> Note that they now remove the request attribute. I'll add that.
>
> Cheers,
>
> Peter
>
Re: Issue with Grails / Shiro
Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
> This is interesting. The OncePerRequestFilter was just copied from
> Spring - did they not have the same issue then?
Here's the latest code I just grabbed from src.springsource.org:
public final void doFilter(ServletRequest request, ServletResponse
response, FilterChain filterChain)
throws ServletException, IOException {
if (!(request instanceof HttpServletRequest) || !(response
instanceof HttpServletResponse)) {
throw new ServletException("OncePerRequestFilter just supports HTTP
requests");
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
if (request.getAttribute(alreadyFilteredAttributeName) != null ||
shouldNotFilter(httpRequest)) {
// Proceed without invoking this filter...
filterChain.doFilter(request, response);
}
else {
// Do invoke this filter...
request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
try {
doFilterInternal(httpRequest, httpResponse, filterChain);
}
finally {
// Remove the "already filtered" request attribute for this request.
request.removeAttribute(alreadyFilteredAttributeName);
}
}
}
Note that they now remove the request attribute. I'll add that.
Cheers,
Peter
Re: Issue with Grails / Shiro
Posted by Les Hazlewood <lh...@apache.org>.
This is interesting. The OncePerRequestFilter was just copied from
Spring - did they not have the same issue then?
I'm certainly in favor of consistent behavior!
+1 to Peter's proposed change.
On Mon, Feb 22, 2010 at 3:45 PM, Peter Ledbrook <pe...@cacoethes.co.uk> wrote:
>>> It appears to be a Tomcat-specific issue :(
>>
>> This seems to be a problem with Shiro's OncePerRequestFilter and
>> Tomcat. The trouble is, Tomcat completes the execution of the filters
>> on the main request before then (apparently) forwarding to the error
>> handler. So before the error handler is invoked, the Shiro filter
>> clears the thread local variables, including the bound security
>> manager. The security manager is never bound again because the Shiro
>> filter extends OncePerRequestFilter which works out that this is still
>> the same request (it's a forward, you see).
>>
>> Is this incorrect behaviour in Tomcat? I have no idea. The servlet
>> specification does leave some holes, which means that it's not clear
>> what the correct behaviour should be. Note that Tomcat only appears to
>> perform a forward after completing the current request when it's
>> forwarding to an error handler.
>
> It seems that the servlet specification makes no guarantees in this
> area. Tomcat's and Jetty's approaches are both valid. One thing that
> is guaranteed by the spec is that the servlet container will pass the
> original *unwrapped* request and response to the error handler, unless
> there are any filters specifically attached to the ERROR dispatcher.
> That means Shiro needs fixing.
>
> I propose that we modify OncePerRequestFilter such that the *.FILTERED
> request attribute is removed straight after the call to
> doFilterInternal(). Users and the Grails plugin can then configure the
> Shiro filter to trigger on the ERROR dispatcher if they want.
> Admittedly the filter would no longer be once-per-request, but the
> behaviour would be consistent between Tomcat and Jetty.
>
> Cheers,
>
> Peter
>
Re: Issue with Grails / Shiro
Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
>> It appears to be a Tomcat-specific issue :(
>
> This seems to be a problem with Shiro's OncePerRequestFilter and
> Tomcat. The trouble is, Tomcat completes the execution of the filters
> on the main request before then (apparently) forwarding to the error
> handler. So before the error handler is invoked, the Shiro filter
> clears the thread local variables, including the bound security
> manager. The security manager is never bound again because the Shiro
> filter extends OncePerRequestFilter which works out that this is still
> the same request (it's a forward, you see).
>
> Is this incorrect behaviour in Tomcat? I have no idea. The servlet
> specification does leave some holes, which means that it's not clear
> what the correct behaviour should be. Note that Tomcat only appears to
> perform a forward after completing the current request when it's
> forwarding to an error handler.
It seems that the servlet specification makes no guarantees in this
area. Tomcat's and Jetty's approaches are both valid. One thing that
is guaranteed by the spec is that the servlet container will pass the
original *unwrapped* request and response to the error handler, unless
there are any filters specifically attached to the ERROR dispatcher.
That means Shiro needs fixing.
I propose that we modify OncePerRequestFilter such that the *.FILTERED
request attribute is removed straight after the call to
doFilterInternal(). Users and the Grails plugin can then configure the
Shiro filter to trigger on the ERROR dispatcher if they want.
Admittedly the filter would no longer be once-per-request, but the
behaviour would be consistent between Tomcat and Jetty.
Cheers,
Peter
Re: Issue with Grails / Shiro
Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
> It appears to be a Tomcat-specific issue :(
This seems to be a problem with Shiro's OncePerRequestFilter and
Tomcat. The trouble is, Tomcat completes the execution of the filters
on the main request before then (apparently) forwarding to the error
handler. So before the error handler is invoked, the Shiro filter
clears the thread local variables, including the bound security
manager. The security manager is never bound again because the Shiro
filter extends OncePerRequestFilter which works out that this is still
the same request (it's a forward, you see).
Is this incorrect behaviour in Tomcat? I have no idea. The servlet
specification does leave some holes, which means that it's not clear
what the correct behaviour should be. Note that Tomcat only appears to
perform a forward after completing the current request when it's
forwarding to an error handler.
Cheers,
Peter
Re: Issue with Grails / Shiro
Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
>> Any progress??
>
> I can't reproduce the problem.
It appears to be a Tomcat-specific issue :(
Re: Issue with Grails / Shiro
Posted by Peter Ledbrook <pe...@cacoethes.co.uk>.
> Any progress??
I can't reproduce the problem.
Peter
Re: Issue with Grails / Shiro
Posted by Lucas Simão <ls...@gmail.com>.
Any progress??
--
View this message in context: http://n2.nabble.com/Issue-with-Grails-Shiro-tp4568683p4576979.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: Issue with Grails / Shiro
Posted by Javid Jamae <ja...@gmail.com>.
Thanks for the feedback. I just stumbled upon the following issue in Grails,
so it looks like its a known thing:
http://jira.codehaus.org/browse/GRAILS-5234
I noticed that the filter mapping for the ShiroFilter didn't have the ERROR
dispatcher set, so I tried setting it, but it didn't seem to make a
difference.
<filter>
<filter-name>securityContextFilter</filter-name>
<filter-class>org.apache.shiro.spring.SpringShiroFilter</filter-class>
<init-param>
<param-name>securityManagerBeanName</param-name>
<param-value>shiroSecurityManagerProxy</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>securityContextFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
I'll just follow up with the issue tracker and see if they have made any
progress.
Regards,
Javid
--
View this message in context: http://n2.nabble.com/Issue-with-Grails-Shiro-tp4568683p4569367.html
Sent from the Shiro User mailing list archive at Nabble.com.
Re: Issue with Grails / Shiro
Posted by Kalle Korhonen <ka...@gmail.com>.
ShiroFilter isn't configured (one way or another). The assumption
seems to be that Grails configures this for you and as such, a better
place to report this is Grails user list (or a list specific to Shiro
integration with Grails if there's such a thing).
Kalle
On Sat, Feb 13, 2010 at 5:39 PM, Javid Jamae
<ja...@gmail.com> wrote:
>
> I ran into an issue while running Grails 1.2.1 and Shiro for security. I'm
> not sure if I'm doing something wrong, or if this is a bug, but it seems
> like a bug. Please advise if and where I should report this.
>
> 1) grails create-app newapp
> 2) cd newapp
> 3) grails install-plugin shiro
> 4) grails quick-start
>
> 5) Create a controller called ErrorsController.groovy
>
> class ErrorsController {
> def serverError = {
> render(view:"error")
> }
>
> def notFound = {
> render(view:'notFound')
> }
> }
>
> [Note: The error view already exists from when you generated the app, but
> the notFound one doesn't. You can create the notFound view, but it won't
> make a difference. ]
>
> 6) Change grails-app/conf/UrlMappings.groovy and add the following 404
> definition -
>
> class UrlMappings {
> static mappings = {
> "/$controller/$action?/$id?"{
> constraints {
> // apply constraints here
> }
> }
> "/"(view:"/index")
> "500"(view:'/error')
> "404"(controller:"errors", action:"notFound")
> }
> }
>
> 7) grails run-app
> 8) Go to
>
> http://localhost:8080/newapp/asdf
>
> and you'll get the following Stack Trace:
>
> java.lang.IllegalStateException: No SecurityManager accessible to the
> calling code, either bound to the org.apache.shiro.util.ThreadContext or as
> a vm static singleton. This is an invalid application configuration.
> at
> org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:115)
> at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:57)
> at ShiroGrailsPlugin.accessControlMethod(ShiroGrailsPlugin.groovy:414)
> at ShiroGrailsPlugin.accessControlMethod(ShiroGrailsPlugin.groovy)
> at
> ShiroGrailsPlugin$_closure3_closure19.doCall(ShiroGrailsPlugin.groovy:228)
> at
> SecurityFilters$_closure1_closure2_closure3.doCall(SecurityFilters.groovy:12)
> at
> SecurityFilters$_closure1_closure2_closure3.doCall(SecurityFilters.groovy)
> at java.lang.Thread.run(Thread.java:619)
>
> 9) Now if you want to see things get nasty, add the following 500 definition
> to you UrlMappings.groovy and watch the console:
>
> class UrlMappings {
> static mappings = {
> "/$controller/$action?/$id?"{
> constraints {
> // apply constraints here
> }
> }
> "/"(view:"/index")
> "500"(controller:"errors", action:"serverError")
> "404"(controller:"errors", action:"notFound")
> }
> }
>
> --
> View this message in context: http://n2.nabble.com/Issue-with-Grails-Shiro-tp4568683p4568683.html
> Sent from the Shiro User mailing list archive at Nabble.com.
>