How to Solve Error: Preflight response is not successful

XMLHttpRequest cannot load http://130.51.23.12 : 8180/remotelogin. Preflight response is not successful. Add annotation to solve the problem: @ crossorigin (origins = "*", maxage = 3600)

This annotation comes after spring 4.2

If it is lower than version 4.2, add filter:

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CorsFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
        httpServletResponse.setHeader("Access-Control-Allow-Headers",
                "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With, Access-Control-Allow-Methods, Access-Control-Allow-Origin");
        httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");// 允许cookie
        chain.doFilter(request, response);
    }

    public void destroy() {}
}

web.xml

    <filter>
        <filter-name>corsFilter</filter-name>
        <filter-class>com.x.sso.cas.server.security.CorsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>corsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Similar Posts: