加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:33:00 所属栏目:大数据 来源:网络整理
导读:我为我的春季启动应用程序创建了自定义登录表单. 在我的表单集成测试中,我想检查收到的cookie是否包含JSESSIONID和XSRF-TOKEN. 但是,我只收到了XSRF-TOKEN. 这是我的测试: @RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes

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

安全配置:

@Override
    public void configure(HttpSecurity http) throws Exception {
        // @formatter:off   
        http
            //.httpBasic()
            //.and()
                .headers().frameOptions().disable()
            .and()
                .antMatcher("/**").authorizeRequests()
                .antMatchers("/actuator/health").permitAll()
                .antMatchers("/actuator/**").hasAuthority(Authority.Type.ROLE_ADMIN.getName())
                .antMatchers("/login**","/index.html","/home.html").permitAll()
                .anyRequest().authenticated()
            .and()
                .formLogin().loginPage("/login.jsp")
                    .usernameParameter("username")
                    .passwordParameter("password")
                    .loginProcessingUrl("/login")
                     .permitAll()
            .and()
                .logout().logoutSuccessUrl("/login.jsp").permitAll()
            .and()
                .csrf().csrfTokenRepository(csrfTokenRepository())
            .and()
                .addFilterAfter(csrfHeaderFilter(),CsrfFilter.class)
                .addFilterBefore(ssoFilter(),BasicAuthenticationFilter.class);
        // @formatter:on
    }

请帮助我获得所需的结果.

最佳答案
您也没有看到Set-Cookie标头.对我而言,这是MockMVC的一个很大的局限.有关解决方法,请参阅Why does Spring MockMvc result not contain a cookie?.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读