A method of Java backend to solve cross domain problems
Access to XMLHttpRequest at 'xxxx' from origin 'xxxx' has been blocked by CORS policy: Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.
URL indicates whether communication is allowed http://www.a.com/a.js http://www.a.com/b.js Allowed under the same domain name http://www.a.com/lab/a.js http://www.a.com/script/b.js Allow different folders under the same domain name http://www.a.com:8000/a.js http://www.a.com/b.js The same domain name is not allowed on different ports http://www.a.com/a.js https://www.a.com/b.js The same domain name is not allowed by different protocols http://www.a.com/a.js http://192.168.1.1/b.js Domain name and corresponding IP address are not allowed http://www.a.com/a.js http://script.a.com/b.js Same primary domain, different sub domains are not allowed http://www.a.com/a.js http://a.com/b.js The same domain name and different secondary domain names (as above) are not allowed (cookies are also not allowed to access in this case) http://www.cnblogs.com/a.js http://www.a.com/b.js Different domain names are not allowed
package com.lcry.filter; import javax.servlet.*; import javax.servlet.http. HttpServletResponse; import java.io.IOException; /** * @author lcry * @create 2019/1/4 */ public class SessionListener implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse httpServletResponse=(HttpServletResponse) response; httpServletResponse.setHeader("Access-Control-Allow-Origin", "*"); chain.doFilter(request, response); } @Override public void destroy() { // TODO Auto-generated method stub } }
<!-- Solving cross domain problems --> <filter> <filter-name>SessionListener</filter-name> <filter-class>com.lcry.filter. SessionListener</filter-class> </filter> <filter-mapping> <filter-name>SessionListener</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>