You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@shiro.apache.org by Luke Biddell <lu...@gmail.com> on 2014/01/16 12:11:23 UTC

Get the HttpServletRequest associated with the current subject

I'm looking for ways of getting the current request for the current subject
in a custom implementation of a Shiro cache manager and cache.

I've looked at the return of SecurityUtils.getSubject and I notice that if
the subject is a WebDelegatingSubject, I can cast and obtain the request.

However, some invocations only provide a DelegatingSubject.

Is there an official way of getting hold of the request? Am I on the right
lines?

TIA

Luke

Re: Get the HttpServletRequest associated with the current subject

Posted by Luke Biddell <lu...@gmail.com>.
Thanks Les.

I've got it to dump a stack trace and a call to my cache is coming from:

AbstractShiroFilter.doFilterInternal()
    final Subject subject = createSubject(request, response);

Which ultimately pulls through the session manager to resolve the session
and eventually hits my cache.

So it's before subject.execute which I'm assuming binds the context to the
thread.

Luke


On 16 January 2014 20:06, Les Hazlewood <lh...@apache.org> wrote:

> Hi Luke,
>
> That's correct - if the instance is a WebDelegatingSubject, you can
> obtain the request that way.  You can use Shiro's WebUtils for this:
>
> HttpServletRequest request = WebUtils.getHttpRequest(subject);
>
> This will return iff the subject is a WebDelegatingSubject however.
> You would see a DelegatingSubject if you're not operating in the
> context of a request thread.  Are you seeing DelegatingSubject even on
> a request thread?
>
> Cheers,
> --
> Les Hazlewood | @lhazlewood
> CTO, Stormpath | http://stormpath.com | @goStormpath | 888.391.5282
>
>
> On Thu, Jan 16, 2014 at 3:11 AM, Luke Biddell <lu...@gmail.com>
> wrote:
> > I'm looking for ways of getting the current request for the current
> subject
> > in a custom implementation of a Shiro cache manager and cache.
> >
> > I've looked at the return of SecurityUtils.getSubject and I notice that
> if
> > the subject is a WebDelegatingSubject, I can cast and obtain the request.
> >
> > However, some invocations only provide a DelegatingSubject.
> >
> > Is there an official way of getting hold of the request? Am I on the
> right
> > lines?
> >
> > TIA
> >
> > Luke
>

Re: Get the HttpServletRequest associated with the current subject

Posted by Les Hazlewood <lh...@apache.org>.
Hi Luke,

That's correct - if the instance is a WebDelegatingSubject, you can
obtain the request that way.  You can use Shiro's WebUtils for this:

HttpServletRequest request = WebUtils.getHttpRequest(subject);

This will return iff the subject is a WebDelegatingSubject however.
You would see a DelegatingSubject if you're not operating in the
context of a request thread.  Are you seeing DelegatingSubject even on
a request thread?

Cheers,
--
Les Hazlewood | @lhazlewood
CTO, Stormpath | http://stormpath.com | @goStormpath | 888.391.5282


On Thu, Jan 16, 2014 at 3:11 AM, Luke Biddell <lu...@gmail.com> wrote:
> I'm looking for ways of getting the current request for the current subject
> in a custom implementation of a Shiro cache manager and cache.
>
> I've looked at the return of SecurityUtils.getSubject and I notice that if
> the subject is a WebDelegatingSubject, I can cast and obtain the request.
>
> However, some invocations only provide a DelegatingSubject.
>
> Is there an official way of getting hold of the request? Am I on the right
> lines?
>
> TIA
>
> Luke