Jetty Server设置允许跨域的方法

pom依赖


	org.eclipse.jetty
	jetty-server
	9.4.28.v20200408
	

	org.eclipse.jetty
	jetty-servlet
	9.4.28.v20200408


	org.eclipse.jetty
	jetty-servlets
	9.4.28.v20200408

Main代码

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlets.CrossOriginFilter;

public class JettyServer {

    public static void main(String[] args) throws Exception {
        // 创建 Jetty 服务器
        Server server = new Server(8080);

        // 创建 Servlet 上下文处理程序
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");

        // 添加 CrossOriginFilter
        context.addFilter(CrossOriginFilter.class, "/*", null);

        // 设置过滤器参数
        context.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,POST,HEAD,OPTIONS");
        context.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
        context.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, "X-Requested-With,Content-Type,Accept,Origin");

        // 将 Servlet 上下文处理程序设置到服务器中
        server.setHandler(context);

        // 启动 Jetty 服务器
        server.start();
        server.join();
    }
}

你可能感兴趣的:(Java,jetty,hive,hadoop)