-
Since I can't get around I thought to post question here. I want to use library to mock keycloak user token for API calls. Token is tored in header with
So the headers are null and so is the token. I tried following:
Perhaps you could instruct me a bit or point me in the direction where to look. I would much appreciate it. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I had missed that, sorry. In the output you pasted above, you get a 403 (forbidden) and not a 401 (unauthorized) which means that your request actually had authentication context but that spring-security denied access. A frequent cause for that is security being role based ( If your app uses something else than NullAuthoritiesMapper, you must either
Now an important note about Spring security in mocked requests: All the process of building a Spring As a consequence, this lib does neither of the following:
What happens instead is the lib building an Authentication instance based on annotations (or requests post-processors) and directly populating => do not try to find headers in mock requests, just fetch Authentication from Spring SecurityContext. Regarding usage, please refer to
|
Beta Was this translation helpful? Give feedback.
I had missed that, sorry.
In the output you pasted above, you get a 403 (forbidden) and not a 401 (unauthorized) which means that your request actually had authentication context but that spring-security denied access. A frequent cause for that is security being role based (
hasRole('DRIVER')
as opposed tohasAuthority('DRIVER')
) andGrantedAuthoritiesMapper
not being exposed as a@Bean
.If your app uses something else than NullAuthoritiesMapper, you must either
@WithMockKeycloakAuth(authorities = { "ROLE_DRIVER" }
)Now an important note about Spring securi…