亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

如何解決cors java中的預檢請求問題

小樊
91
2024-09-05 15:13:30
欄目: 編程語言

CORS(跨源資源共享)是一種安全機制,用于在Web應用程序中允許或禁止來自不同源的HTTP請求

  1. 使用Java Servlet Filter:

創建一個名為CORSFilter.java的新類,并實現javax.servlet.Filter接口。在這個類中,你需要實現以下三個方法:init(), doFilter()destroy()

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
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.HttpServletResponse;

@WebFilter(asyncSupported = true, urlPatterns = { "/*" })
public class CORSFilter implements Filter {

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

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
  1. 配置Spring Boot:

如果你使用的是Spring Boot,可以通過添加@Bean到你的主應用程序類中來配置CORS。例如:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                        .allowedHeaders("*")
                        .maxAge(3600);
            }
        };
    }
}
  1. 配置JAX-RS:

如果你使用的是JAX-RS(Jersey),可以通過創建一個名為CORSFilter.java的新類,并實現ContainerResponseFilter接口來配置CORS。例如:

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;

public class CORSFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
            throws IOException {
        MultivaluedMap<String, Object> headers = responseContext.getHeaders();
        headers.add("Access-Control-Allow-Origin", "*");
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");
        headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
    }
}

然后,確保在你的應用程序中注冊此過濾器。例如,在Application類中添加以下代碼:

import org.glassfish.jersey.server.ResourceConfig;

public class Application extends ResourceConfig {
    public Application() {
        packages("your.package.name");
        register(CORSFilter.class);
    }
}

這些方法應該能幫助你解決Java中的CORS預檢請求問題。根據你的項目需求和架構選擇合適的方法進行配置。

0
长春市| 屏东县| 临沭县| 蒙山县| 静安区| 紫金县| 富平县| 朔州市| 永昌县| 岳西县| 潜江市| 永新县| 惠安县| 苏尼特右旗| 邯郸县| 女性| 当阳市| 铁岭市| 武威市| 垫江县| 蚌埠市| 望谟县| 北票市| 泰和县| 惠州市| 元朗区| 海兴县| 铜鼓县| 泰州市| 内黄县| 罗山县| 博白县| 都昌县| 三门县| 高安市| 芜湖市| 开化县| 克拉玛依市| 介休市| 大关县| 桂林市|