91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

如何解決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
易门县| 介休市| 电白县| 莱西市| 河津市| 汤阴县| 博爱县| 科技| 峨眉山市| 双峰县| 台州市| 乳源| 岑溪市| 遵化市| 兴安县| 尼勒克县| 自贡市| 土默特右旗| 永川市| 漾濞| 彩票| 万荣县| 高邑县| 大安市| 吴旗县| 和田县| 疏勒县| 宁蒗| 山阴县| 阜城县| 望谟县| 陕西省| 涿鹿县| 池州市| 花莲市| 开江县| 孟津县| 新疆| 沾化县| 新巴尔虎左旗| 黄浦区|