zoukankan      html  css  js  c++  java
  • ajax跨域访问 java controller 和 cxf(webservice) 配置方式(CORS)

    1. controller跨域访问,配置方式

    重点在这里:

    <mvc:cors>
            <mvc:mapping path="/*" allowed-origins="*"
                allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
                allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
                allow-credentials="true" />
        </mvc:cors> 

    maven 需要引入 jar包  http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter 

    <dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.5</version>
    </dependency>

    <dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>java-property-utils</artifactId>
    <version>1.9.1</version>
    </dependency>

    ----------------------------------------------------------------------------------------------------------------------

    2.(cxf)webservice接口的跨域访问配置(这个比较恶心)

    cxf.xml文件中的配置方式

    重点在这里:

    <jaxrs:providers>
                <bean
                    class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
                    <property name="allowOrigins"
                        value="#{systemProperties['origins'] != null ? systemProperties['origins'].split(',') : {}}">
                    </property>
                </bean>
                <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"></bean>
            </jaxrs:providers>

     maven需要引入jar包

    查询地址 : http://mvnrepository.com/search?q=cors  

    (cxf)相关的 jar包文件

    <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-security-cors -->
    <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-security-cors</artifactId>
    <version>3.1.5</version>  //版本号换成您心仪的版本
    </dependency>

  • 相关阅读:
    html表单
    html基础
    MySQL数据库 数据的更新
    JAVA数据库编程
    集合框架
    线程和进程
    反射
    centos 7环境
    js中的this
    javascript的作用域以及闭包现象
  • 原文地址:https://www.cnblogs.com/Tmc-Blog/p/5991926.html
Copyright © 2011-2022 走看看