Skip to content
Advertisement

Why this API is forbidden for an user having a JWT token containing the “correct” authority?

I am not so into Spring Security and JWT token and I have the following doubt on a project which I am working on.

Basically I have this SecurityConfiguration class containing my Spring Security configuration, as you can see it is intended to handle JWT token:

JavaScript

As you can see in the previous code I have the following two matcher lists:

JavaScript

At the moment the both contains the same API path: /api/users/email/**. This because my original idea was that this API should be avaiable for simple users and admin users.

Then in the code you can find the following matcher definition based on the authorities contained into the generated token:

JavaScript

(the USER_MATCHER is related to the CLIENT authority that, at the moment, is the simplest type of operation that can be performed…please don’t pay too much attention to the authority’s name…these are mainly some example then I will better define my authority list).

So doing in this way I expected that this /api/users/email/ API must be enabled both for an user having the ADMIN authority but also for an user having the CLIENT authority.

But it seems not to be true, doing an example. I generate a token for an user having the ADMIN authority, something like this:

JavaScript

using https://jwt.io/ website you can see that this token have the ADMIN authority:

JavaScript

So I use this token to call my target API (/api/users/email/) and I am obtaining what I expect:

enter image description here

Ok, now I generate a brand new JWT token for another user of my system having only the CLIENT authority. It generate something like this:

JavaScript

As usual, using https://jwt.io/, I can check that it contains this authority and infact here it is:

JavaScript

So now I use this newtoken to call my target API (/api/users/email/) but the API is not accessible by this user:

enter image description here

As you can see using this token the API access seems to be forbidden.

Why if in my configuration I specified that the API defined into the USER_MATCHER list (so the previous target API) should be accessible also by the user having a token containing the CLIENT authority?

What is wrong? Or what am I missing in the authority definition logic?

Advertisement

Answer

it sounds like you would like the /api/users/email/ endpoint to be accessible by both CLIENT & ADMIN

instead of .antMatchers(ADMIN_MATCHER).hasAnyAuthority("ADMIN")

try .antMatchers(ADMIN_MATCHER).hasAnyAuthority("ADMIN", "CLIENT)

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