这工作正常,直到我必须测试需要登录用户的服务,如何将用户添加到上下文:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@WebAppConfiguration
public class FooTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Resource(name = "aService")
private AService aService; //uses logged in user
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.webApplicationContext).build();
}
Run Code Online (Sandbox Code Playgroud) 我正在用Java编写一个简单的客户端,以允许可重用使用通过RESTful API访问的专有病毒扫描软件.要上传用于扫描API的文件,需要使用POST
for Connect,然后使用POST
for将文件发布到服务器.在对Connect的响应中,POST
服务器设置了cookie,后者需要在POST
发布文件时使用.我目前正在RestTemplate
我的客户端使用Spring .
我的问题是如何访问响应中的cookie以便随后转发回服务器POST
?我可以看到它们存在于返回的标题中,但是没有方法ResponseEntity
可以访问它们.
我为我的春季启动应用程序创建了自定义登录表单.在我的表单集成测试中,我想检查收到的cookie是否包含JSESSIONID和XSRF-TOKEN.
但是,我只收到了XSRF-TOKEN.
这是我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class UserIT {
@Autowired
private WebApplicationContext context;
@Autowired
private FilterChainProxy springSecurityFilterChain;
@Value("${local.server.port}")
private Integer port;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc =
MockMvcBuilders.webAppContextSetup(context).addFilters(springSecurityFilterChain)
.build();
}
@Test
public void getUserInfoTest() throws Exception {
disableSslVerification();
MvcResult result =
mockMvc.perform(formLogin("/login").user("roy").password("spring")).andExpect(authenticated())
.andReturn();
Cookie sessionId = result.getResponse().getCookie("JSESSIONID");
Cookie token = result.getResponse().getCookie("XSRF-TOKEN");
}
Run Code Online (Sandbox Code Playgroud)
安全配置:
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
//.httpBasic()
//.and() …
Run Code Online (Sandbox Code Playgroud) 我有一个受HTTP Basic身份验证保护的控制器。
我将应用程序设置为使用会话cookie,并且可以正常工作。
但是,当我使用MockMvc测试控制器时,成功的身份验证不会给我任何cookie。
网页配置:
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
}
}
Run Code Online (Sandbox Code Playgroud)
简单的控制器:
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!";
}
}
Run Code Online (Sandbox Code Playgroud)
不给我会话cookie的简单控制器测试: …
spring ×3
cookies ×2
java ×2
spring-mvc ×2
http ×1
junit ×1
mockmvc ×1
resttemplate ×1
spring-boot ×1
testing ×1