当前位置: 首页 > 知识库问答 >
问题:

Spring引导:CORS问题

孙海
2023-03-14

我使用的是Spring Boot版本2.0.2Release。下面是我的安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        prePostEnabled = true,
        securedEnabled = true,
        jsr250Enabled = true)
@ComponentScan("com.mk")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationProvider myAuthenticationProvider;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.cors().configurationSource(corsConfigurationSource())
                .and()
                .csrf().disable()
                .anonymous().and()
                .authorizeRequests()
                .antMatchers(HttpMethod.GET,"/index.html").permitAll()
                .antMatchers(HttpMethod.POST,"/login").permitAll()
                .antMatchers(HttpMethod.GET,"*").authenticated()
                .and().httpBasic();
    }
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }

加载http://localhost:8080/myurl失败:对预飞行请求的响应未通过访问控制检查:请求的资源上没有“access-control-allow-origin”标头。因此,不允许访问源“http://localhost:4200”。响应的HTTP状态代码为403。

共有1个答案

傅峰
2023-03-14

虽然Spring security提供了一种在http Configureer中配置CORS的方法,但有一种更简单的方法可以将CORS过滤器添加到应用程序中-

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyCORSFilter implements Filter {


@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;

    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

    chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

按优先级最高的筛选器排序可以确保javax.servlet.filter的MyCORSFilter实现是链中的第一个。希望这有帮助

 类似资料:
  • 我的基本配置是: 我尝试了这些变量的不同组合,但仍然不起作用。有什么想法吗?

  • 我有一个带rest控制器的Spring启动应用程序和一个Angular应用程序作为前端。目前,它们都在本地主机上运行,SpringSecurity在Spring中启用。最初,由于Cors,我无法从Angular向Spring发出getRequest。我将@CrossOrigin添加到我的restContoller中,现在我可以执行从angular到Spring的Get请求。现在我在post请求中也

  • 我需要添加CORS过滤器到我的Spring Boot web应用程序。 我添加了CORS映射,如下文所述http://docs.spring.io/spring/docs/current/spring-framework-reference/html/CORS.html 这是我的配置:

  • 我有Spring Boot REST API和React基于CMS。 BasicWebSecurityConfigurerAdapter.kt 我也尝试在我的RestControllers上使用注释,但我遇到了同样的问题,GET请求可以工作,POST请求不能工作。我对Spring Boot还是相当陌生的,所以我肯定有些东西我遗漏了。

  • 我在spring cloud gateway应用程序中设置CORS配置时遇到了问题。我已经将以下配置设置为允许所有内容中的所有内容: 然而,当我调用endpoint到网关时,我会得到: null 以下是api gateway应用程序在调试时设置的日志(此处为完整日志):

  • “已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在‘访问控制允许来源’标头。” 当api被角度部分击中时获得此消息,但当邮递员击中endpoint时获得正确响应。