前后端分离JSON跨域问题,已经配置了CORS跨越,依然无效 10C
 <!-- json跨域过滤器相关jar包and -->  
   <dependency>  
       <groupId>com.thetransactioncompany</groupId>  
       <artifactId>java-property-utils</artifactId>  
       <version>1.9.1</version>  
   </dependency>
    <!-- https://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter -->
    <dependency>
       <groupId>com.thetransactioncompany</groupId>
       <artifactId>cors-filter</artifactId>
       <version>1.3.2</version>
    </dependency> 
    <!-- json跨域过滤器相关end -->  

<!-- WEB.XML中JSON跨域处理 and-->
    <filter>  
       <description>跨域过滤器</description>  
       <filter-name>CORS</filter-name>    
       <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>    
       <init-param>
            <param-name>cors.allowGenericHttpRequests</param-name>
            <param-value>true</param-value>
       </init-param>   
       <init-param> 
            <param-name>cors.allowOrigin</param-name>    
            <param-value>*</param-value>    
       </init-param> 
       <init-param>
            <param-name>cors.allowSubdomains</param-name>
            <param-value>false</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>  
    <!-- JSON跨域处理 end-->

2个回答

public class CrossDomainFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException { 
    System.out.println("==初始化允许跨域访问的拦截器=="); 
}

@Override
public void doFilter(ServletRequest request, ServletResponse resp,
        FilterChain chain) throws IOException, ServletException {    

    HttpServletResponse response  = (HttpServletResponse)resp; 

    response.addHeader("Access-Control-Allow-Origin", "*"); 
    chain.doFilter(request, response); 
}

@Override
public void destroy() {

} 

}

我是直接写一个跨域的fitler、需要跨域的请求都放到中国风fitle下面 不过有些情况可能不行

filter-class 路径具体到方法名

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问