启动Springboot项目,报错p://localhost/goods/select?id=1‘ from origin ‘null‘ has been blocked by CORS policy

报错:

p://localhost/goods/select?id=1' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

这个错误通常是由于浏览器的同源策略(Same Origin Policy)引起的。解决此问题的方法是在服务器端配置跨域资源共享(CORS)头信息。

在 Spring Boot 中,可以使用@CrossOrigin注解来为控制器方法添加跨域支持,或者在配置文件中添加全局 CORS 配置。以下是示例代码:

  1. 为控制器方法添加跨域支持。在控制器的方法上添加 @CrossOrigin(origins="*")注解即可。示例代码如下:
@RestController
public class MyController {
 
    @GetMapping("/myapi")
    @CrossOrigin(origins="*")
    public String handle() {
        // 处理请求逻辑
    }
}

在以上示例中,@CrossOrigin(origins="*") 注解允许来自任何域的请求访问该控制器方法。

  1. 全局 CORS 配置。在 Spring Boot 的配置文件 (例如 application.properties)中添加以下配置,以开启全局 CORS 支持:
# 允许任何域进行跨域访问
spring.mvc.cors.allowed-origins=*

以上配置允许任何域进行跨域访问,你也可以指定允许的域名,例如:

# 允许www.example.com域进行跨域访问
spring.mvc.cors.allowed-origins=https://www.example.com

以上两种方法都可以解决跨域问题。

注意:

有时必须在controller层添加

@CrossOrigin(origins="*")注解才能解决

二、在tomcat上部署javaweb项目时出现跨域问题

解决(暂未实践):

        这个错误是由于浏览器的CORS策略导致的,具体原因是你的代码向另一个域(一般指端口或者协议)发送了请求,而浏览器默认情况下会拒绝这样的请求,以避免潜在的漏洞。

要解决这个问题,可以通过设置服务器响应头部来允许来自不同域的请求。在Tomcat中,可以在web.xml文件中配置过滤器,将以下代码粘贴到web.xml文件中:

<filter>  
  <filter-name>CORSFilter</filter-name>  
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>  
  <init-param>  
    <param-name>cors.allowed.origins</param-name>  
    <param-value>*</param-value>  
  </init-param>  
  <init-param>  
    <param-name>cors.allowed.methods</param-name>  
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>  
  </init-param>  
  <init-param>  
    <param-name>cors.allowed.headers</param-name>  
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>  
  </init-param>  
</filter>  

<filter-mapping>  
  <filter-name>CORSFilter</filter-name>  
  <url-pattern>/*</url-pattern>  
</filter-mapping> 

其中,cors.allowed.origins参数表示允许的来源地址,*表示允许任意地址,cors.allowed.methods参数表示允许的HTTP请求方法,cors.allowed.headers参数表示允许的请求头部。

注意,这个配置会将所有的请求都允许跨域,不需要的话可以根据实际情况精细化配置。配置完成后,重启Tomcat服务器,再次尝试访问你的项目应该就不会再出现这个错误了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
The CORS (Cross-Origin Resource Sharing) policy is a security measure implemented by web browsers to restrict access to resources (such as APIs) on a different origin (domain, protocol, or port) than the one the web page is served from. In your case, it seems that you are making a request from 'http://localhost:8081' to a different origin, and the server is blocking that request due to CORS policy restrictions. To resolve this issue, you can try the following solutions: 1. Enable CORS on the server-side: If you have control over the server-side code, you can configure the server to include the appropriate CORS headers in the response. These headers allow the browser to determine if it should allow the request. The minimal required headers are 'Access-Control-Allow-Origin' and 'Access-Control-Allow-Methods'. You can set the value of 'Access-Control-Allow-Origin' to '*' to allow requests from any origin during development. However, in production, it's recommended to set it to the specific origin of your application. 2. Use a proxy server: If you don't have control over the server-side code or enabling CORS is not feasible, you can set up a proxy server that acts as an intermediary between your client-side code and the server. Your client-side code will make requests to the proxy server, and the proxy server will forward those requests to the actual server. Since the proxy server will be on the same origin as your client-side code, you won't face any CORS issues. 3. Disable CORS in the browser: Although not recommended for production environments, during development, you can disable CORS checks in your browser. Keep in mind that this is a temporary solution and should not be used in a production environment. The method to disable CORS varies between browsers, so you need to search for instructions specific to your browser. Remember to consider security implications when implementing any solution and ensure that proper CORS configurations are applied in a production environment.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值