Spring boot 弹簧靴&x2B;spring安全性-如何在应用层阻止CORS请求?

Spring boot 弹簧靴&x2B;spring安全性-如何在应用层阻止CORS请求?,spring-boot,spring-security,cors,Spring Boot,Spring Security,Cors,我有一个带spring security的spring启动应用程序。当来自不同主机的任何客户端从我的应用程序调用API方法时,他会看到异常: Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:8088' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header

我有一个带spring security的spring启动应用程序。当来自不同主机的任何客户端从我的应用程序调用API方法时,他会看到异常:

Access to XMLHttpRequest at 'http://localhost:8080/test' from origin 'http://localhost:8088' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
但是我的控制器仍然接受请求。我想禁用它。我希望spring security拒绝这些请求

我试过这个:

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

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
        return source;
    }

我想我还不了解全部情况,我想了解:如何在应用层阻止CORS请求?因为默认情况下,应用程序处理请求,而浏览器只是阻止响应。

字面上的答案是检查请求是否有
源文件
标题,如果有,则拒绝该标题,但这并不能解决真正的问题

攻击者仍然可以欺骗用户发出跨源请求,但他们不能使用Ajax。他们仍然可以(例如)将表单提交到隐藏的iframe并启动相同类型的恶意请求

同源策略仅适用于防止攻击者通过第三方读取数据。这不能阻止他们提出要求。(CORS只允许有选择地禁用同源策略)

如果您想要防御请求中的有效负载,那么您需要对请求实施适当的防御

通常,这将使上一页正文中出现的令牌与会话中的匹配数据相匹配。同源策略可防止攻击者从上一页读取令牌

因为您使用的是Spring,所以请使用它的