Spring solves cross-domain problems through CROS protocol
Now I am taking over a project in the school network center. Based on the actual situation and development needs of the team members, the teacher hopes to completely separate the front and back ends. The backend uses java to provide restful API as the core, and the frontend can share the same core regardless of PC or mobile terminal. In the early stage, problems such as oauth2 as an authorization mechanism were solved, and I thought it would be a great success. (Recently I plan to introduce in detail the solution of Spring sercurity to implement oauth2) As a result, another cross-domain problem occurred, which made us step into a big pit. We will record it here to avoid future troubles.
The error message is as follows:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
What is cross-domain?
Simply put, it means that the browser restricts access to the js code under site A and makes an ajax request to the url under site B. For example, if the front-end domain name is www.abc.com, then the js code running in the current environment is restricted from accessing resources under the www.xyz.com domain name for security reasons. Modern browsers will block cross-domain ajax requests by default for security reasons. This is a necessary feature in modern browsers, but it often brings inconvenience to development. Especially for a backend developer like me, this thing is simply magical.
But the need for cross-domain has always been there. In order to cross-domain, hard-working and brave programmers have come up with many methods, such as jsonP, proxy files, etc. However, these practices add a lot of unnecessary maintenance costs, and there are many limitations in application scenarios. For example, jsonP is not XHR, so jsonP can only use GET to pass parameters. More detailed information can be found here. Summary of cross-domain access solutions for web applications
CORS protocol
Nowadays, JS has a tendency to dominate the world, and the browser has become the best place for most applications. Even on the mobile side, there are various Hybird solutions. On the Web page of the local file system, there are also needs to obtain external data, and these needs must be cross-domain. When looking for a cross-domain solution, I found that the most elegant solution is the new feature of "Cross-Origin Resource Sharing" brought by HTML5, which gives developers the power to decide whether resources are allowed to be accessed across domains.
CORS is a W3C standard, its full name is "Cross-origin resource sharing".
It allows the browser to issue XMLHttpRequest requests to cross-origin servers, thereby overcoming the limitation that AJAX can only be used from the same origin.
Why do you say it is elegant?
The entire CORS communication process is automatically completed by the browser and does not require user participation. For developers, there is no difference between CORS communication and AJAX communication from the same source, and the code is exactly the same. Once the browser discovers that the AJAX request is cross-origin, it will automatically add some additional header information, and sometimes an additional request will be made, but the user will not feel it.
Therefore, the key to achieving CORS communication is the server. As long as the server implements the CORS interface, it can communicate across sources.
The key to solving this problem falls on me, the programmer responsible for the backend.
It’s not difficult to look at the documentation, but you need to set Access-Control-Allow-Origin in the http header to determine which sites need to be allowed to access. For more details about the CROS protocol, please refer to Cross-domain Resource Sharing CORS Detailed explanation
CROS common headers
CORS has the following common headers
Access-Control-Allow-Origin: http://kbiao.me Access-Control-Max-Age: 3628800 Access-Control-Allow-Methods: GET,PUT, DELETE Access-Control-Allow-Headers: content-type
"Access-Control-Allow-Origin" indicates that it allows "http://kbiao.me" Initiating a cross-domain request
"Access-Control-Max-Age" indicates that within 3628800 seconds, there is no need to send a pre-inspection request and the result can be cached (from the above information, we know that in the CROS protocol, an AJAX request is divided into The first step of OPTION pre-detection request and formal request)
"Access-Control-Allow-Methods" indicates that it allows GET, PUT, DELETE external domain requests
"Access-Control-Allow-Headers" indicates that it allows cross-domain requests The request contains the content-type header
Of course, there are many pitfalls in the front-end when dealing with this problem, especially the Chrome browser does not allow cross-domain requests from localhost. For detailed solutions, please see the partner responsible for the front-end solution in my project. Pretend there is a link
General solution
I know the cause of the problem and the supporting solution, let us solve it now. The idea is very simple. When the front end requests cross-domain resources, we just add the response header to it. Obviously it is easiest for us to define a filter ourselves.
/** * Created by kangb on 2016/5/10. */ @Component public class myCORSFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; String origin = (String) servletRequest.getRemoteHost()+":"+servletRequest.getRemotePort(); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization"); response.setHeader("Access-Control-Allow-Credentials","true"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
@Component 是Spring的注解,关键部分在doFilter中,添加了我们需要的头, option 是预检测需要所以需要允许, Authorization 是做了oauth2登录响应所必须的, Access-Control-Allow-Credentials 表示允许cookies。都是根据自己项目的实际需要配置。
再配置Web.xml使得过滤器生效
<filter> <filter-name>cors</filter-name> <filter-class>·CLASS_PATH·.myeCORSFilter</filter-class> </filter> <filter-mapping> <filter-name>cors</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping>
接下来前端就可以像往常一样使用AJAX请求获得资源了,完全不需要做出什么改变。
SPRING 4中更优雅的办法
SpringMVC4提供了非常方便的实现跨域的方法。在requestMapping中使用注解。
@CrossOrigin(origins = “http://kbiao.me”)
全局实现 .定义类继承WebMvcConfigurerAdapter,设置跨域相关的配置
public class CorsConfigurerAdapter extends WebMvcConfigurerAdapter{ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/*").allowedOrigins("*"); } }
将该类注入到容器中:
<bean class="com.tmall.wireless.angel.web.config.CorsConfigurerAdapter"></bean>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Bring matrix movie effects to your page! This is a cool jQuery plugin based on the famous movie "The Matrix". The plugin simulates the classic green character effects in the movie, and just select a picture and the plugin will convert it into a matrix-style picture filled with numeric characters. Come and try it, it's very interesting! How it works The plugin loads the image onto the canvas and reads the pixel and color values: data = ctx.getImageData(x, y, settings.grainSize, settings.grainSize).data The plugin cleverly reads the rectangular area of the picture and uses jQuery to calculate the average color of each area. Then, use

This article will guide you to create a simple picture carousel using the jQuery library. We will use the bxSlider library, which is built on jQuery and provides many configuration options to set up the carousel. Nowadays, picture carousel has become a must-have feature on the website - one picture is better than a thousand words! After deciding to use the picture carousel, the next question is how to create it. First, you need to collect high-quality, high-resolution pictures. Next, you need to create a picture carousel using HTML and some JavaScript code. There are many libraries on the web that can help you create carousels in different ways. We will use the open source bxSlider library. The bxSlider library supports responsive design, so the carousel built with this library can be adapted to any

Data sets are extremely essential in building API models and various business processes. This is why importing and exporting CSV is an often-needed functionality.In this tutorial, you will learn how to download and import a CSV file within an Angular
