首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rest调用从Heroku角前端调用到heroku springboot引导后端时返回403

Rest调用从Heroku角前端调用到heroku springboot引导后端时返回403
EN

Stack Overflow用户
提问于 2020-01-02 06:31:01
回答 1查看 984关注 0票数 0

我在Heroku上开发了一个简单的后端。

  • 如果我将前端部署到本地环境中,那么rest调用就可以了。如果我从本地或Heroku前端bash运行
  • ,rest调用是可以的。如果我使用apitester.com对后端进行rest调用,则rest调用是可以的,包括选项
  • ,如果我将前端部署到Heroku,则rest调用会给我status=403错误。在Heroku上部署角应用程序,以便对另一个Heroku服务器进行rest调用时,是否需要做一些特殊的工作?

我已尝试禁用csrf,如How to Solve 403 Error in Spring Boot Post Request接受的答案所建议的那样。@十字路口也添加到所有rest控制器中。

任何帮助都非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-02 10:13:29

跨原点,有问题寄存器CrossFilter Bean,也可以使用Access-Control-Allow-Origin或注册站点,您可以检查以下链接here

代码语言:javascript
复制
@Configruation
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

public String crossOriginAllowedHeaders="header1,header2, *" ;
public String crossOriginAllowedSites="site1,site2, * ";

  @Override
    protected void configure(HttpSecurity http) throws Exception {
     )
      http.cors()
           .and()
           .csrf()
           .disable();

             ............
             ...........
        .and()
             .headers()
             .frameOptions()
             .sameOrigin().addHeaderWriter((request,response)->{
                                                 response.setHeader("Cache-Control","no-cache, no-store, max-age=0, must-revalidate, private");
                                                 response.setHeader("Pragma","no-cache");
                                                 response.setHeader("Access-Control-Allow-Origin",this.crossOriginAllowedSites);
                                             })



}


 @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    protected CorsFilter crossFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(false);
        config.setAllowedHeaders(Arrays.asList(crossOriginAllowedHeaders.split(",")));
        config.setAllowedOrigins(Arrays.asList(crossOriginAllowedSites.split(",")));
        //config.setAllowedHeaders("*"); whitelist all sites
        //config.setAllowedOrigins("*"); whitelist all headers
        config.addAllowedMethod(HttpMethod.OPTIONS);
        config.addAllowedMethod(HttpMethod.GET);
        config.addAllowedMethod(HttpMethod.POST);
        config.addAllowedMethod(HttpMethod.PUT);
        config.addAllowedMethod(HttpMethod.DELETE);
        config.addExposedHeader("Authorization");
        config.setMaxAge(new Long(1800));

        source.registerCorsConfiguration("/api/**", config);
        source.registerCorsConfiguration(MANAGEMENT, config);
        source.registerCorsConfiguration("/v2/api-docs", config);
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }

}

我希望这能起作用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59559179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档