web123456

Java backend solves cross-domain problems

Filter filter

Allow cross-domain access of the entire project, which can be considered through filter:

public class SimpleCORSFilter implements Filter{  
  
    @Override  
    public void destroy() {  
          
    }  
  
    @Override  
    public void doFilter(ServletRequest req, ServletResponse res,  
            FilterChain chain) throws IOException, ServletException {  
            HttpServletResponse response = (HttpServletResponse) res;  
            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");  
            chain.doFilter(req, res);  
          
    }  
  
    @Override  
    public void init(FilterConfig arg0) throws ServletException {  
          
    }  
  
}

The following configuration needs to be added:

<filter>  
      <filter-name>cors</filter-name>  
      <filter-class>com.ssm.web.filter.SimpleCORSFilter</filter-class>  
    </filter>  
    <filter-mapping>  
      <filter-name>cors</filter-name>  
      <url-pattern>/*</url-pattern>  
    </filter-mapping>
</filter>

Provide cross-domain access to a single method, adding the request header directly:

       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");

2. Backend Http request forwarding

Forwarding using HttpClinet (simple examples are not recommended)

try {
     HttpClient client = (); //client object
     HttpGet get = new HttpGet("http://localhost:8080/test"); //Create get request
     CloseableHttpResponse response = (get); //Execute get request
     String mes = (()); //Convert the return body information into a string
     (mes);
 } catch (ClientProtocolException e) {
     ();
 } catch (IOException e) {
     ();
 }

3. Configure the same origin Cors in the background (recommended)

Cross-domain on SpringBoot 2.0 can perfectly solve your front-end cross-domain request problem with the following code configuration

import ;
 import ;
 import ;
 import ;
 import ;
 /**
  * Implement basic cross-domain requests
  * @author linhongcun
  *
  */
 @Configuration
 public class CorsConfig {
     @Bean
     public CorsFilter corsFilter() {
         final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
         final CorsConfiguration corsConfiguration = new CorsConfiguration();
         /*Whether requests are allowed with verification information*/
         (true);
         /*Accessible client domain name*/
         ("*");
         /*Client request header that allows server access*/
         ("*");
         /*Accessed method name, GET POST, etc.*/
         ("*");
         ("/**", corsConfiguration);
         return new CorsFilter(urlBasedCorsConfigurationSource);
     }
 }

4. Use SpringCloud Gateway

The service gateway (zuul), also known as the routing center, is used to access all API interfaces and maintain services.
 Spring Cloud Zuul integrates with Spring Cloud Eureka to realize automated maintenance of service instances. Therefore, when using service routing configuration, we do not need to specify the specific service instance address as traditional routing configuration methods, we only need to use the Ant mode configuration file parameters.

5. Use nginx for forwarding

There are two websites who want to access each other's interface now.:81/AWant to visit:81/BThen make the following configuration
Then access/AYou can access it inside/B

s

erver {
        listen       80;
        server_name  ;
        location /A {
            proxy_pass  :81/A;
            index   ;
        }
        location /B {
            proxy_pass  :81/B;
            index   ;
        }
    }

If two ports want to access each other's interfaces:80/ApiWant to visit:81/ApiThen make the following configuration
Cross-domain problems can be completed using nginx forwarding mechanism

server {
        listen       80;
        server_name  ;
        location /Api {
            proxy_pass  :81/Api;
            index   ;
        }
    }