HTTP——跨域相关问题

背景:毕业设计,用C++做HTTP server

跨域导致的问题:

1,直接访问链接可以访问成功,在ajax中,返回状态为200,却跳到error所属的function中,下面代码数据结果为0,0,error

error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert(XMLHttpRequest.status);
				alert(XMLHttpRequest.readyState);
				alert(textStatus);
				window.location.href="404/404.html"
			},

2,debug可以看出,拦截跨域请求

跨域解决方法

static std::string header(std::string type, std::string Lenth){
        std::string header{"HTTP/1.1 200 OK\r\nContent-Type: "+type+"\nCache-Control: no-cache\nContent-Length: "+Lenth+"\n"
                      "Access-Control-Allow-Origin: http://localhost:443\nAccess-Control-Allow-Credentials: true\n"
                      "Access-Control-Allow-Methods:GET,POST,PUT,POST\nAccess-Control-Allow-Headers:x-requested-with,content-type\n"};
        return header;
    }
    static std::string sendMsg(std::string type, std::string &content, bool cookie = false, std::string cookieValue=""){
        std::string all{};
        all += header(type, std::to_string(content.length()));
        if (cookie == true) {
            all += "Set-Cookie:phone=" + cookieValue + "; domain=localhost; path=/\n";
        }
        all += "\n";
        all += content;
        all += "\r\n";
        return all;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Security中配置有多种方法。一种常见的方法是使用@CrossOrigin注解或重写addCorsMappings方法来配置,但是当项目中引入了Spring Security依赖后,这种配置方式可能会失效。 为了解决这个问题,可以使用Spring Security提供的更专业的方案。首先,需要创建一个继承自WebSecurityConfigurerAdapter的配置类,并重写configure方法。在configure方法中,可以通过调用HttpSecurity对象的cors方法来启用配置。 在cors方法中,可以通过CorsConfigurationSource对象的configurationSource方法来配置具体的设置。可以使用CorsConfiguration对象来设置允许的请求头、请求方法和请求来源。此外,还可以设置预检请求的缓存时间。最后,需要将CorsConfiguration对象注册到UrlBasedCorsConfigurationSource对象中。 下面是一个示例的配置类的代码: ```java @Configuration public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .mvcMatchers("/hello1").permitAll() .anyRequest().authenticated() .and() .formLogin() .and() .cors() // 配置 .configurationSource(configurationSource()); } CorsConfigurationSource configurationSource() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowedHeaders(Collections.singletonList("*")); corsConfiguration.setAllowedMethods(Collections.singletonList("*")); corsConfiguration.setAllowedOrigins(Collections.singletonList("*")); corsConfiguration.setMaxAge(3600L); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration); return source; } } ``` 通过以上配置,Spring Security会自动应用配置,并且保持其他安全配置不受影响。这种方式可以确保配置在Spring Security过滤器之前生效,避免了配置失效的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Spring Security(七) ——配置](https://blog.csdn.net/tongkongyu/article/details/125982927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值