我正在使用 Java Spring 3.0.4(由于某些要求而无法升级),并且我需要启用 Cors 以便我的前端能够与后端对话。
我的后端是一个运行在以下位置的角度应用程序:http://localhost:4200/home
我尝试了以下方法但没有成功:
public static final String CREDENTIALS_NAME = "Access-Control-Allow-Credentials";
public static final String ORIGIN_NAME = "Access-Control-Allow-Origin";
public static final String METHODS_NAME = "Access-Control-Allow-Methods";
public static final String HEADERS_NAME = "Access-Control-Allow-Headers";
public static final String MAX_AGE_NAME = "Access-Control-Max-Age";
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping(value="/data", method=RequestMethod.GET)
public void serverSide(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-store");
response.setHeader(CREDENTIALS_NAME, "true");
response.setHeader(ORIGIN_NAME, "http://localhost:4200");
response.setHeader(METHODS_NAME, "GET, OPTIONS, POST, PUT, DELETE");
response.setHeader(HEADERS_NAME, "Origin, X-Requested-With, Content-Type, Accept");
response.setHeader(MAX_AGE_NAME, "3600");
PrintWriter out = …Run Code Online (Sandbox Code Playgroud)