代码之家  ›  专栏  ›  技术社区  ›  pyb

Spring MockMvc不包含Cookie

  •  5
  • pyb  · 技术社区  · 7 年前

    我将应用程序设置为使用会话cookies,它可以正常工作。

    然而,当我使用MockMvc测试控制器时,成功的身份验证不会给我任何cookie。

    Web配置:

    package world.pyb.spring.cookiesdemo;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
    import org.springframework.security.config.http.SessionCreationPolicy;
    
    @Configuration
    @EnableWebMvcSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("admin").password("argentina").roles("USER");
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //@formatter:off
            http.httpBasic()
                    .and().authorizeRequests().antMatchers(HttpMethod.GET, "/hello").authenticated()
                    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
            //@formatter:on
        }
    }
    

    package world.pyb.spring.cookiesdemo;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @RequestMapping("/hello")
        public String index() {
            return "Greetings from Spring Boot!";
        }
    
    }
    

    不给我会话cookie的简单控制器测试:

    package world.pyb.spring.cookiesdemo;
    
    import static org.hamcrest.Matchers.equalTo;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.cookie;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    public class HelloControllerTest {
    
        @Autowired
        private MockMvc mvc;
    
        @Test
        public void getHello() throws Exception {
            mvc.perform(MockMvcRequestBuilders.get("/hello")
                .with(SecurityMockMvcRequestPostProcessors.httpBasic("admin", "argentina"))
        )
            // prints "Cookies = []"
            .andDo(MockMvcResultHandlers.print())
            .andExpect(cookie().exists("JSESSIONID"))
            .andExpect(status().is2xxSuccessful())
            .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
        }
    
    }
    

    相关问题:

    1 回复  |  直到 7 年前
        1
  •  5
  •   Brent Bradburn    7 年前

    如问题代码所示, MockMvcResultMatcher 包括对Cookie的支持。

    测试在真实HTTP服务器上运行的控制器,这是测试安全层Cookie所需的。

    TestRestTemplate ,它在其完整的服务器上下文中调用控制器,提供了更全面的测试环境。

    但是请注意,从Spring 5开始,运行服务器API测试的新方法是基于 WebTestClient .