Java SSM solves cross-domain issues

Source: Internet
Author: User

What is a cross-domain

Cross-domain refers to a resource that requests another domain name from a Web page of a domain name. For example, from the Www.baidu.com page to request www.google.com resources. The strict definition of cross-domain is that as long as the protocol, domain name, the port has any difference, it is considered to be a cross-domain

The previous time contact a new project, originally wanted to make the front and rear end separation, but this will involve the Ajax cross-domain problem, then did not solve, so there is no front and back end separation, but today's project and contact with the cross-domain (front and rear separation of the problem), studied a night, looked up a lot of information, Also communicated with some of the predecessors, using different methods,

Some people say that with Google's plug-in, some people say with nginx to do a reverse proxy, more people say, with JSONP way, these methods are feasible, but the operation is a bit big ah, the first not to say, to fq out to download the installation, the second, for many novice, it is difficult to understand,

Third, JSONP can only use get to request data, but we use Get method to request data, this is a few cases? So, I synthesized a bit, I hope to help more small partners!

This configuration allows cross-domain is completely in our background, regardless of the front-end, I use the backend framework is SSM (in fact, the problem of Spring), other frameworks, please bypass!!!! (with the contact Nutz frame, the cross-domain of this frame is simpler, add a note to the line:@Filters (@By (type=crossoriginfilter.class)))

Well, a lot of nonsense, to get to the matter, in fact, the trilogy on it,

1. Create a new filter and implement the filter interface

Please note the Guide Package!!!!

 Public classSimplecorsfilterImplementsFilter {Private BooleanIscross =false; @Override Public voiddestroy () {Iscross=false; } @Override Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain)throwsIOException, servletexception {if(iscross) {httpservletrequest httpservletrequest=(httpservletrequest) request; HttpServletResponse HttpServletResponse=(httpservletresponse) response; System.out.println ("Intercept Request:" +Httpservletrequest.getservletpath ()); Httpservletresponse.setheader ("Access-control-allow-origin", "*"); Httpservletresponse.setheader ("Access-control-allow-methods", "POST, GET, OPTIONS, DELETE"); Httpservletresponse.setheader ("Access-control-max-age", "0"); Httpservletresponse.setheader ("Access-control-allow-headers",                    "Origin, No-cache, X-requested-with, If-modified-since, Pragma, last-modified, Cache-control, Expires, Content-type, X-e4m-with,userid,token "); Httpservletresponse.setheader ("Access-control-allow-credentials", "true"); Httpservletresponse.setheader ("Xdomainrequestallowed", "1");    } chain.dofilter (request, response); } @Override Public voidInit (Filterconfig filterconfig)throwsservletexception {String iscrossstr= Filterconfig.getinitparameter ("Iscross"); Iscross= Iscrossstr.equals ("true")?true:false;    System.out.println (ISCROSSSTR); }}

2. Set the configuration in Web. xml

 <!--cross-domain requests -       <Filter>         <Filter-name>Simplecorsfilter</Filter-name>         <Filter-class>Com.ssm.util.SimpleCORSFilter</Filter-class>         <Init-param>             <Param-name>Iscross</Param-name>             <Param-value>True</Param-value>         </Init-param>     </Filter>     <filter-mapping>         <Filter-name>Simplecorsfilter</Filter-name>         <Url-pattern>/*</Url-pattern>     </filter-mapping>  

3. Configure cross-domain requests in the STRINGMVC configuration file

      <!--interface cross-domain configuration -        <mvc:cors>            <mvc:mappingPath="/**"Allowed-origins="*"Allowed-methods= "POST, GET, OPTIONS, DELETE, PUT"allowed-headers= "Content-type, Access-control-allow-headers, Authorization, X-requested-with"allow-credentials= "true" />        </mvc:cors>  

Spring MVC cross-domain configuration, to this end, the front end does not change anything, the normal use of Ajax can

Java SSM solves cross-domain issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.