ctomek
ctomek

Reputation: 1786

@WithMockUser without annotation

How can I run every line of code with different role in Spring security unit test? When using @WithMockUser it is only possible to run entire method with chosen authority, but I want to run every line of code with different authority so I would need a no annotation solution.

Upvotes: 2

Views: 1980

Answers (1)

ctomek
ctomek

Reputation: 1786

Based on that question: How to perform RunAs using method security with Spring MVC 3.2 and Spring Security 3.1

this seems to work:

SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken(null, null, Arrays.asList(new SimpleGrantedAuthority("ROLE_XYZ"))));

Upvotes: 3

Related Questions