Skip to content
Advertisement

spring security hasAuthority(“SCOPE_xxx”) method not working with spring authorization server version 0.2.0

I have created an authorization server using the new spring authorization server module. I am able to get the token successfully but when I try to use the token against a protected endpoint with hasAuthority() I get forbidden 403 error. Below my pom.xml file

JavaScript

Below is my Authorization Server config

JavaScript

And this is my Security Config

JavaScript

Here is my user detail service

JavaScript

when using the token to make a request to an endpoint that requires authentication alone it succeeds but when I try using it on an endpoint that requires a role it fails.

What could be the issue?

Advertisement

Answer

Based on a brief discussion in comments to clarify your setup, it seems that you are configuring your authorization server as a regular secured application with the intention of using scopes as authorities. However, this is an incorrect usage of the authorization server.

Note: I have not seen any reference to an OAuth 2.0 client or resource server in your description, so I’m assuming you are trying to hit endpoints directly on the authorization server. If that’s not the case, let me know.

There are three applications involved in an OAuth 2.0 protected setup:

  1. Authorization server
  2. Resource server
  3. Client

Your configuration is only for #1 (as far as I can tell). The authorization server contains two filter chains and additionally a configuration for a single oauth client. The two filter chains do the following:

  1. Secure endpoints provided by the authorization server framework
  2. Secure the login endpoint(s) the user will interact with prior to using the authorization endpoint (/oauth2/authorize) to obtain an authorization code, which the client will later use to obtain an access token

The scopes you have configured would allow a user (resource owner) to grant an oauth client the ability to make a protected call to a resource server using an access token. Only when the client makes a call to a resource server will your configured scopes be used. When the user directly interacts with an endpoint on the authorization server using a browser, the configuration for form login is in play, which as I mentioned in comments, uses roles from your database.

See the SpringOne 2021 repository and presentation to understand how to take an application from an unsecured application to a secured one, and then see how we turn it into a resource server, which uses scopes as authorities.

The presentation demonstrates all three applications, though the focus is on the resource server, which matches closely what you are trying to accomplish with scopes as authorities.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement