Herr Derb
Herr Derb

Reputation: 5367

@WithMockUser: Use a custom GrantedAuthority class instead of SimpleGrantedAuthority

I'm building a permission based access control with hierarchically permissions for a spring project. For this, I'm extending the GrantedAuthority object to a Permission. A custom PermissionVoter will finally draw benefits out of this, as the single permission parts can be retrieved. This works as expected. The only problem are my integration tests.

With the annotation @WithMockUser I can mock a security context. By default this mocking method will create SimpleGrantedAuthoritys. Expecting Permissions in my voter, this obviously is not going to work. Can I configure @WithMockUser to use my custom GrantedAuthority class?

PS. I'm intentionally not using Shiro.

Upvotes: 3

Views: 836

Answers (1)

mrkernelpanic
mrkernelpanic

Reputation: 4451

You could instead use @WithUserDetailswhich takes a userDetailsService as parameter e.g. userDetailsServiceBeanName = "mockedUserDetailsService"and a User ("value")

You can then mock your Users and their GrantedAuthorities with the mocked UserDetailsService.

Upvotes: 6

Related Questions