【问题标题】:How can I get the jsessionid from a response using react js?如何使用 react js 从响应中获取 jsessionid?
【发布时间】:2020-04-18 17:58:48
【问题描述】:

上下文

  • 春季启动
  • React Js

问题

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

下面是从 React JSSpring 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 Securtiy 配置类

@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

【问题讨论】:

    标签: reactjs spring-boot cookies request jsessionid


    【解决方案1】:

    您可以从 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));
    

    【讨论】:

    • 感谢您的回答!因此,如果我尝试将其解析为 json,我会收到此错误 SyntaxError: Unexpected token < in JSON at position 0
    • 我已经更新了答案,请看一下。我无法检查代码,所以请告诉我结果如何。
    • 试试credentials: 'same-origin'
    • 它工作但没有凭据:'include' 但现在的问题是我总是必须将数据作为 url 搜索参数传递。我想把它放在体内。
    • 使用body: JSON.stringify(this.state.student) 而不是body: new URLSearchParams(student),你的状态应该是这样的:user:{ username: 'user1', password: '123' }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2016-05-01
    • 1970-01-01
    • 2018-12-30
    • 2021-10-27
    • 2020-09-17
    • 2017-12-28
    相关资源
    最近更新 更多