Announcement Announcement Module
No announcement yet.
OAuth2 never redirects. Never hits exception in OAuth2ClientContextFilter Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • OAuth2 never redirects. Never hits exception in OAuth2ClientContextFilter


    I've been looking at this for 2 days now and I am starting to really scratch my head.

    Here's the background history.
    - I wanted to integrate Spring Security OAuth into my site.
    - I started looking at the tonr2/sparklr2 example.
    - I configured my application with the configurations for sparklr2
    - I copied over the unit tests in sparklr2 and ran them for my application to verify that they all passed. (Gotta love tests!)
    - I then copied over tonr2 and started customizing it for my needs
    - I wanted my version of tonr2 to be a developer test bed that mobile developers could use to test on so they knew what to expect once they started coding on the iphone or android devices.
    - Everything compiles. Everything is configured the way I want it and everything is basically the same except that my version of tonr2 calls different remote services.
    - Problem is I get an exception like this:
    Problem Details
    Error: No OAuth 2 security context has been established. Unable to access resource 'foo'.
    Message: No OAuth 2 security context has been established. Unable to access resource 'lela'.
    - I started looking into this and stepped through the code and compared by stepping through your example code.
    - What I found was that there was a piece of code that never got called in my application but it did get called in your sample application:
    try {
          try {
            chain.doFilter(servletRequest, servletResponse);
          catch (Exception ex) {
            OAuth2ProtectedResourceDetails resourceThatNeedsAuthorization = checkForResourceThatNeedsAuthorization(ex);
            String neededResourceId = resourceThatNeedsAuthorization.getId();
    - That exception is not getting caught in my application and it's the exception necessary to initiate the redirect to "the sparklr" app to confirm access.

    - Here's what I have done that differs from your app:
    1. I am using a web app that is v2.5
    2. I am using @RequestMapping for my controllers
    3. I've tested on spring 3.0.5 and 3.1.M2 with the same results
    4. I am using Tiles

    In a nutshell, I pretty much upgraded the tonr2 app to be what a Spring Roo version of tonr2 would look like and be configured as. Latest and greatest in other words.

    The question is why isn't the exception being caught? It is certainly being thrown here:
    OAuth2AccessToken accessToken = accessTokens == null ? null : accessTokens.get(this.resource.getId());
        if (accessToken == null) {
              throw new OAuth2AccessTokenRequiredException("No OAuth 2 security context has been established. Unable to access resource '" + this.resource.getId() + "'.", resource);
    When I step through the code to the end I see that it goes through the OAuth2ClientContextFilter filter, but is already at the bottom of the method and never hit the exception. Both my service and controller throws an Exception but somewhere that exception is being gobbled up by someone before it is able to hit the exception and initiate the redirect.

    Any ideas what I am doing wrong here?

  • #2
    My web.xml:
    <web-app xmlns=""


    • #3
      Note to self:

      Don't define a bean org.springframework.web.servlet.handler.SimpleMapp ingExceptionResolver with a defaultErrorView.

      Works now.


      • #4
        Glad to know this got resolved.