Cross-domain intercept Access-control-allow-origin set multiple origin

Source: Internet
Author: User

In ExtJS and Java projects encountered the need to handle cross-domain at the same time, external access to the background interface problems

The original code is this way, you can only set a cross-domain request that the ExtJS foreground needs to filter

 PackageCom.xgt.config;Importjavax.servlet.*;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException;Importjava.util.Arrays;ImportJava.util.HashSet;ImportJava.util.Set;/*** Troubleshoot cross-domain issues*/ Public classSimplecorsfilterImplementsFilter { Public voidDoFilter (servletrequest req, servletresponse Res, filterchain chain)throwsIOException, servletexception {httpservletresponse response=(HttpServletResponse) res; Response.setheader ("Access-control-allow-origin", "http://127.0.0.1:1841"); Response.setheader ("Access-control-allow-methods", "POST, GET, OPTIONS, DELETE"); Response.setheader ("Access-control-max-age", "3600"); Response.setheader ("Access-control-allow-headers", "Content-type, X-requested-with"); Response.setheader ("Access-control-allow-credentials", "true"); }     Public voidInit (Filterconfig filterconfig) {} Public voiddestroy () {}}

Slightly improved, to the same LAN colleagues to access my interface, I set the IP is the colleague's 192.168.1.178, because his IP access to my interface will be intercepted, my own IP will not be intercepted, so do not set

 PackageCom.xgt.config;Importjavax.servlet.*;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException;Importjava.util.Arrays;ImportJava.util.HashSet;ImportJava.util.Set;/*** Troubleshoot cross-domain issues*/ Public classSimplecorsfilterImplementsFilter { Public voidDoFilter (servletrequest req, servletresponse Res, filterchain chain)throwsIOException, servletexception {httpservletresponse response=(HttpServletResponse) res; String [] Allowdomain= {"http://127.0.0.1:1841", "http://192.168.1.178"}; Set<String> allowedorigins=NewHashset<string>(Arrays.aslist (Allowdomain)); String Originheader= ((httpservletrequest) req). GetHeader ("Origin"); if(Allowedorigins.contains (Originheader)) {Response.setheader ("Access-control-allow-origin", Originheader); Response.setheader ("Access-control-allow-methods", "POST, GET, OPTIONS, DELETE"); Response.setheader ("Access-control-max-age", "3600"); Response.setheader ("Access-control-allow-headers", "Content-type, X-requested-with"); Response.setheader ("Access-control-allow-credentials", "true");    } chain.dofilter (req, res); }     Public voidInit (Filterconfig filterconfig) {} Public voiddestroy () {}}

Cross-domain intercept Access-control-allow-origin set multiple origin

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.