java使用cors解決跨域問題,服務器端開發爲語言爲java

轉載:https://blog.csdn.net/s8460049/article/details/52173713html

先後端分離開發,各司其職,分工明確,加上如今大前端的發展的火熱,因此分離開發一定是一個趨勢,分離開發就必定會遇到跨域的問題。前端

 

仍是以前的意見,我的認爲最好的解決方案是經過服務器的反向代理來解決。java

 

固然根據項目的具體,有些時候也會採起不一樣的解決方案,web

今天這裏介紹一下cors解決,這裏是站在服務端的角度,講解須要作的工做,服務端語言爲java後端

 

<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>

 

參數講解跨域

cors.allowOrigin指的能夠經過的ip,*表明全部,可使用指定的ip,多個的話能夠用逗號分隔,默認爲*服務器

cors.supportedMethods指的是請求方式 默認爲*app

cors.supportedHeaders請求支持的頭信息,默認爲*cors

cors.exposedHeaders暴露的頭信息,默認的empy list前後端分離

cors.supportsCredentials支持證書,默認爲true

cors.maxAge 最大過時時間,默認爲-1

cors.tagRequests 默認爲false

cors.allowSubdomains容許子域 默認爲false

 

具體能夠查看com.thetransactioncompany.cors.CORSConfiguration這個類的源碼

 

官網文檔:

http://software.dzhuvinov.com/cors-filter-configuration.html

 

 

 

xml直接配置filter過濾器,對全部請求進行過濾,添加請求頭,這裏採用的是第三方jar包提供的過濾器,固然也能夠本身開發,

這裏提供一個簡單的實現

@Component  
    public class SimpleCORSFilter implements Filter {  
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {  
        HttpServletResponse response = (HttpServletResponse) res;  
        response.setHeader("Access-Control-Allow-Origin", "*");  
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");  
        response.setHeader("Access-Control-Max-Age", "3600");  
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");  
        chain.doFilter(req, res);  
      }  
      public void init(FilterConfig filterConfig) {}  
      public void destroy() {}  
    }

 


固然這裏也能夠用攔截器。

 

回到web.xml裏面配置的filter,使用的jar的依賴爲:

<!-- https://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter -->  
<dependency>  
    <groupId>com.thetransactioncompany</groupId>  
    <artifactId>cors-filter</artifactId>  
    <version>2.5</version>  
</dependency>

 

 

也可使用ebay提供,這個只有一個過濾器,資料還ting

<!-- https://mvnrepository.com/artifact/org.ebaysf.web/cors-filter -->  
<dependency>  
    <groupId>org.ebaysf.web</groupId>  
    <artifactId>cors-filter</artifactId>  
    <version>1.0.1</version>  
</dependency>
相關文章
相關標籤/搜索