Swagger跨域问题的解决

现象:可以访问以localhost:8080为为域名的Swagger,将localhost换成本机ip地址后出现无权限访问的问题,其实质为跨域问题

解决方法:

第一,在pom.xml中添加maven依赖

 
         
           com.thetransactioncompany  
           cors-filter  
        2.5 
 
  
第二,在web.xml中添加配置

 
     
        CORS  
        com.thetransactioncompany.cors.CORSFilter  
         
            cors.allowOrigin  
            *  
       
 
         
            cors.supportedMethods  
            GET, POST, HEAD, PUT, DELETE  
       
 
         
            cors.supportedHeaders  
            Accept, Origin, X-Requested-With, Content-Type, Last-Modified  
       
 
         
            cors.exposedHeaders  
            Set-Cookie  
       
 
         
            cors.supportsCredentials  
            true  
       
 
   
 
     
        CORS  
        /*  
   
 

注意,这里的配置是允许所有的ip访问,这样会相应的出现安全问题,大家请参考这篇文章:http://www.freebuf.com/articles/web/18493.html

你可能感兴趣的:(Swagger跨域问题的解决)