Skip to content
Advertisement

OAuth2 authorization code flow: spring-security does not accept the issued access_token

I am learning the OAuth2 authorization code flow.

  • I have my own Authorization Server (AS) which is OpenAM 7.1.
  • The Client is a simple Spring-Boot web application with a static HTML page, I use Spring-Security to protect the HTML page and control the Oauth2 flow.

I think that my Authorization Server configuration is correct because AS produces the access_token at the end when I simulate the communication with CURL. But somehow Spring-Security does not want to accept the issued and validated access token. So I think that my Spring-Security configuration is not correct.

I tried to configure Spring-Security in many different ways, but unfortunately, none of them was working. Maybe I need to implement the steps that I execute with CURL with Spring-Security, but maybe I just missed a configuration line.

This is the last step of my CURL chain where AS gives me the access token (exchange the authorization code for an access token):

JavaScript

This is the probe of the token (validate and retrieve information about the token):

JavaScript

Then I try to fetch the protected content with this CURL but Spring-Security redirects me again to the Spring Security’s provider selection page:

JavaScript

As you can see my web-app with Spring-Security does not accept the valid bearer token and redirects the request to the Authorization Server despite I provided the token.

This is my Spring-Security configuration:

JavaScript

And the Spring-Security config I use:

JavaScript

What did I miss? Could you please guide me in the right direction?

—-> ADDITIONAL INFO <—-

When I open the protected page https://web.example.com:8444/user.html in the web browser, then

  1. I am redirected properly to the Authorization Server login page.
  2. Then I log in.
  3. Then the consent approval form appears where I give access to the “public_profile” scope
  4. Then Spring redirects me again to the login page (step 2).

I think that happens because Spring just does not want to accept the issued access token.

Spring log:

JavaScript

Advertisement

Answer

I notice two issues in the code you have shared.

The first is that you may be confusing an OAuth 2.0 resource server and an OAuth 2.0 client.
The application running on web.example.com:8444 is configured as an OAuth 2.0 client.
However, you are making a request to web.example.com:8444, providing a bearer token and asking for a resource.
The client will not validate the bearer token. In this scenario it seems like you are treating the application as if it were a resource server.

If you are looking to create a resource server application, you can see the full documentation in the Spring Security reference.

The second issue is the behaviour you described when accessing the client in the browser.
The problem here is customising redirect-uri: https://web.example.com:8444/user.html.

When doing this you override the default redirect URI, which is /login/oauth2/callback/{registrationId}.

This URI is special because it prompts the OAuth2LoginAuthenticationFilter to process the request, attempt to authenticate the user and create the OAuth2AuthenticationToken.

When you customise redirect URI, the OAuth2LoginAuthenticationFilter is not invoked and the application does not know if the user is authenticated.

Advertisement