java backend solves cross domain problems through Filter filter

This scheme only needs the code modification of the server

Because now we want to separate the front end and the back end, there must be cross domain problems. Here are the effective codes we have tested:

Steps:

I. add in the web.xml file

<!-- 2019-01-15   Solve cross domain problems -->
    <filter>
        <filter-name>crossFilter</filter-name>
        <filter-class>com.sx.test.controller.CorsFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>crossFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2. Create the CorsFilter.java file in your own project

package com.sx.test.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "CORSFilter", urlPatterns = {"/*"})
@Order(value = 1)
@Configuration
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {
        System.out.println("Filter Filter executed");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // The response header specifies the URI path to which the resource can be accessed
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

        //The response header specifies one or more methods allowed when the response accesses the resource
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");

        //Set the maximum number of seconds the cache can survive
        response.setHeader("Access-Control-Max-Age", "3600");

        //Set supported request headers
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");

        // Indicates whether the response of the request for can be exposed to the page. It can be exposed when the true value returns
        response.setHeader("Access-Control-Allow-Credentials","true");

        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {

    }
}

Note:

Annotation function in the above code:

The @ WebFilter is used to declare a class as a filter. The annotation will be processed by the container during deployment. The container will deploy the corresponding class as a filter according to the specific property configuration. @WebFilter common properties:

The @ Order annotation is mainly used to control the loading Order of configuration classes. Its value is a positive integer. The smaller the value, the higher the priority

From spring 3.0, @ Configuration is used to define Configuration classes

 

For setHeader(), the API details related to the response header are as follows: https://cloud.tencent.com/developer/section/1189894 

If it's helpful to you, please give me a comment. If there's a better way or resource, please don't hesitate to give me a comment

Keywords: Java xml Spring

Added by aseaofflames on Sun, 08 Dec 2019 18:52:48 +0200