如何使用 react js 从响应中获取 jsessionid?

How can I get the jsessionid from a response using react js?

上下文

问题

我想从 react 发出登录请求以获取 jsessionid。我从 Spring Boot 收到奇怪的回复。在回复中我没有找到任何 cookie。在邮递员中,我可以只在 url 中提供用户名和密码作为参数,在响应中,我得到一个带有 cookie jsessionid 的响应,对于更多请求,我可以使用它。但是在 react 中,我得到了一个奇怪的响应,我不知道如何获取 cookie。

这是将请求从 React JS 发送到 Spring Boot:

的代码
const { username, password } = this.state;
    const student = { username, password };
    fetch("http://localhost:8080/login", {
      method: "POST",
      body: new URLSearchParams(student)
    })
      .then(res => {
        console.log(res);
        const jsessionid = document.cookie;
        console.log("id", jsessionid);
        //Here I am trying to get the jsessionid
      })
      .catch(error => console.log(error));

This 是我得到的响应,并在控制台中打印出来

这是我的 Spring 安全配置 Class:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Autowired
    UserDetailsServiceImpl userDetailsService;

    @Bean
    DaoAuthenticationProvider authenticationProvider(){
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        return daoAuthenticationProvider;
    }



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable().cors().and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("*"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }



}

Here 我尝试使用 curl,如您所见,我得到了 cookie jsessionid .

您可以从 response.header 访问您的 jsessionid
例如

fetch("http://localhost:8080/login", {
  method: "POST",
  body: new URLSearchParams(student)
  credentials: 'include', 
  headers: {
  'Content-Type': 'application/json'
  } 
  })
  .then(res=>console.log(res.headers.get('set-cookie'));)

  .catch(error => console.log(error));