Unit testing with Spring Security

后端 未结 11 2175
暖寄归人
暖寄归人 2020-11-29 15:19

My company has been evaluating Spring MVC to determine if we should use it in one of our next projects. So far I love what I\'ve seen, and right now I\'m taking a look at th

11条回答
  •  [愿得一人]
    2020-11-29 15:52

    After quite a lot of work I was able to reproduce the desired behavior. I had emulated the login through MockMvc. It is too heavy for most unit tests but helpful for integration tests.

    Of course I am willing to see those new features in Spring Security 4.0 that will make our testing easier.

    package [myPackage]
    
    import static org.junit.Assert.*;
    
    import javax.inject.Inject;
    import javax.servlet.http.HttpSession;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.experimental.runners.Enclosed;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.security.core.context.SecurityContext;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.security.web.FilterChainProxy;
    import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    import org.springframework.web.context.WebApplicationContext;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
    import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
    
    @ContextConfiguration(locations={[my config file locations]})
    @WebAppConfiguration
    @RunWith(SpringJUnit4ClassRunner.class)
    public static class getUserConfigurationTester{
    
        private MockMvc mockMvc;
    
        @Autowired
        private FilterChainProxy springSecurityFilterChain;
    
        @Autowired
        private MockHttpServletRequest request;
    
        @Autowired
        private WebApplicationContext webappContext;
    
        @Before  
        public void init() {  
            mockMvc = MockMvcBuilders.webAppContextSetup(webappContext)
                        .addFilters(springSecurityFilterChain)
                        .build();
        }  
    
    
        @Test
        public void testTwoReads() throws Exception{                        
    
        HttpSession session  = mockMvc.perform(post("/j_spring_security_check")
                            .param("j_username", "admin_001")
                            .param("j_password", "secret007"))
                            .andDo(print())
                            .andExpect(status().isMovedTemporarily())
                            .andExpect(redirectedUrl("/index"))
                            .andReturn()
                            .getRequest()
                            .getSession();
    
        request.setSession(session);
    
        SecurityContext securityContext = (SecurityContext)   session.getAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
    
        SecurityContextHolder.setContext(securityContext);
    
            // Your test goes here. User is logged with 
    }
    

提交回复
热议问题