springsecurity +vue 跨域
时间: 2023-09-24 20:08:43 浏览: 88
如何解决?
要解决springsecurity vue跨域问题,你可以在Spring Security配置类中添加如下配置:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// ...
http.cors();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.addAllowedOrigin("*");
configuration.addAllowedMethod("*");
configuration.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
同时,在Vue项目中,需要在axios发送请求时设置withCredentials为true,例如:
axios.defaults.withCredentials = true;
这样就能够解决springsecurity vue跨域问题了。
阅读全文