中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

如何解決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
义乌市| 黄山市| 砀山县| 淅川县| 启东市| 文成县| 鸡东县| 安新县| 赤水市| 林州市| 彰化县| 东兰县| 体育| 嘉禾县| 玛多县| 宝山区| 山丹县| 喀喇沁旗| 凤山县| 禄丰县| 巴里| 蛟河市| 广昌县| 阳江市| 宁远县| 成武县| 汾西县| 贡山| 宜川县| 万山特区| 罗定市| 云和县| 阳原县| 铜梁县| 霞浦县| 和硕县| 南溪县| 临漳县| 安塞县| 太仓市| 耒阳市|