How to solve the 403 error when developing a small program

王林
Release: 2020-12-21 09:23:47
forward
4248 people have browsed it

How to solve the 403 error when developing a small program

Problem analysis:

We know that when the front-end program sends a request to the back-end server, if the server does not allow cross-domain requests, a 403 error will occur (the error message is : "Invalid CORS request"). So how to solve this problem?

(Learning video sharing: Programming video)

Solution:

Configure the trusted domain to the CORS allowed source address list, as follows The code is shown:

@Bean
public CorsFilter corsFilter() {
   UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
   CorsConfiguration config = new CorsConfiguration();
   config.setAllowCredentials(true);
   config.addAllowedOrigin("http://localhost:3000");
   config.addAllowedOrigin("http://127.0.0.1:3000");
   config.addAllowedOrigin("http://127.0.0.1:55135");
   config.addAllowedHeader(CorsConfiguration.ALL);
   config.addAllowedMethod(CorsConfiguration.ALL);
   source.registerCorsConfiguration("/**", config);
   CorsFilter bean = new CorsFilter(source);
   return bean;
}
Copy after login

For the development of WeChat applet, the situation is a little different. Since the WeChat applet only allows https connections in the domain name, an intranet penetration tool such as peanut shell is used to build a A public domain name accessible from the outside. The public domain name points to the internal address.

While debugging, I encountered the problem of illegal cross-domain requests. The reason is that when requesting the backend server, the WeChat developer tool includes the Origin field in the request header, so the server determines that it is a cross-domain request. Through tools such as Fiddler, you can capture packets and see the following information:

POST https://xxx.xxx.net/public/login HTTP/1.1Host: sharework.gicp.netConnection: keep-aliveContent-Length: 50Pragma: no-cacheCache-Control: no-cacheOrigin: http://127.0.0.1:55135User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 wechatdevtools/1.02.1902010 MicroMessenger/6.7.3 Language/zh_CN webview/ token/e011a64b71b385130aa1f595fe48521ccontent-type: application/jsonAccept: */*Referer: https://servicewechat.com/wx955fc9354838fd46/devtools/page-frame.htmlAccept-Encoding: gzip, deflate, br
{"account":"user","password":"defaultPassword123"}
Copy after login

The reason is here. If you preview or debug directly on your mobile phone, you will not encounter this problem.

Add http://127.0.0.1:55135 to the domain that allows CORS access, and you can start debugging happily.

Of course, the port 55135 changes frequently, and I have not yet found a way to fix it. Currently, you can quickly find this port through the following methods (taking Windows as an example):

1. tasklist | findstr "wechat", find the process number with the largest memory usage, such as 12824

E:\apps\data-integration>tasklist | findstr "wechat"
wechatdevtools.exe           13180 Console                    2     98,572 K
wechatdevtools.exe           11092 Console                    2      7,676 K
wechatdevtools.exe           15276 Console                    2    132,520 K
wechatdevtools.exe           18380 Console                    2    136,748 K
wechatdevtools.exe            8652 Console                    2     26,100 K
wechatdevtools.exe           12824 Console                    2    183,668 K
wechatdevtools.exe           16124 Console                    2     89,524 K
wechatdevtools.exe            1164 Console                    2    103,336 K
wechatdevtools.exe           12616 Console                    2     77,056 K
wechatdevtools.exe           13136 Console                    2     83,312 K
Copy after login

2 , netstat -ano | findstr "12824", find the line with the status LISTENING and the only port

E:\apps\data-integration>netstat -ano | findstr "12824"
  TCP    127.0.0.1:28475        0.0.0.0:0              LISTENING       12824
  TCP    127.0.0.1:28475        127.0.0.1:61306        ESTABLISHED     12824
  TCP    127.0.0.1:28475        127.0.0.1:61318        ESTABLISHED     12824
  TCP    127.0.0.1:28475        127.0.0.1:61402        ESTABLISHED     12824
  TCP    127.0.0.1:28475        127.0.0.1:61403        ESTABLISHED     12824
  TCP    127.0.0.1:55135        0.0.0.0:0              LISTENING       12824
Copy after login

3, 55135 is the port we are looking for.

Related recommendations: WeChat Mini Program Development Tutorial

The above is the detailed content of How to solve the 403 error when developing a small program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!