有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在jboss eap 7.3上启用cors?

我知道这个问题被问了很多次

我有一个关于spring boot rest项目的简单war文件

使用全局cors,我能够成功地在tomcat上解析cors,但在jbos上同样的代码和配置失败

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedMethods("*")
                .allowedOrigins("http://localhost:9000").allowCredentials(true);
            }
        };
    }

但即使使用这种配置,jboss也会出现cors错误

那么我们是否需要额外更新jboss配置以启用cors

enter image description here

enter image description here

从这里添加xml配置

http://www.mastertheboss.com/jboss-web/jbosswebserver/how-to-configure-cors-on-wildfly

所以在我添加了上面链接中提到的过滤器之后,现在我的请求给出了404错误

我的更新版独立版。xml文件更新的代码片段

<subsystem xmlns="urn:jboss:domain:undertow:10.0" default-server="default-server" default-virtual-host="default-host" default-servlet-container="default" default-security-domain="other" statistics-enabled="${wildfly.undertow.statistics-enabled:${wildfly.statistics-enabled:false}}">
            <buffer-cache name="default"/>
            <server name="default-server">
                <http-listener name="default" socket-binding="http" redirect-socket="https" enable-http2="true"/>
                <https-listener name="https" socket-binding="https" security-realm="ApplicationRealm" enable-http2="true"/>
                <host name="default-host" alias="localhost">
                    <location name="/" handler="welcome-content"/>
                    <http-invoker security-realm="ApplicationRealm"/>
                    <filter-ref name="server-header"/>
                    <filter-ref name="x-powered-by-header"/>
                    <filter-ref name="Access-Control-Allow-Origin"/>
                    <filter-ref name="Access-Control-Allow-Methods"/>
                    <filter-ref name="Access-Control-Allow-Headers"/>
                    <filter-ref name="Access-Control-Allow-Credentials"/>
                    <filter-ref name="Access-Control-Max-Age"/>
                </host>
            </server>
            <servlet-container name="default">
                <jsp-config/>
                <websockets/>
            </servlet-container>
            <handlers>
                <file name="welcome-content" path="${jboss.home.dir}/welcome-content"/>
            </handlers>
            <filters>
                <response-header name="server-header" header-name="Server" header-value="WildFly/10"/>
                <response-header name="x-powered-by-header" header-name="X-Powered-By" header-value="Undertow/1"/>
                <response-header name="Access-Control-Allow-Origin" header-name="Access-Control-Allow-Origin" header-value="http://localhost:9000"/>
                <response-header name="Access-Control-Allow-Methods" header-name="Access-Control-Allow-Methods" header-value="GET, POST, OPTIONS, PUT"/>
                <response-header name="Access-Control-Allow-Headers" header-name="Access-Control-Allow-Headers" header-value="*"/>
                <response-header name="Access-Control-Allow-Credentials" header-name="Access-Control-Allow-Credentials" header-value="true"/>
                <response-header name="Access-Control-Max-Age" header-name="Access-Control-Max-Age" header-value="1"/>
        </filters>
        </subsystem>

共 (2) 个答案

  1. # 2 楼答案

    找到了解决办法

    实际上,问题与CORS错误无关,正如浏览器在“网络”选项卡中显示的那样。它具有误导性

    首先,我从postman测试了api,当它部署在jboss上时,它给出了404错误。这是主要问题

    然后我搜索并找到了如何在jboss上部署spring boot war,并找到了缺失的链接

    更新-1。弹簧靴主类

    与其只是添加全局cors配置,不如扩展SpringBootServletilizer

    @SpringBootApplication
    public class CorsApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(CorsApplication.class);
        }
        
        public static void main(String[] args) {
            SpringApplication.run(CorsApplication.class, args);
        }
        
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**").allowedMethods("*")
                    .allowedOrigins("http://localhost:9000").allowCredentials(true);
                }
            };
        }
    }
    

    Update-2必须添加javax。pom中的servlet api依赖关系。并从spring boot starter web中排除嵌入式tomcat

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.5</version>
            <relativePath/> <!  lookup parent from repository  >
        </parent>
        <groupId>com.Spring</groupId>
        <artifactId>CORS</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>CORS</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                 <exclusions>
                    <exclusion>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-starter-tomcat</artifactId>
                    </exclusion>
              </exclusions>
            </dependency>
    
             <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
        </build>
    </project>
    

    因此,在这两次更改之后,jboss上的部署成功了。他们不需要在单机版中进行任何更改。xml文件