大佬教程收集整理的这篇文章主要介绍了Spring Boot + Angular 代理 - 403,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在设置一个小应用程序来测试各种事物时,我遇到了一个我无法解决或解释的奇怪行为。
我的 Spring Boot(2.4.2 版)应用程序公开了一个休息端点 /user。 我已将上下文路径设置为 /API(通过在 application.propertIEs 中设置 server.servlet.context-path=/API)。
我已尝试使用此配置禁用 CORS、CSRF、..:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@OverrIDe
protected voID configure(httpSecurity httpSecurity) throws Exception {
httpSecurity
.cors().disable()
.cors().disable()
.httpBasic().disable()
.formLogin().disable()
.authorizerequests()
.antMatchers("/user/**").permitAll()
.anyrequest().permitAll();
}
}
为了公开端点,我创建了这个控制器:
@RestController
public class UserController {
@GetMapPing("/user")
public User getUser() {
return User.builder().name("George").build();
}
}
我的端点在 http://localhost:8080/API/user 上可用,并按预期提供以下结果:
{"name":"George"}
为了在 angular 11 应用程序中测试一些东西,我创建了一个代理并在每次使用 npm start 时附加它。
代理配置:
{
"/API": {
"target": "http://localhost:8080","secure": false,"logLevel": "deBUG","changeOrigin": "true"
}
}
在我的浏览器中,每当我调用端点时(通过端口 4200 直接输入 url),我都会得到 403。尝试访问组件中的 API 也会给出这个 403:
ngOnInit(): voID {
this.http.get("/API/greeTing").subscribe(resp => {});
}
但是,在我的日志中,我看到 angular 应用程序正确地代理到后端: [HPM] GET /API/user -> http://localhost:8080
如果我从 spring boot 应用程序中删除上下文路径 (/API) 并使用 / 代替,我什至在将代理更改为后得到 404:
{
"/API": {
"target": "http://localhost:8080","pathRewrite": {"^/API" : ""}
}
}
我确实认为这与 CORS/CSRF 有关,但我无法解释上述行为。 我的问题是:我做错了什么,如何避免 4200 角端口上的 403/404?
谢谢,
科恩
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的Spring Boot + Angular 代理 - 403全部内容,希望文章能够帮你解决Spring Boot + Angular 代理 - 403所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。