Lah*_*age 1 cors spring-boot angular
我正在尝试 通过angular http call从spring boot服务器获取数据到angular应用程序。另外,我还为Spring Boot应用程序添加了安全性依赖性。
当我尝试使用浏览器时,它会来。
在Intellij控制台中输入密码后,它将显示响应。
当我尝试使用有角度的http调用时,它失败并显示关于主题的错误。
我的角度组件具有以下代码。
constructor(private http: HttpClient) {
http.get('http://localhost:8080/resource').subscribe(data => this.greeting = data);
}
Run Code Online (Sandbox Code Playgroud)
我的Spring Boot应用程序具有以下代码
@RequestMapping("/resource")
public Map<String,Object> home() {
Map<String,Object> model = new HashMap<String,Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello World");
return model;
}
Run Code Online (Sandbox Code Playgroud)
我也阅读了有关跨域资源共享(CORS)的信息,但目前尚不清楚如何将其应用于角度。
我的错误是:
我在angular应用程序中所做的操作是正确的,并且只需要更正我的spring boot应用程序。
我所做的唯一更改是创建名为WebSecurityConfig的配置文件。
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
UrlBasedCorsConfigurationSource source = new
UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6880 次 |
| 最近记录: |