Access to XMLHttpRequest at ‘域名1 ‘ from origin ‘域名2‘ has been blocked by CORS java跨域问题详细解决方案

文章介绍了前端遇到的CORS政策导致的请求被阻止问题,并提供了多种解决方案,包括在后端设置Access-Control-Allow-Origin头信息,在Tomcat的web.xml配置文件中禁用或允许特定HTTP方法,以及使用Java注解和Filter处理跨域请求。此外,还提到了反向代理(如Nginx)和JSONP作为替代的跨域访问方式。

实际前端F12问题
Access to XMLHttpRequest at ‘域名1 ‘ from origin ‘域名2‘ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
在这里插入图片描述

解决方案 :
1、tomcat/conf/web.xml 加上,表示禁用options方法

<security-constraint>
		<web-resource-collection>
			<http-method>OPTIONS</http-method>
		</web-resource-collection>
		<auth-constraint></auth-constraint>
</security-constraint>

在这里插入图片描述
扩展:
需要禁用的HTTP方法

<security-constraint>  
    <web-resource-collection>  
        <web-resource-name>acooly-default-policy</web-resource-name>
        <!-- 表示需要认证控制的URL -->
        <url-pattern>/*</url-pattern>  
        <!-- 以下多个是表示允许的http请求方法 -->
        <http-method>PUT</http-method>  
    	<http-method>DELETE</http-method
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值