Spring MVC学习总结(11)——Spring MVC集成Swagger跨域问题

时间:2024-12-16 10:05:38
  <!-- CORS配置,为了让别的机器访问本机的swagger接口文档服务 -->  
       <dependency>  
           <groupId>com.thetransactioncompany</groupId>  
           <artifactId>cors-filter</artifactId>  
        <version>2.5</version> 
 </dependency>   
   <!-- cors配置 -->  

    <filter>  

        <filter-name>CORS</filter-name>  

        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>  

        <init-param>  

            <param-name>cors.allowOrigin</param-name>  

            <param-value>*</param-value>  

        </init-param>  

        <init-param>  

            <param-name>cors.supportedMethods</param-name>  

            <param-value>GET, POST, HEAD, PUT, DELETE</param-value>  

        </init-param>  

        <init-param>  

            <param-name>cors.supportedHeaders</param-name>  

            <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>  

        </init-param>  

        <init-param>  

            <param-name>cors.exposedHeaders</param-name>  

            <param-value>Set-Cookie</param-value>  

        </init-param>  

        <init-param>  

            <param-name>cors.supportsCredentials</param-name>  

            <param-value>true</param-value>  

        </init-param>  

    </filter>  

    <filter-mapping>  

        <filter-name>CORS</filter-name>  

        <url-pattern>/*</url-pattern>  

    </filter-mapping>  
注意,这里的配置是允许所有的ip访问,这样会相应的出现安全问题,大家请参考这篇文章:http://www.freebuf.com/articles/web/18493.html
Spring MVC学习总结(11)——Spring MVC集成Swagger跨域问题