Inject @AuthenticationPrincipal when unit testing a Spring REST controller

后端 未结 4 1743
不知归路
不知归路 2021-01-03 23:04

I am having trouble trying to test a REST endpoint that receives an UserDetails as a parameter annotated with @AuthenticationPrincipal.

It

4条回答
  •  难免孤独
    2021-01-03 23:56

    For some reason Michael Piefel's solution didn't work for me so I came up with another one.

    First of all, create abstract configuration class:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @TestExecutionListeners({
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        WithSecurityContextTestExecutionListener.class})
    public abstract MockMvcTestPrototype {
    
        @Autowired
        protected WebApplicationContext context;
    
        protected MockMvc mockMvc;
    
        protected org.springframework.security.core.userdetails.User loggedUser;
    
        @Before
        public voivd setUp() {
             mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    
            loggedUser =  (User)  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        } 
    }
    

    Then you can write tests like this:

    public class SomeTestClass extends MockMvcTestPrototype {
    
        @Test
        @WithUserDetails("someUser@app.com")
        public void someTest() throws Exception {
            mockMvc.
                    perform(get("/api/someService")
                        .withUser(user(loggedUser)))
                    .andExpect(status().isOk());
    
        }
    }
    

    And @AuthenticationPrincipal should inject your own User class implementation into controller method

    public class SomeController {
    ...
        @RequestMapping(method = POST, value = "/update")
        public String update(UdateDto dto, @AuthenticationPrincipal CurrentUser user) {
            ...
            user.getUser(); // works like a charm!
           ...
        }
    }
    

提交回复
热议问题