您可以編寫組件類CORS起源過濾
@Component
public class CorsFilterConfiguration implements Filter {
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, PUT, PATCH, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with, Content-Type, authToken"); //TODO: Fix 'token' to 'OAuth'/'SAML' header.
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
,但我沒有實現的servlet。我正在使用一個主要的功能,我正在生產寧靜的網絡服務。有沒有辦法讓它不使用servelt過濾器? (「Access-Control-Allow-Origin」,「*」) .header(「Access-Control-Allow-Methods」,「POST」) – Vijay
您也可以在您的響應中設置這些參數 返回Response.ok() .header ,GET,PUT,UPDATE,OPTIONS) .header(「Access-Control-Allow-Headers」,「Content-Type,Accept,X-Requested-With」)。 –
這是我的代碼。我怎麼會這樣做,請幫助 – Vijay