Problem
When developing projects using the front-end detach mode, you often encounter the problem of not getting server-side data across domains
This is due to the browser's homologous policy, which is intended to be secure. In the front and back end of the separation of development models favored today, frontend and background projects are often in different environments to develop, then there will be cross-domain request data requirements, the current solution is mainly the following:
JSONP, IFRAME, proxy mode, cors, etc.
Here are a few ways not to speak here, there is a lot of information on the Internet. Here I mainly share the solution of Cors, Cors is "cross-domain resource sharing", which allows the browser to cross-origin server, the XMLHttpRequest request, thus overcoming the limitations of Ajax can only use the same origin.
Using CORS across domains is the same as the normal Ajax process, but the browser will automatically help us with some things when it finds out that this is a cross-domain request, so as long as the service side provides support, the front end does not need to do anything extra .
Realize
The general idea of implementation is to first use the filter to obtain information about request requests, such as the Origin field (which indicates which source the request came from, including the protocol, domain name, port), to determine whether the request is legitimate through preconfigured parameters, and then to set the header information for the Response object response. Implement cross-domain resource requests. Before introducing the implementation approach, let's look at the response header information that will be used.
Response header
-
Access-control-allow-methods
The HTTP methods used to list the browser's cors requests, such as GET, POST, PUT, DELETE, OPTIONS
-
Access-control-allow-credentials
Indicates whether cross-domain cookies are supported
-
Access-control-allow-headers
A comma-delimited string representing all header information fields supported by the server, such as Content-type and custom fields
-
Access-control-expose-headers
In contrast to "access-control-allow-headers", represents an unsupported header information field
-
Access-control-allow-origin
Allow cross-domain request source information, including protocol, domain name, port, to * allow all request sources, and can only set one request source
Here's how the Java backend is implemented in this way.
Code
Because of the recent use of Spring-boot, this is followed by a spring-boot-based implementation.
First create a corsfilter filter with the following code:
... @WebFilter (filtername = "Corsfilter", Urlpatterns = "/*", InitParams = {@WebInitParam (name = "Alloworigin", Valu E = "*"), @WebInitParam (name = "Allowmethods", value = "Get,post,put,delete,options"), @WebI Nitparam (name = "Allowcredentials", value = "true"), @WebInitParam (name = "Allowheaders", value = "content- Type,x-token ")}) public class Corsfilter implements Filter {private String alloworigin; Private String allowmethods; Private String allowcredentials; Private String allowheaders; Private String exposeheaders; @Override public void init (Filterconfig filterconfig) throws servletexception {Alloworigin = Filterconfig.getin Itparameter ("Alloworigin"); Allowmethods = Filterconfig.getinitparameter ("Allowmethods"); Allowcredentials = Filterconfig.getinitparameter ("Allowcredentials"); Allowheaders = Filterconfig.getinitparameter ("Allowheaders"); Exposeheaders = Filterconfig.getiniTparameter ("Exposeheaders"); } @Override public void DoFilter (ServletRequest servletrequest, Servletresponse servletresponse, Filterchain Filterc Hain) throws IOException, servletexception {httpservletrequest request = (httpservletrequest) servletrequest; HttpServletResponse response = (httpservletresponse) servletresponse; if (! Stringutils.isempty (Alloworigin)) {if (Alloworigin.equals ("*")) {Response.setheader ("Access-cont Rol-allow-origin ", Alloworigin); }else{list<string> alloworiginlist = arrays.aslist (Alloworigin.split (",")); if (alloworiginlist! = null && alloworiginlist.size () > 0) {String currentorigin = Request. GetHeader ("Origin"); if (Alloworiginlist.contains (Currentorigin)) {Response.setheader ("Access-control-allow-origin", CU Rrentorigin); } } } } if (! Stringutils.isempty (Allowmethods)) {Response.setheader ("Access-control-allow-methods", allowMethods); } if (! Stringutils.isempty (allowcredentials)) {Response.setheader ("access-control-allow-credentials", allowCredential s); } if (! Stringutils.isempty (allowheaders)) {Response.setheader ("access-control-allow-headers", allowHeaders); } if (! Stringutils.isempty (exposeheaders)) {Response.setheader ("access-control-expose-headers", exposeHeaders); } filterchain.dofilter (ServletRequest, servletresponse); } @Override public void Destroy () {}}
Finished, now the front-end can be cross-domain to obtain background data, more easily than other ways, the code is not explained, easy to understand, using other background development method is the same, the ultimate goal is to judge the request, set the response head, the front end nothing to do.
Java Learning Exchange QQ Group: 589809992 prohibit small talk, non-happy do not enter!
Java implements cors cross-domain requests