• Today, when the project was connected, I stored the entity data in session, which could pass the local test with PostMan, but could not get the data when the front-end connected data. I felt very strange at that time, and modified it as follows after consulting the data:

The solution

  • Add the CrossFilter class to the Java project directory
  • The following code
import org.springframework.stereotype.Component; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /* */ @component public class implements Filter {@override public void init(FilterConfig) filterConfig) throws ServletException { } @Override public voiddoFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.addHeader("Access-Control-Allow-Origin"."*");
        response.setHeader("Access-Control-Allow-Methods"."POST, GET, OPTIONS, DELETE, PUT, GET");
        response.setHeader("Access-Control-Max-Age"."3600");
        response.setHeader("Access-Control-Allow-Headers"."Origin, X-Requested-With, Content-Type, Accept");
        response.setHeader("Access-Control-Allow-Credentials"."true");

        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {}}Copy the code
  • If that doesn’t work, add the following code to the front end
Ajax needs to set: xhrFields: {withCredentials:true
       },
       crossDomain: true
Copy the code
  • Notice the addition of the CrossFilter class to the Java project directory

    Access-control-allow-credentials are true. Access-control-allow-origin “cannot be *. Add something to the Ajax request as well

Summing up,

  • careful
  • reflection
  • ascension
  • Write code with IQ, work with EQ!