【问题标题】:Reason: CORS header ‘Access-Control-Allow-Origin’ missing / Spring Boot原因:缺少 CORS 标头“Access-Control-Allow-Origin”/Spring Boot
【发布时间】:2021-01-25 16:37:13
【问题描述】:

我有一个基本的 Spring Boot Rest 应用程序和 Angular 应用程序。 (使用智威汤逊)

但是,由于这个错误,我无法发出任何请求:(即使我在响应头中添加了"Access-Control-Allow-Origin", "*"

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/api/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/api/login. (Reason: CORS request did not succeed).

这里是安全配置:

public class AuthTokenFilter extends OncePerRequestFilter {
// ...
}

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        // securedEnabled = true,
        // jsr250Enabled = true,
        prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors().and().csrf().disable()
                .authorizeRequests()
                .antMatchers(WebUrls.API.API + WebUrls.LOGIN.LOGIN,
                        WebUrls.API.API + WebUrls.LOGIN.REGISTER,
                        WebUrls.API.API + WebUrls.LOGIN.REFRESH_TOKEN)
                .permitAll()
                .anyRequest().authenticated();
        http.addFilterBefore(authJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
}

这里是我添加 cors 标头的地方:

@Component
public class MyCorsFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "*");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

【问题讨论】:

  • 作为一般经验法则,如果有一些安全措施阻止您,而您不理解它,那么最后您应该做的事情是谷歌搜索“如何我要禁用这个我不明白的东西吗?
  • 你能试试这个吗? public class MyCorsFilter extends OncePerRequestFilter
  • @wak786 这不起作用

标签: java spring spring-boot spring-security


【解决方案1】:

请尝试以下配置以使其正常工作。这使得 CORS 请求可以从任何来源发送到应用程序中的任何端点。

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
 
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }
}

【讨论】:

  • 我删除了MyCorsFilter 并创建了这个类并且它可以工作,但我不明白我的解决方案与您提供的解决方案之间的区别。 MyCorsFilter 还将添加 CORS 标头 以响应任何端点?
  • 在 Spring boot 中,WebMvcConfigurer 接口为 CORS Mapping 提供了一个抽象实现,因此我们必须提供我们的 impl。实现 CORS 政策。您添加的是过滤器而不是配置。 spring 容器需要配置 CORS。
  • 感谢 Kapil,我的最后一个问题是因为我们在这一行禁用了 cors `http.cors().and().csrf().disable()`,所以我们不需要像这样设置registry.addMapping("/**").allowedOrigins("*"); ?
  • 正确,您不必设置那个。引导提供了多种方式来做到这一点。所以,这是其中一种方式。
猜你喜欢
  • 2020-11-12
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2018-03-28
  • 2021-11-10
  • 2019-02-07
  • 2017-01-09
  • 2021-07-15
相关资源
最近更新 更多