java后端解决跨域问题

跨域问题

最常见的因为前后端分离,浏览器的保护机制就产生了跨域问题

vue前端:

    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    baseURL: 'http://localhost:8090/',
    timeout: 5000,
    responseType: 'json',   // 响应数据格式
    responseEncoding: 'utf8',  // 响应数据编码
});

java代码:过滤器

package com.service.config;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @program: service
 * @description: 前端跨域
 * @author: 单人影
 * @create: 2019-12-07 18:20
 **/
@Configuration
public class CorsConfig implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Expose-Headers", "*");
        response.setContentType("application/json;charset=UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

或者
使用注解: @CrossOrigin
在单个请求的方法上加 @CrossOrigin注解

public class SimpleCORSFilter implements Filter{


@Override
public void destroy() {
 

}


@Override
public void doFilter(ServletRequest request, ServletResponse response,  
                         FilterChain chain) throws IOException, ServletException {  
        try {  
            HttpServletRequest httpRequest = (HttpServletRequest) request;  
            HttpServletResponse httpResponse = (HttpServletResponse) response;  
  
            // 跨域  
            String origin = httpRequest.getHeader("Origin");  
            if (origin == null) {  
                httpResponse.setHeader("Access-Control-Allow-Origin", "*");  
            } else {  
                httpResponse.setHeader("Access-Control-Allow-Origin", origin);  
            }  
            httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, x-requested-with, Content-Type, Accept,X-Cookie");  
            httpResponse.setHeader("Access-Control-Allow-Credentials", "true");  
            httpResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");  
            if ( httpRequest.getMethod().equals("OPTIONS") ) {  
                httpResponse.setStatus(HttpServletResponse.SC_OK);  
                return;  
            }  
            chain.doFilter(request, response);  
        } catch (Exception e) {  
            throw e;  
        }  
    }  


	@Override
	public void init(FilterConfig arg0) throws ServletException {
 
	}
}

你可能感兴趣的:(后端)