相关疑难解决方法(0)

如何使用spring 3.2新的mvc测试登录用户

这工作正常,直到我必须测试需要登录用户的服务,如何将用户添加到上下文:

@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 junit spring spring-mvc

20
推荐指数
2
解决办法
2万
查看次数

RestTemplate客户端与cookie

我正在用Java编写一个简单的客户端,以允许可重用​​使用通过RESTful API访问的专有病毒扫描软件.要上传用于扫描API的文件,需要使用POSTfor Connect,然后使用POSTfor将文件发布到服务器.在对Connect的响应中,POST服务器设置了cookie,后者需要在POST发布文件时使用.我目前正在RestTemplate我的客户端使用Spring .

我的问题是如何访问响应中的cookie以便随后转发回服务器POST?我可以看到它们存在于返回的标题中,但是没有方法ResponseEntity可以访问它们.

java cookies spring http resttemplate

15
推荐指数
5
解决办法
4万
查看次数

Spring MVC测试(安全集成测试),JSESSIONID不存在

我为我的春季启动应用程序创建了自定义登录表单.在我的表单集成测试中,我想检查收到的cookie是否包含JSESSIONIDXSRF-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)

spring spring-security spring-mvc-test spring-boot mockmvc

7
推荐指数
1
解决办法
2023
查看次数

Spring MockMvc不包含Cookie

我有一个受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的简单控制器测试: …

testing cookies spring-mvc spring-security

3
推荐指数
1
解决办法
2649
查看次数