【发布时间】:2020-04-18 17:58:48
【问题描述】:
上下文
- 春季启动
- 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 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